-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnake.s
More file actions
127 lines (127 loc) · 2.59 KB
/
snake.s
File metadata and controls
127 lines (127 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
setup:
mov r1, #0x00FF00 // snake color
mov r2, #0xFFFFFF // background color
mov r3, #271 // tail pos
mov r4, #272 // head pos
mov r5, #0xFF0000 // apple color
mov r6, #520 // apple position
mov r7, #767 // wrap location/size of screen
mov r8, #snake_body // front of queue - tail location location
add r9, r8, #1 // back of queue - head location location
mov r10, #68 // movement direction
str r3, [r8]
str r4, [r9]
initial_draw:
str r1, [r3+256]
str r1, [r4+256]
move:
str r5, [r6+256] // draw apple
inp r0, 4
handle_keypresses:
cmp r0, #83 // S key
beq go_down
cmp r0, #65 // A key
beq go_left
cmp r0, #68 // D key
beq go_right
cmp r0, #87 // W key
beq go_up
mov r0, r10
b handle_keypresses
go_right:
cmp r10, #65 // if was left
beq reset_key
mov r10, r0
add r4, r4, #1
and r0, r4, #31
cmp r0, #0 // note; change back if wrong
beq rwrap
b draw
rwrap:
sub r4, r4, #31
b draw
go_left:
cmp r10, #68 // if was right
beq reset_key
mov r10, r0
sub r4, r4, #1
and r0, r4, #31
cmp r0, #31
beq lwrap
b draw
lwrap:
add r4, r4, #32
b draw
go_up:
cmp r10, #83 // if was down
beq reset_key
mov r10, r0
sub r4, r4, #32
cmp r4, #0
blt uwrap
b draw
uwrap:
add r4, r4, #736
b draw
go_down:
cmp r10, #87 // if was up
beq reset_key
mov r10, r0
add r4, r4, #32
cmp r4, r7
bgt dwrap
b draw
dwrap:
sub r4, r4, #736
draw:
cmp r4, r6 // check if in apple location
beq create_apple // do not move tail - apple attained
move_tail:
ldr r0, [r8]
str r2, [r0+256] // clear tail
add r8, r8, #1 // inc tail pointer
cmp r8, #200
blt move_head
mov r8, #snake_body
move_head:
add r9, r9, #1 // inc head pointer
cmp r9, #200
blt actually_move_head
mov r9, #snake_body
actually_move_head:
str r4, [r9]
ldr r0, [r4+256] // check if snake is self-intersecting via reading screen memory
cmp r0, r1
beq game_over
cmp r8, r9 // check for win condition - head and tail in same spot means it's very long, so you win
beq game_win
str r1, [r4+256] // draw head
b move // loop infinitely
create_apple:
inp r6, 8
mov r0, #1023 // too big for immediate parameter
and r6, r6, r0
cmp r6, r7
bgt create_apple // loop again if out of range
cmp r6, r4
beq create_apple
b move_head
reset_key:
mov r0, r10
b handle_keypresses
game_over:
mov r0, #loss_message
out r0, 8
halt
game_win:
mov r0, #win_message
out r0, 8
halt
loss_message:
dat 0x20756f59 // "You "
dat 0x65736f4c // "Lose"
dat 0 // ␀
win_message:
dat 0x20756f59 // "You"
dat 0x006e6957 // "Win␀"
snake_body: dat 0