-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_loop_move_head.asm
executable file
·367 lines (270 loc) · 6.35 KB
/
5_loop_move_head.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
; /////////////////////////////////////////////////////////////////////////////
; Znake (ZX Spectrum 48K)
; -----------------------------------------------------------------------------
; 5_loop_move_head.asm
; -----------------------------------------------------------------------------
; Copyright (C) 2016, Chris Wyatt
; All rights reserved
; Distributed under the Apache 2 license (see LICENSE)
; /////////////////////////////////////////////////////////////////////////////
macro move_head_hv, direction
; Load head location to register b
ld b,e
; Depending on the current direction, the new head location may be placed
; to the right or to the left ...
if direction = 0x08
bit 0,d
jr nz,head_turning_up_right
bit 1,d
jr nz,head_turning_up_left
endif
if direction = 0x04
bit 0,d
jr nz,head_turning_down_right
bit 1,d
jr nz,head_turning_down_left
endif
if direction = 0x02
bit 2,d
jr nz,head_turning_left_down
bit 3,d
jr nz,head_turning_left_up
endif
if direction = 0x01
bit 2,d
jr nz,head_turning_right_down
bit 3,d
jr nz,head_turning_right_up
endif
; Low bit of neck graphics
if direction = 0x08 || direction = 0x04
ld c,0x28
endif
if direction = 0x02 || direction = 0x01
ld c,0x30
endif
; Draw later
push bc
; ... otherwise the head is still moving vertically
; Get head location again and ...
if direction = 0x08
; shift it up
dec b
endif
if direction = 0x04
; shift it down
inc b
endif
if direction = 0x02
; shift it left
ld a,b
sub 0x10
ld b,a
endif
if direction = 0x01
; shift it right
ld a,b
add a,0x10
ld b,a
endif
; Low bit of head graphics
if direction = 0x08
ld c,0x08
endif
if direction = 0x04
ld c,0x18
endif
if direction = 0x02
ld c,0x20
endif
if direction = 0x01
ld c,0x10
endif
; Draw later
push bc
; Update the existing head location in our table
ld (hl),b
endm
macro turn_head, prev_direction, new_direction
; Low bit of neck graphics
if prev_direction = 0x08
if new_direction = 0x02
ld c,0x50
endif
if new_direction = 0x01
ld c,0x38
endif
endif
if prev_direction = 0x04
if new_direction = 0x02
ld c,0x48
endif
if new_direction = 0x01
ld c,0x40
endif
endif
if prev_direction = 0x02
if new_direction = 0x08
ld c,0x40
endif
if new_direction = 0x04
ld c,0x38
endif
endif
if prev_direction = 0x01
if new_direction = 0x08
ld c,0x48
endif
if new_direction = 0x04
ld c,0x50
endif
endif
; Draw later
push bc
; Move head
if prev_direction = 0x08 || prev_direction = 0x04
ld a,b
if new_direction = 0x02
sub 0x10
endif
if new_direction = 0x01
add a,0x10
endif
ld b,a
endif
if prev_direction = 0x02 || prev_direction = 0x01
if new_direction = 0x08
dec b
endif
if new_direction = 0x04
inc b
endif
endif
; Low bit of head graphics
if prev_direction = 0x08 || prev_direction = 0x04
if new_direction = 0x02
ld c,0x20
endif
if new_direction = 0x01
ld c,0x10
endif
endif
if prev_direction = 0x02 || prev_direction = 0x01
if new_direction = 0x08
ld c,0x08
endif
if new_direction = 0x04
ld c,0x18
endif
endif
; Draw later
push bc
; Move pointer to new head location
inc l
; Add new head location to our table
ld (hl),b
ld a,(snake_history_head_offset)
inc a
ld (snake_history_head_offset),a
endm
update_head_history:
ld a,(snake_direction_current)
ld d,a
ld h,(TBL_SNAKE_HISTORY >> 8) & $FF
; Move pointer to end of snake history
ld a,(snake_history_head_offset)
ld l,a
; Load current head location to register e
ld e,(hl)
; Move pointer to turn (or tail) before head
dec l
; Load current turn location to register b
ld b,(hl)
; Move pointer back to head
inc l
; Subtract turn location from head location
ld a,e
xor b
cp 0x10
jp c,head_moving_vertically
; Head moving horizontally
; Restore head location
xor b
and 0xf0
ld c,a
ld a,b
and 0xf0
sub c
jr c,head_moving_right
; Head moving left
move_head_hv 0x02
jp check_food_eaten
head_turning_left_down:
turn_head 0x02, 0x04
jp check_food_eaten
head_turning_left_up:
turn_head 0x02, 0x08
jp check_food_eaten
head_moving_right:
move_head_hv 0x01
jp check_food_eaten
head_turning_right_down:
turn_head 0x01, 0x04
jp check_food_eaten
head_turning_right_up:
turn_head 0x01, 0x08
jp check_food_eaten
head_moving_vertically:
; Restore head location
xor b
and 0x0f
ld c,a
ld a,b
and 0x0f
sub c
jr c,head_moving_down
; Head moving up
move_head_hv 0x08
jp check_food_eaten
head_turning_up_right:
turn_head 0x08, 0x01
jp check_food_eaten
head_turning_up_left:
turn_head 0x08, 0x02
jp check_food_eaten
head_moving_down:
move_head_hv 0x04
jp check_food_eaten
head_turning_down_right:
turn_head 0x04, 0x01
jp check_food_eaten
head_turning_down_left:
turn_head 0x04, 0x02
check_food_eaten:
ld a,(current_food_location)
cp (hl)
; If location of head does not equal current food location, continue to
; update tail history routine
jr nz,update_tail_history
; Otherwise, set food eaten flag ...
ld hl,flags
set 0,(hl)
; Record snake length increase
ld hl,snake_length
inc (hl)
; Increase score by 10
ld hl,score
ld a,10
add a,(hl)
daa
ld (hl),a
inc hl
ld a,0
adc a,(hl)
daa
ld (hl),a
dec hl ; hl = score
ld de,str_score
call gen_score_str
; Skip tail update (i.e. grow snake)
jp collision_detection