-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
1607 lines (1346 loc) · 35.2 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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
PROCESSOR 6502
include "lib/vcs.h"
include "lib/macro.h"
;---------------------------------------------------
;
; General Documentation
;
;---------------------------------------------------
; Playfield "coordinates" (10 x 8)
;
; 1 2 3 4 5 6 7 8
; | | | | | | | |
; 1 -- 00 01 02 03 04 05 06 07
; 2 -- 08 09 0a 0b 0c 0d 0e 0f
; 3 -- 10 11 12 13 14 15 16 17
; 4 -- 18 19 1a 1b 1c 2d 2e 2f
; 5 -- 20 21 22 23 24 25 26 27 -- middle
; 6 -- 28 29 2a 2b 2c 2d 2e 2f -- middle
; 7 -- 30 31 32 33 34 35 36 37
; 8 -- 38 39 3a 3b 3c 3d 3e 3f
; 9 -- 40 41 42 43 44 45 46 47
; 10 -- 48 49 4a 4b 4c 4d 4e 4f
; | |
; | middle
; middle
;---------------------------------------------------
;
; Constants
;
;---------------------------------------------------
SNAKECOLORNORMAL = #$c4
SNAKECOLORSLOW = #$94
SNAKECOLORFAST = #$42
SPEEDSLOW = #22
SPEEDNORMAL = #16
SPEEDFAST = #11
SIZEOFMAP = #$50
;---------------------------------------------------
;
; RAM Variable Declarations
;
;---------------------------------------------------
; variables here do not end up in ROM
SEG.U VARS
ORG $80 ; RAM starts at $80
ROWS ds 10 ; each byte = a row, bits 0-7 = columns 1 through 8
RANDOM ds 1 ; random number taken from RIOT chip timer
SNAKELENGTH ds 1 ; length of snake
SCORE ds 1 ; score: number of apples collected
; playfield location is determined by wrapping from
; left to right, up to down: #$00 (0 dec) to #$4f (79 dec)
SNAKEHEADLOCATION ds 1 ; where in the 10 x 8 playfield the snake's head is located
SNAKEOLDTAILPOINTER ds 1 ; where int the 10 x 8 playfield the snake's head is located
APPLELOCATION ds 1 ; where in the 10 x 8 playfield the apple is located
HEADDIRECTION ds 1 ; direction we want the snake to move in next turn
STOREDDIRECTION ds 1 ; direction that was last used
; points to where the head currently is in memory
HEADHISTORYPOINTER ds 1 ; pointer for where in the HEADHISTORY the current head is in
GAMEMODE ds 1 ; 0 = game playing, 1 = game over, 2 = game selection
LOOPROW ds 1 ; used to help draw the 10 rows in the playfield
ROWNUMBER ds 1 ; used in gamefield positioning calculation
COLUMNMASK ds 1 ; used in gamefield positioning calculation
COLUMNMASKINVERTED ds 1 ; inverted version of the COLUMNMASK
RENDERCOUNTER ds 1 ; a counter for determining if snake collision logic should be executed in the current frame
SELECTEDGAMETYPE ds 1 ; 0 = normal, 1 = slow, 2 = fast
DEBOUNCE ds 1 ; 0 = can select another option, 1 = user needs to release select lever
LEFTDIGITOFFSET ds 1 ; the scanline offset for the left digit of the score
RIGHTDIGITOFFSET ds 1 ; the scanline offset for the right digit of the score
LEVELOFFSET ds 1 ; the scanline offset for the selection menu level "L" character
SELECTEDGAMETYPEOFFSET ds 1 ; the scanline offset for the selection menu level (0, 1, 2) number
HEADERITERATIONSTORE ds 1 ; a helper storage variable for header rendering
RENDERSPEED ds 1 ; how fast the "game speed" is
SOUNDITERATOR ds 1 ; a variable containing the number of frames left for the sound to play
ORG $a8 ; last 80 spots in ram, tracking snake head history
HEADHISTORY ds 80 ; a "history" of where the snake head has been in the 10 x 8 playfield
;---------------------------------------------------
;
; ROM Code
;
;---------------------------------------------------
SEG CODE
ORG $F800 ; 2k ROM
;---------------------------------------------------
;
; Initialize Console and Ram Variables
;
;---------------------------------------------------
InitSystem:
; set RAM, TIA registers and CPU registers to 0
CLEAN_START
; seed the random number generator
lda INTIM ; random value
sta RANDOM ; use as seed
; init rows of the 10 x 8 playfield
ldx #10; 10 rows
initrows:
dex
lda #%11111111 ; bits 0-7 all enabled
sta ROWS,x
cpx #0
bne initrows
; add player 1 support
lda #%11110000
sta SWCHA
; init score
lda #0
sta SCORE
; setting render counter to 0
lda #0
sta RENDERCOUNTER
; setting game mode to difficulty selection
lda #2
sta GAMEMODE
; setting default game type (normal mode/green)
lda #0
sta SELECTEDGAMETYPE
lda #5
sta SELECTEDGAMETYPEOFFSET
; setting sound iterator to "not playing sound"
lda #0
sta SOUNDITERATOR
; clearing registers
lda #0
ldx #0
ldy #0
;---------------------------------------------------
;
; Game Loop
;
;---------------------------------------------------
Main:
jsr VerticalSync
jsr VerticalBlank
jsr Kernel
jsr OverScan
jmp Main
VerticalSync:
lda #2
ldx #49
sta WSYNC
sta VSYNC
stx TIM64T ; set timer to go off in 41 scanlines (49 * 64) / 76
sta WSYNC ; Wait for Sync - halts CPU until end of 1st scanline of VSYNC
sta WSYNC
lda #0
sta WSYNC
sta VSYNC
rts
; game logic can resides here
VerticalBlank:
; reseting loop row
lda #0
sta LOOPROW
; reseting render offset for
lda #0
sta LEVELOFFSET
; checking if game is over
lda GAMEMODE
cmp #1
beq lostgame
; checking is game mode is in "level selection" mode
cmp #2
beq choosegamemode
jsr SetDirection ; poll user input
jsr GenerateRandom ; generate random number
; checking if collision should be checked this frame or not
lda RENDERCOUNTER
cmp RENDERSPEED
bne dontrender
jsr GenerateRandom ; generate random number
jsr CheckCollision
; reseting render counter after collision is checked for this frame
lda #0
sta RENDERCOUNTER
jmp lostgame
dontrender:
inc RENDERCOUNTER ; increment render counter until it meets the RENDERSPEED threshold
choosegamemode:
jsr ProcessSwitches
lostgame:
rts
Kernel:
sta WSYNC
lda INTIM ; check the timer
bne Kernel ; branch if timer is not equal to 0
; turn on the display
sta VBLANK ; Accumulator D1=0, turns off Vertical Blank signal (image output on)
; draw the screen
; 192 scanlines for the game (NTSC)
KernelLoop:
; set background color
lda #0
sta COLUBK
; set playfield color
lda #$0a
sta COLUPF
; checking is game mode is in "level selection" mode
lda #2
cmp GAMEMODE
bne top20init
; iterates 20 times, a header for the screen containing the level selected
; this block of code is executed if the gamemode is 2
ldx #20 ; 20 iterations
ldy #0
top20title:
lda #0
sta PF2
sta WSYNC
lda #0
sta PF0
sty HEADERITERATIONSTORE
ldy LEVELOFFSET
lda levelletter,y
sta PF2
ldy SELECTEDGAMETYPEOFFSET
lda digits,y
sta PF0
ldy HEADERITERATIONSTORE
iny
cpy #4
bne continuetop20title
inc LEVELOFFSET
inc SELECTEDGAMETYPEOFFSET
ldy #0
continuetop20title:
dex
bne top20title
jmp clearPF
; iterates 20 times, a header for the screen containing the score
; this block of code is executed if the gamemode is either 0 or 1
top20init:
ldx #20 ; 20 iterations
ldy #0
top20:
lda #0
sta PF2
sta WSYNC
lda #0
sta PF0
sty HEADERITERATIONSTORE
; left digit
ldy LEFTDIGITOFFSET
lda digits,y
sta PF2
; right digit
ldy RIGHTDIGITOFFSET
lda digits,y
sta PF0
ldy HEADERITERATIONSTORE
iny
cpy #4
bne continuetop20
inc LEFTDIGITOFFSET
inc RIGHTDIGITOFFSET
ldy #0
continuetop20:
dex
bne top20
clearPF:
; clearing playfield from score display section
lda #0
sta PF2
sta PF1
sta PF0
; sets the appropriate playfield color according to the gametype selected
; and also waits 4 scanlines + one extra scanline outside of the main loop
ldx #4
top4plus1:
sta WSYNC
; set playfield color
ldy SELECTEDGAMETYPE
lda snakecolors,y
sta COLUPF
dex
bne top4plus1
; set background color
lda #0
sta COLUBK
sta WSYNC ; just once
; iterates over 160 scanlines, displaying the entire playfield
; also wastes two scanlines at the end
middle160plus3:
loop10: ; loops 10 times, representing each of the 10 rows of the playfield
ldx #16 ; loop 16 times per "playfield row", making square-ish "pixels"
ldy LOOPROW
middlerow:
sta WSYNC ; performed 160 times total
; draw snake playfield
; columns 1-4
lda ROWS,y
and #%11110000
tay
lda leftPF2,y
sta PF2
ldy LOOPROW
; columns 5-6
lda ROWS,y
and #%00001100
tay
lda rightPF0,y
sta PF0
ldy LOOPROW
; columns 7-8
lda ROWS,y
and #%00000011
tay
lda rightPF1,y
sta PF1
; reseting PF2
lda #0
sta PF2
sta PF0
sta PF1
ldy LOOPROW
dex
bne middlerow
; loop back to the top after the 16 pixels of a row have been displayed
inc LOOPROW
cpy #$09
bne loop10
; draw snake playfield ended
finalizedraw:
sta WSYNC ; wasting 1
; clearing the playfield
lda #0
sta PF0
sta PF1
sta PF2
lda #0
sta COLUBK
; wasting 2
sta WSYNC
sta WSYNC
; setting background color for bottom56
lda #$0a
sta COLUBK
; iterates 4 times, displaying a bottom bar for the playfield
ldx #4
bottom4:
sta WSYNC
dex
bne bottom4
rts
; drawing 192 scanlines completed
OverScan:
sta WSYNC
lda #2
sta VBLANK
lda #32 ; set timer for 27 scanlines, 32 = ((27 * 76) / 64)
sta TIM64T ; set timer to go off in 27 scanlines
; game logic can go here
jsr ProcessSwitches
jsr PlaceAppleInPlayfield
jsr PrepScore
jsr PrepSelectedGame
jsr ProcessSound
jsr GenerateRandom
OSwait:
sta WSYNC ; Wait for SYNC (halts CPU until end of scanline)
lda INTIM ; Check the timer
bne OSwait ; loop back if the timer has not elapsed all of the waiting time
rts
;---------------------------------------------------
;
; Sound Subroutines
;
;---------------------------------------------------
EatAppleSound:
lda #1
sta AUDC0 ; channel
lda #8
sta AUDF0 ; frequency
lda #3
sta AUDV0 ; volume
lda #5
sta SOUNDITERATOR
rts
SelectGameSound:
lda #1
sta AUDC0 ; channel
lda #15
sta AUDF0 ; frequency
lda #3
sta AUDV0 ; volume
lda #5
sta SOUNDITERATOR
rts
ProcessSound:
lda SOUNDITERATOR
beq turnsoundoff
dec SOUNDITERATOR
rts
turnsoundoff:
lda #0
sta AUDV0
rts
;---------------------------------------------------
;
; General Purpose Subroutines
;
;---------------------------------------------------
; called when the user presses the reset switch on the console
UserReset:
; init rows of the 10 x 8 playfield
ldx #10; 10 rows
initrowsreset:
dex
lda #%11111111 ; bits 0-7 all enabled
sta ROWS,x
cpx #0
bne initrowsreset
; setting up starting position of snake
; NOTE: 0 = snake in this square
; using row 6 of the playfield
lda #%11100111
sta ROWS + 5
; init snake length
lda #2
sta SNAKELENGTH
; set game speed
ldx SELECTEDGAMETYPE
lda snakespeed,x
sta RENDERSPEED
; init score
lda #0
sta SCORE
; init snake head location
lda #$2c
sta SNAKEHEADLOCATION
; init snake tail location
lda #$a8
sta SNAKEOLDTAILPOINTER
; init apple location
lda #$ff
sta APPLELOCATION
; generate new random number
jsr GenerateRandom
; init snake head direction
lda #%01111111 ; right
sta HEADDIRECTION
; init head history
lda #$2a
sta HEADHISTORY
lda #$2b
sta HEADHISTORY + 1
lda #$2c
sta HEADHISTORY + 2
; init head history pointer
lda #$aa ; a8 being the start of the history + 2 (assuming the snake is 2 long at the start)
sta HEADHISTORYPOINTER
; setting render counter
lda #0
sta RENDERCOUNTER
; setting score color
lda #$0a
sta COLUPF
; setting display table offsets to 0
lda #0
sta LEFTDIGITOFFSET
sta RIGHTDIGITOFFSET
sta LEVELOFFSET
sta SELECTEDGAMETYPEOFFSET
rts
ProcessSwitches:
lda SWCHB ; load in the state of the switches
lsr ; D0 is now in C
bcs notreset ; if D0 was on, the RESET switch was not held
jsr UserReset ; prep for new game
lda #0
sta GAMEMODE
rts
notreset:
lsr ; carry flag
bcs NotSelect
; selection mode engaged
; set game mode to SELECT mode (#2)
lda #2
sta GAMEMODE
; checking if user is allowed to make a selection
; this is done so the selection won't fall through and
; refresh selection at 60Hz
lda #0
cmp DEBOUNCE
bne selected
; reset game type selection if the next game type is out of bounds
lda #2
cmp SELECTEDGAMETYPE
beq resetselectedgametype
; setting debounce to prevent fall through
lda #1
sta DEBOUNCE
jsr SelectGameSound
inc SELECTEDGAMETYPE
jmp selected
resetselectedgametype:
; resetting selected gametype to 0
lda #0
sta SELECTEDGAMETYPE
jsr SelectGameSound
; setting debounce to prevent fall through
lda #1
sta DEBOUNCE
jmp selected
NotSelect:
; if the user has let go of the select lever
; they are allowed to select again
lda #0
sta DEBOUNCE
selected:
rts
; generate random number
GenerateRandom:
lda RANDOM
lsr
bcc noeor
eor #$8e
noeor:
sta RANDOM
rts
SetDirection:
ldx SWCHA
cpx #%11111111 ; no direction selected
beq nodirection
cpx #%11101111
beq normaldirection
cpx #%11011111
beq normaldirection
cpx #%10111111
beq normaldirection
cpx #%01111111
beq normaldirection
jmp nodirection
normaldirection:
; check if direction is not opposite to current direction
lda STOREDDIRECTION
cmp #%11101111 ; up
beq isdown
cmp #%01111111 ; right
beq isleft
cmp #%11011111 ; down
beq isup
cmp #%10111111 ; left
beq isright
jmp nodirection
isdown:
cpx #%11011111 ; down
beq nodirection
jmp storedirection
isleft:
cpx #%10111111 ; left
beq nodirection
jmp storedirection
isright:
cpx #%01111111 ; right
beq nodirection
jmp storedirection
isup:
cpx #%11101111 ; up
beq nodirection
jmp storedirection
storedirection:
stx HEADDIRECTION
nodirection:
rts
CheckCollision:
ldx HEADDIRECTION
cpx #%11101111 ; up
bne checkcollisioncontinue1
jmp up
checkcollisioncontinue1:
cpx #%01111111 ; right
bne checkcollisioncontinue2
jmp right
checkcollisioncontinue2:
cpx #%11011111 ; down
bne checkcollisioncontinue3
jmp down
checkcollisioncontinue3:
; left is the only option left
jmp left
up:
lda SNAKEHEADLOCATION
sec
sbc #$08
clc
bpl applecollisionup
jmp BadCollision ; if value of subtraction is negative
applecollisionup:
; is collision with an apple?
lda SNAKEHEADLOCATION
sec
sbc APPLELOCATION
sec
sbc #$08
bne collisionresumeup
clc
; increment score
sed
lda SCORE
clc
adc $01
sta SCORE
cld
; increment snake length
inc SNAKELENGTH
; setting value of apple location to $ff to know that we need to put apple in new location
clc ; clears carry
lda #$ff
sta APPLELOCATION
jsr GetRowAndColumnForHead
jsr MoveHeadHistoryPointerForward
jsr MoveSnakeUp
jsr EatAppleSound
rts
collisionresumeup:
clc ; clears carry
; get row and column of the snake head
; check if the square above (up) the snake head is its body (bit = 0)
jsr GetRowAndColumnForHead
ldx ROWNUMBER
dex
lda ROWS,x
and COLUMNMASK
cmp #0
bne rowsupreturn
jmp BadCollision
rowsupreturn:
jsr MoveHeadHistoryPointerForward
jsr IncrementOldSnakeTailPointer
jsr MoveSnakeUp
jsr GetRowAndColumnForTail
jsr removeoldsnaketail
rts
right:
lda #$07
checkright:
cmp SNAKEHEADLOCATION
bne checkrightcontinue
jmp BadCollision
checkrightcontinue:
clc
adc #$08
cmp #$57
bne checkright
; is collision with an apple?
lda SNAKEHEADLOCATION
clc
adc #$01
cmp APPLELOCATION
bne collisionresumeright
; increment score
sed
lda SCORE
clc
adc $01
sta SCORE
cld
; increment snake length
inc SNAKELENGTH
; setting value of apple location to $ff to know that we need to put apple in new location
lda #$ff
sta APPLELOCATION
jsr GetRowAndColumnForHead
jsr MoveHeadHistoryPointerForward
jsr MoveSnakeRight
jsr EatAppleSound
rts
collisionresumeright:
; get row and column of the snake head
; check if the square right of the snake head is its body (bit = 0)
jsr GetRowAndColumnForHead
ldx ROWNUMBER
lda ROWS,x
asl
and COLUMNMASK
cmp #0
bne rowsrightreturn
jmp BadCollision
rowsrightreturn:
jsr MoveHeadHistoryPointerForward
jsr IncrementOldSnakeTailPointer
jsr MoveSnakeRight
jsr GetRowAndColumnForTail
jsr removeoldsnaketail
rts
down:
lda SNAKEHEADLOCATION
clc
adc #$08
cmp #$50
bcc applecollisiondown
jmp BadCollision ; if $50 is greater than the a register
applecollisiondown:
; is collision with an apple?
lda APPLELOCATION
sec
sbc SNAKEHEADLOCATION
sec
sbc #$08
bne collisionresumedown
clc
; increment score
sed
lda SCORE
clc
adc $01
sta SCORE
cld
; increment snake length
inc SNAKELENGTH
; setting value of apple location to $ff to know that we need to put apple in new location
clc ; clear carry
lda #$ff
sta APPLELOCATION
jsr GetRowAndColumnForHead
jsr MoveHeadHistoryPointerForward
jsr MoveSnakeDown
jsr EatAppleSound
rts
collisionresumedown:
clc ; clear carry
; get row and column of the snake head
; check if the square below (down) the snake head is its body (bit = 0)
jsr GetRowAndColumnForHead
ldx ROWNUMBER
inx
lda ROWS,x
and COLUMNMASK
cmp #0
bne rowsdownreturn
jmp BadCollision
rowsdownreturn:
jsr MoveHeadHistoryPointerForward
jsr IncrementOldSnakeTailPointer
jsr MoveSnakeDown
jsr GetRowAndColumnForTail
jsr removeoldsnaketail
rts
left:
lda #$00
checkleft:
cmp SNAKEHEADLOCATION
bne checkleftcontinue
jmp BadCollision
checkleftcontinue:
clc
adc #$08
cmp #$50
bne checkleft
; is collision with an apple?
lda SNAKEHEADLOCATION
sec
sbc #$01
clc
cmp APPLELOCATION
bne collisionresumeleft
; increment score
sed
lda SCORE
clc
adc $01
sta SCORE
cld
; increment snake length
inc SNAKELENGTH
; setting value of apple location to $ff to know that we need to put apple in new location
lda #$ff
sta APPLELOCATION
jsr GetRowAndColumnForHead
jsr MoveHeadHistoryPointerForward
jsr MoveSnakeLeft
jsr EatAppleSound
rts
collisionresumeleft:
; get row and column of the snake head
; check if the square right of the snake head is its body (bit = 0)
jsr GetRowAndColumnForHead
ldx ROWNUMBER
lda ROWS,x
lsr
and COLUMNMASK
cmp #0
bne rowsleftreturn
jmp BadCollision
rowsleftreturn:
jsr MoveHeadHistoryPointerForward
jsr IncrementOldSnakeTailPointer
jsr MoveSnakeLeft
jsr GetRowAndColumnForTail
jsr removeoldsnaketail
rts
BadCollision:
; set gamemode to #1, meaning game over
lda #$01
sta GAMEMODE
clc
rts
GetRowAndColumnForHead:
ldx #0
lda SNAKEHEADLOCATION
sec
rowloop:
sbc #$08
bmi columnloop
inx
jmp rowloop
columnloop:
storerow:
stx ROWNUMBER
sec
lda endofrownumber,x ; rownumber already in x register
sbc SNAKEHEADLOCATION
clc
tax
lda columnmasklist,x
sta COLUMNMASK
lda removetailmasklist,x
sta COLUMNMASKINVERTED