-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
283 lines (229 loc) · 4.79 KB
/
snake.asm
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
;; SETUP
org 7C00h
jmp setup_game
;; CONSTANTS
VIDMEM equ 0B800h
SCREENW equ 80
SCREENH equ 25
WINCOND equ 10
BGCOLOR equ 1020h
APPLECOLOR equ 4020h
SNAKECOLOR equ 2020h
TIMER equ 046Ch
SNAKEXARRAY equ 1000h
SNAKEYARRAY equ 2000h
UP equ 0
DOWN equ 1
LEFT equ 2
RIGHT equ 3
;;VARIABLES
playerX: dw 40
playerY: dw 12
appleX: dw 16
appleY: dw 8
direction: db 4
snakeLength: dw 1
;;LOGIC-----------
setup_game:
;; Set video mode - VGA mode 03h (80x25 text mode, 16 colors)
mov ax, 0003h
int 10h
;; Set up video memory
mov ax, VIDMEM
mov es, ax
;; Set 1st sanake segment "head"
mov ax, [playerX]
mov word [SNAKEXARRAY], ax
mov ax, [playerY]
mov word [SNAKEYARRAY], ax
;; HIDE CURSUR
mov ah, 02h
mov dx, 2600h ;DH=ROW DL=COL
int 10h
;; GAME LOOP
game_loop:
;; Clear screen every loop iteration
mov ax, BGCOLOR
xor di, di
mov cx, SCREENW*SCREENH
rep stosw
;; Draw snake
xor bx, bx
xor cx, [snakeLength]
mov ax, SNAKECOLOR
.snake_loop:
imul di, [SNAKEYARRAY+bx], SCREENW*2 ; Y POSITION OF SNAKE, 2 BYTES
imul dx, [SNAKEXARRAY+bx], 2
add di, dx
stosw
inc bx
inc bx
loop .snake_loop
;; Draw apple
imul di, [appleY], SCREENW*2
imul dx, [appleX], 2
add di, dx
mov ax, APPLECOLOR
stosw
;; MOVE snake in current direction
mov al, [direction]
cmp al, UP
je move_up
cmp al, DOWN
je move_down
cmp al, LEFT
je move_left
cmp al, RIGHT
je move_right
jmp update_snake
move_up:
dec word [playerY] ;move up 1 row
jmp update_snake
move_down:
inc word [playerY] ;move down 1 row
jmp update_snake
move_left:
dec word [playerX] ;move left 1 row
jmp update_snake
move_right:
inc word [playerX] ;move right 1 row
;; update snake position.....
update_snake:
;; update all snake segments past head
imul bx, [snakeLength], 2
.snake_loop:
mov ax, [SNAKEXARRAY-2+bx]
mov word [SNAKEXARRAY+bx], ax
mov ax, [SNAKEYARRAY-2+bx]
mov word [SNAKEYARRAY+bx], ax
dec bx
dec bx
jnz .snake_loop
;; store updated values to head....
mov ax, [playerX]
mov word [SNAKEXARRAY], ax
mov ax, [playerY]
mov word [SNAKEYARRAY], ax
;; Lose conditions
;; 1) hit borders
cmp word [playerY], -1 ;top of screen
je game_lost
cmp word [playerY], SCREENH ; Bottom of screen
je game_lost
cmp word [playerX], -1 ; Left screen
je game_lost
cmp word [playerX], SCREENW ; Right of screen
je game_lost
;; 2) hit parrt of snake
cmp word [snakeLength], 1
je get_player_input
mov bx, 2 ; Array indexes, start at 2nd array
mov cx, [snakeLength] ; Loop counter........
check_hit_snake_loop:
mov ax, [playerX]
cmp ax, [SNAKEXARRAY+bx]
jne .increment
mov ax, [playerY]
cmp ax, [SNAKEYARRAY+bx]
je game_lost ; hit snake body........
.increment:
inc bx
inc bx
loop check_hit_snake_loop
get_player_input:
mov bl, [direction] ; save current direction
mov ah, 1
int 16h ;get Keyboard status
jz check_apple ; if no key was pressed move on..
xor ah, ah
int 16h ; get keystrokes, AH=scancode, AL=asciichar entered
cmp al, 'w'
je w_pressed
cmp al, 's'
je s_pressed
cmp al, 'a'
je a_pressed
cmp al, 'd'
je d_pressed
jmp check_apple
w_pressed:
mov bl, UP
jmp check_apple
s_pressed:
mov bl, DOWN
jmp check_apple
a_pressed:
mov bl, LEFT
jmp check_apple
d_pressed:
mov bl, RIGHT
jmp check_apple
;; did player hit apple?........
check_apple:
mov byte [direction], bl
mov ax, [playerX]
cmp ax, [appleX]
jne delay_loop
mov ax, [playerY]
cmp ax, [appleY]
jne delay_loop
; hit apple , increase snake length.............
inc word [snakeLength]
cmp word [snakeLength], WINCOND
je game_won
next_apple:
;; random X position
xor ah, ah
int 1Ah ;timer ticks
mov ax, dx
xor dx, dx
mov cx, SCREENW
div cx
mov word [appleX], dx
;; random Y pos
xor ah, ah
int 1Ah ;timer ticks
mov ax, dx
xor dx, dx
mov cx, SCREENH
div cx
mov word [appleY], dx
;; check if apple spawn
xor bx, bx
mov cx, [snakeLength]
.check_loop:
mov ax, [appleX]
cmp ax, [SNAKEXARRAY+bx]
jne .increment
mov ax, [appleY]
cmp ax, [SNAKEYARRAY+bx]
je next_apple
.increment:
inc bx
inc bx
loop .check_loop
delay_loop:
mov bx, [TIMER]
inc bx
inc bx
.delay:
cmp [TIMER], bx
jl .delay
jmp game_loop
;; GAME END CONDITIONS
game_won:
mov dword [ES:0000], 1F491F57h ;WI
mov dword [ES:0004], 1F211F4Eh ;N!
jmp reset
game_lost:
mov dword [ES:0000], 1F4F1F4ch ;LO
mov dword [ES:0004], 1F451F53h ;SE
;; RESET
reset:
xor ah, ah
int 16h
jmp 0FFFFh:0000h ; reboot
;; int 19h ;alternative restart qemu
;;BOOT SECTOR PADDING
times 510 - ($-$$) db 0
dw 0AA55h