-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
653 lines (623 loc) · 13.1 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
;;
; Copyright Jacques Deschênes 2023,2024
; This file is part of stm8-gamepad
;
; stm8-gamepad is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; stm8-gamepad is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with ntsc_tuto. If not, see <http://www.gnu.org/licenses/>.
;;
;------------------------
; snake game
; snake grow by eating mouse
; 2 buttons used LEFT|RIGHT
; to change snake direction
; collision with walls,itself
; and boulder end game
;------------------------
.area G_DATA (ABS)
.org 4
app_variables:
score: .blkw 1 ; game score
speed: .blkb 1 ; snake speed delay
chrono: .blkb 1 ; chronometer delay
max_score: .blkw 1 ; maximum score
game_flags: .blkb 1 ; game boolean flags
snake_len: .blkb 1 ; snake length
snake_dir: .blkb 1 ; head direction
food_coord: .blkw 1 ; food coordinates
snake_body: .blkw 32 ; snake rings coords
.area CODE
; chronometer delay
CHRONO_DELAY=127
; snake speed value
MIN_SPEED=1
MAX_SPEED=9
; borders position and area dimensions
LEFT_BORDER=0 ; left fence position
MIN_XCOOR=LEFT_BORDER+1; minimum snake x coordinate
RIGHT_BORDER=(HRES-4)&0xFC ; right fence position , 4 pixels reserved for calories bargraph.
MAX_XCOOR=(RIGHT_BORDER-SNAKE_SPRITE_WIDTH) ; maximum snake x coordinate
PG_WIDTH=(RIGHT_BORDER-MIN_XCOOR) ; play ground width
TOP_BORDER=FONT_HEIGHT ; top fence y position , 1 character height reserved for status display
MIN_YCOOR=(TOP_BORDER+1) ; minimum snake y coordinate
BOTTOM_BORDER=(VRES-1) ; bottom fence position
MAX_YCOOR=(BOTTOM_BORDER-SNAKE_SPRITE_HEIGHT)
PG_HEIGHT=(BOTTOM_BORDER-MIN_YCOOR); play ground height
; game boolean flags
F_FOOD_COLL=0 ; collision with food, earn point
F_NO_FOOD=1 ; no food available
F_GAME_OVER=2 ; game ended
F_POO=3 ; snake poo
; snake heading
NORTH=0
EAST=1
SOUTH=2
WEST=4
; game sprites
; first 2 numbers: width,height
SNAKE_SPRITE_WIDTH=4
SNAKE_SPRITE_HEIGHT=4
HEAD_UP: .byte SNAKE_SPRITE_WIDTH,SNAKE_SPRITE_HEIGHT,0X60,0X60,0X90,0X60
HEAD_RIGHT: .byte SNAKE_SPRITE_WIDTH,SNAKE_SPRITE_HEIGHT,0X40,0XB0,0XB0,0X40
HEAD_DOWN: .byte SNAKE_SPRITE_WIDTH,SNAKE_SPRITE_HEIGHT,0X60,0X90,0X60,0X60
HEAD_LEFT: .byte SNAKE_SPRITE_WIDTH,SNAKE_SPRITE_HEIGHT,0X20,0XD0,0XD0,0X20
RING: .byte SNAKE_SPRITE_WIDTH,SNAKE_SPRITE_HEIGHT,0x60,0x90,0X90,0X60
MOUSE_WIDTH=5
MOUSE_HEIGHT=4
MOUSE: .byte MOUSE_WIDTH,MOUSE_HEIGHT,0X00,0X70,0XF8,0XA0
POO_WIDTH=4
POO_HEIGHT=4
POO: .byte POO_WIDTH,POO_HEIGHT,0x40,0x60,0x70,0xf0
CB_WIDTH=3
CB_HEIGHT=1
CHRONO_BAR: .byte CB_WIDTH,CB_HEIGHT,0x70
;----------------------
; draw walls around
; game area
;---------------------
draw_walls:
; top fence
ldw x,#(TOP_BORDER<<8)
ldw y,#(TOP_BORDER<<8)+RIGHT_BORDER+1
call line
; bottom fence
ldw x,#(BOTTOM_BORDER<<8)
ldw y,#(BOTTOM_BORDER<<8)+RIGHT_BORDER+1
call line
; left fence
ldw x,#(TOP_BORDER<<8)
ldw y,#(BOTTOM_BORDER<<8)
call line
; right fence
ldw x,#(TOP_BORDER<<8)+RIGHT_BORDER
ldw y,#(BOTTOM_BORDER<<8)+RIGHT_BORDER
call line
ret
;---------------------------
; draw sprite
; input:
; XH y coord
; XL x coord
; Y sprite data
;----------------------------
draw_sprite:
ld a,(1,y)
addw y,#2
jp put_sprite
;---------------------------
; select head sprite from
; value of snake_dir
; and draw it
; input:
; X position
;---------------------------
draw_head:
ldw y,#6
_ldaz snake_dir
mul y,a
addw y,#HEAD_UP
jra draw_sprite
;--------------------------
; snake initial draw
;--------------------------
LEN=1
IDX=LEN+1
VAR_SIZE=IDX+1
draw_snake:
_vars VAR_SIZE
ldw x,snake_body
call draw_head
_ldaz snake_len
dec a
ld (LEN,sp),a
ldw x,#snake_body+2
1$:
ldw (IDX,sp),x ; array index
ldw y,#RING
ldw x,(x)
call draw_sprite
dec (LEN,sp)
jreq 9$
ldw x,(IDX,sp)
addw x,#2 ; next element
jra 1$
9$:
_drop VAR_SIZE
ret
;---------------------
; shit happen
;---------------------
snake_poo:
push a
pushw x
_ldaz snake_len
dec a
clrw x
ld xl,a
addw x,#snake_body
ldw x,(x)
ldw y,#POO
call draw_sprite
popw x
pop a
bres game_flags,#F_POO
ret
;-----------------------------
; check for collision object
; if not mouse then game over.
; if MOUSE increase score and
; delete mouse.
; input:
; X position
; output:
; game_flags:F_FOOD_COLL
; X unchanged
;-------------------------------
POS=1
GAIN=2
VAR_SIZE=2
food_collision:
pushw x
_vars VAR_SIZE
clr (GAIN,sp)
_clrz game_flags
ld a,#MOUSE_WIDTH
add a,#SNAKE_SPRITE_WIDTH
ld (POS,sp),a
ld a,xl ; snake head x coord
sub a,food_coord+1 ; food x coord
jrpl 1$
neg a ; abs(delta)
1$: ; delta X
cp a,(POS,sp)
jrpl 3$ ; if delta X >= 0 collision object not mouse
ld a,#MOUSE_HEIGHT
add a,#SNAKE_SPRITE_HEIGHT
ld (POS,sp),a
ld a,xh ; head y coord
sub a,food_coord ; food y coord
jrpl 2$
neg a
2$: ; delta Y
cp a,(POS,sp)
jrmi 4$
3$: ; if delta Y >= 0 collision object not mouse
bset game_flags,#F_GAME_OVER
jrpl 9$
4$: ; collision with mouse
; erase food
ldw x,food_coord
ldw y,#MOUSE
call draw_sprite
bset game_flags,#F_NO_FOOD
bset game_flags,#F_FOOD_COLL
_incz snake_len
inc (GAIN,sp)
ld a,food_coord ; mouse y coord
cp a,#MIN_YCOOR
jrne 5$
inc (GAIN,sp) ; food at top border
5$: cp a,#MAX_YCOOR
jrne 6$
inc (GAIN,sp) ; food at bottom border
6$: ld a,food_coord+1 ; mouse x coord
cp a,#MIN_XCOOR
jrne 7$
inc (GAIN,sp) ; food at left border
7$: cp a,#MAX_XCOOR
jrne 8$
inc (GAIN,sp) ; food at right border
8$: ; score+=(MAX_SPEED+1-speed)*(GAIN,sp)
ld a,#MAX_SPEED+1
sub a,speed
clrw x
ld xl,a
ld a,(GAIN,sp)
mul x,a ; gain
ld a,xl
addw x,score
_strxz score
cp a,#6
jrmi 81$
bset game_flags,#F_POO
81$:
_clrz food_coord
_clrz food_coord+1
call beep
9$:
_drop VAR_SIZE
popw x
ret
;---------------------
; according to
; actual head position
; and direction compute
; next snake head
; coordinates
; input:
; X actual position
; output:
; X next position
;---------------------
next_head_pos:
_ldaz snake_dir
cp a,#NORTH
jrne 2$
; going north
subw x,#SNAKE_SPRITE_HEIGHT<<8
jra 9$
2$: cp a,#EAST
jrne 4$
;going east
addw x,#SNAKE_SPRITE_WIDTH
jra 9$
4$: cp a,#SOUTH
jrne 6$
;going south
addw x,#SNAKE_SPRITE_HEIGHT<<8
jra 9$
6$: ; going west
subw x,#SNAKE_SPRITE_WIDTH
9$:
ret
;------------------------------
; move memory block from
; low address to higher address
; input:
; A count
; X destination
; Y source
;-------------------------------
move_array_up:
push a
1$: ld a,(y)
decw y
ld (x),a
decw x
dec (1,sp)
jrne 1$
_drop 1
ret
;-----------------------
; move snake
;-----------------------
POS=1 ; new head coordinates
HEAD=POS+2 ; actual head position
TAIL=HEAD
VAR_SIZE=HEAD+1
move_snake:
pushw x
pushw y
_vars VAR_SIZE
; erase head by drawing over it
ldw x,snake_body
ldw (HEAD,sp),x
call draw_head
; draw ring at head position
ldw x,(HEAD,sp) ; head position
ldw y,#RING
call draw_sprite
;draw head at new position
ldw x,(HEAD,sp)
call next_head_pos
ldw (POS,sp),x
call draw_head
jreq 1$
;collision deteted
ldw x,(POS,sp)
call food_collision
btjt game_flags,#F_GAME_OVER,9$
1$:
; move array elements 1 cell toward tail
_ldaz snake_len
dec a
sll a
clrw x
ld xl,a
addw x,#snake_body ; last array element
ldw y,x
ldw x,(x)
ldw (TAIL,sp),x ; last ring position
ldw x,y
decw y
incw x
_ldaz snake_len
dec a
sll a
call move_array_up
; set 1 element as new head position
ldw x,(POS,sp)
ldw snake_body,x
btjt game_flags,#F_FOOD_COLL,8$
; erase last ring
ldw x,(TAIL,sp)
ldw y,#RING
call draw_sprite
jra 9$
8$:
btjf game_flags,#F_POO,9$
call snake_poo
9$:
_drop VAR_SIZE
popw y
popw x
ret
;--------------------------
; rotate snake head
; input
; A {LEFT,RIGHT}
;--------------------------
rotate_head:
push a
_ldaz snake_dir
ldw y,#6
mul y,a
addw y,#HEAD_UP
ldw x,snake_body
call draw_sprite
pop a
cp a,#BTN_LEFT
jrne 2$
_ldaz snake_dir
dec a
jra 4$
2$: _ldaz snake_dir
inc a
4$: and a,#3
_straz snake_dir
ldw y,#6
mul y,a
addw y,#HEAD_UP
ldw x,snake_body
call draw_sprite
ret
;--------------------------
; read keypad
; LEFT turn left
; RIGHT turn right
; UP increase speed
; DOWN decreas speed
;--------------------------
KPAD=1
user_input:
push #0
call read_keypad
jreq 8$
ld (KPAD,sp),a
ld a,#BTN_LEFT
and a,(KPAD,sp)
jreq 2$
call rotate_head
jra 6$
2$: ld a,#BTN_RIGHT
and a,(KPAD,sp)
jreq 3$
call rotate_head
jra 6$
3$:
ld a,#BTN_UP
and a,(KPAD,sp)
jreq 4$
ld a,#MIN_SPEED
cp a,speed
jreq 6$
_decz speed
call prt_info
jra 6$
4$: ld a,#BTN_DOWN
and a,(KPAD,sp)
jreq 6$
ld a,#MAX_SPEED
cp a,speed
jreq 6$
_incz speed
call prt_info
6$:
ldw x,#10
call wait_key_release
8$:
_drop 1
ret
;-----------------------
; when snake eat mouse
; reset chronometer
;-----------------------
chrono_reset:
push #CHRONO_DELAY
1$:
_ldaz chrono
cp a,(1,sp)
jrpl 9$
ld a,#VRES-1
sub a,chrono
ld xh,a
ld a,#RIGHT_BORDER+1
ld xl,a
ldw y,#CHRONO_BAR
call draw_sprite
_incz chrono
jra 1$
9$: _drop 1
ret
;-------------------------
; decrement chrono
; remove a chrono tick
;-------------------------
chrono_decr:
_decz chrono
ld a,#VRES-1
sub a,chrono
ld xh,a
ld a,#(RIGHT_BORDER+1)
ld xl,a
ldw y,#CHRONO_BAR
call draw_sprite
tnz chrono
ret
;-------------------------
; create a new mouse
; at random position
;-------------------------
new_food:
call prng
ld a,#PG_HEIGHT
div x,a
add a,#MIN_YCOOR
_straz food_coord
call prng
ld a,#PG_WIDTH
div x,a
add a,#MIN_XCOOR
_straz food_coord+1
ldw x,food_coord
ldw y,#MOUSE
call draw_sprite
tnz a
jreq 9$
ldw x,food_coord
ldw y,#MOUSE
call draw_sprite
jra new_food
9$: _clrz game_flags
call chrono_reset
ret
;----------------------
; print score top left
; corner
;----------------------
prt_info:
pushw x
_clrz cx
_clrz cy
ldw y,#score_str
call tv_puts
_ldxz score
call put_uint16
ld a,#12
_straz cx
ldw y,#speed_str
call tv_puts
ld a,#MAX_SPEED+1
sub a,speed
clrw x
ld xl,a
call put_uint16
ld a,#24
_straz cx
ldw y,#max_str
call tv_puts
ldw x,max_score
call put_uint16
popw x
ret
score_str: .asciz "SCORE:"
speed_str: .asciz "SPEED:"
max_str: .asciz "max:"
;-------------------------
; game initialization
;-------------------------
snake_init:
ld a,#(1<<F_NO_FOOD)
_straz game_flags
_clrz chrono
ld a,#5
_straz speed
clrw x
_strxz score
_strxz food_coord
call set_seed ; using ticks
ld a,#3
_straz snake_len
ld a,#EAST
_straz snake_dir
ldw x,#snake_body
ldw y,#(31<<8)+48
ldw (x),y
subw y,#SNAKE_SPRITE_WIDTH
ldw (2,x),y
subw y,#SNAKE_SPRITE_WIDTH
ldw (4,x),y
call tv_cls
call draw_walls
call draw_snake
ret
;-------------------------
; game main routine
;-------------------------
snake:
call snake_init
ld a,#30
call pause
1$:
btjf game_flags,#F_NO_FOOD,2$
call prt_info
call new_food
2$:
call move_snake
btjt game_flags,#F_GAME_OVER,game_over
call user_input
call chrono_decr
jreq timeout
_ldaz speed
call noise
jra 1$
timeout:
ldw x,#(12<<8)+10
_strxz cy
ldw y,#timeout_str
call tv_puts
ld a,#60
call pause
game_over:
_ldxz score
cpw x,max_score
jrmi 4$
_strxz max_score
4$:
ldw x,#0xffff
call wait_key_release
call tv_cls
ldw y,#gover
call tv_puts
ldw y,#score_str
call tv_puts
_ldxz score
call put_uint16
call crlf
ldw x,#(3<<8)
call again
cp a,#BTN_A
jreq snake
9$:
ret
gover: .asciz "game over\r"
timeout_str: .asciz "TIME OUT"