   1 REM ***********************
   2 REM *    baSnake 3.0.1    *
   3 REM *  ZX Spectrum Next   *
   4 REM * Marco Varesio 2019  *
   5 REM ***********************
#autostart   
   7 LET TRB = 7 : RUN AT 2
   8 GO SUB 5000: GO SUB 2800
   9 GO SUB 900
  10 REM *** MAIN MENU ***
  11 LET HI = 0
  12 GO SUB 1500
  15 RANDOMIZE 
  20 DIM M(22, 32): DIM S(704, 2)
  22 BRIGHT 0: BORDER 5: PAPER 4: CLS 
  25 GO SUB 1600
  30 LET SC = 0
  35 LET T = 1: LET H = 1
  40 LET S(1, 1) = 11: LET S(1, 2) = 8
  60 LET Y = S(1, 1): LET X = S(1, 2)
  70 LET D = 4: LET G = 4
  80 LET FX = -1: LET FY = -1: LET FD = 0
  81 LET MX = -1: LET MY = -1: LET MR = 0
  90 PRINT #1;  AT 1, 0; "Score: "; SC, "High: "; HI
 100 REM *** MAIN GAME LOOP ***
 110 LET F = PEEK 23672: IF G = 0  THEN GO SUB 1000
 250 IF D = 1  THEN LET Y = Y - 1 : GO TO 270
 255 IF D = 2  THEN LET Y = Y + 1 : GO TO 270
 260 IF D = 3  THEN LET X = X - 1 : GO TO 270
 265 IF D = 4  THEN LET X = X + 1 : GO TO 270
 270 IF Y < 0  THEN LET Y = 21 : GO TO 290
 275 IF Y > 21  THEN LET Y = 0 : GO TO 290
 280 IF X < 0  THEN LET X = 31 : GO TO 290
 285 IF X > 31  THEN LET X = 0 : GO TO 290
 290 GO SUB 2500
 300 IF X <>FX  OR Y <>FY  THEN GO TO 400
 310 LET G = G + 3: SPRITE 0, 0, 0, 0, 0: LET FX = -1: REM HIDE APPLE
 320 IF FD > 16 THEN LET SC = SC + 5: ELSE IF FD > 8 THEN LET SC = SC + 10: ELSE LET SC = SC + 1
 330 PRINT #1;  AT 1, 0; "Score: "; SC, "High: "; HI
 400 INK 0
 405 REM *** Draw new snake head ***
 410 PRINT AT S(H, 1), S(H, 2);  CHR$ 144
 420 LET H = H + 1: IF H = 705  THEN LET H = 1
 430 LET S(H, 1) = Y: LET S(H, 2) = X
 440 PRINT AT S(H, 1), S(H, 2);  CHR$ 145
 450 IF G > 0  THEN GO TO 490 
 452 REM *** Delete snake tail ***
 455 LET TY = S(T, 1): LET TX = S(T, 2)
 460 PRINT AT TY, TX; " "
 465 LET M(TY + 1, TX + 1) = 0
 470 LET T = T + 1: IF T = 705  THEN LET T = 1
 475 REM BEEP .008, -20
 480 GO TO 500
 490 LET G = G - 1
 495 IF G = 2  THEN BEEP .04, -10 
 496 IF G = 1  THEN BEEP .04, -20 
 497 IF G = 0  THEN BEEP .04, -5
 500 LET MV = M(Y+1, X+1)
 501 IF MV = 1  THEN LET M$ = " You bit yourself ": GO TO 700
 502 IF MV = 2  THEN LET M$ = " You hit the wall ": GO TO 700
 503 IF MV = 3  THEN LET M$ = " The mongoose bit you ": GO TO 700
 510 LET M(Y+1, X+1) = 1
 590 GO SUB 4000
 600 GO TO 100
 700 REM *** GAME OVER ***
 701 FOR I=0 TO 1: SPRITE I, 0, 0, 0, 0: NEXT I: REM HIDE APPLE AND MONGOOSE
 705 INK 3
 710 LET I=H
 715 REPEAT
 717  LET C=144: IF I = H THEN LET C=147
 720  IF I = H OR S(I, 1) <> S(H, 1) OR S(I, 2) <> S(H, 2) THEN PRINT AT S(I, 1), S(I, 2); CHR$ C
 725  WHILE I <> T
 730  LET I=I-1
 735  IF I = 0 THEN LET I = 704
 737  PAUSE 1
 740 REPEAT UNTIL 0
 810 PAPER 1
 811 FOR I = 6  TO 15
 812 PRINT AT I, 0; "                                "
 813 NEXT I
 814 BORDER 6: PAPER 2: INK 7
 815 PRINT AT 9, (32 -  LEN (M$)) / 2; M$
 820 INK 6: FLASH 1
 821 PRINT AT 7, 10; " GAME OVER! "
 825 FLASH 0: PAPER 5: INK 1
 830 IF SC <=HI  THEN GO TO 850
 840 LET HI = SC
 841 LET M$ = " New high score : "+  STR$ (HI) + " "
 845 PRINT AT 11,  INT ((32- LEN (M$))/2); M$
 850 PAPER 1: INK 7
 870 FOR I = 15  TO -30  STEP -2
 871 BEEP .05, I
 872 NEXT I
 875 PRINT AT 13, 1; "PRESS "; : INVERSE 1 : PRINT "M"; : INVERSE 0 : PRINT " TO RETURN TO MAIN MENU"
 876 PRINT AT 14, 1; "OR ANY OTHER KEY TO PLAY AGAIN"
 880 LET K$ =  INKEY$ 
 885 IF K$ = "" THEN GO TO 880
 890 IF K$ = "M" OR K$ = "m" THEN GO TO 10
 895 GO TO 15
 900 REM *** UDGs ***
 901 RESTORE 903
 902 FOR I = 0  TO 31: READ L: POKE 65368+I, L: NEXT I
 903 DATA 60, 66, 129, 129, 129, 129, 66, 60: REM SNAKE BODY 144
 904 DATA 60, 66, 165, 129, 165, 153, 66, 60: REM SNAKE HEAD 145
 905 DATA 4, 4, 4, 255, 64, 64, 64, 255: REM WALL 146
 906 DATA 60, 66, 165, 129, 129, 153, 66, 60: REM SNAKE SAD 147
 910 RETURN 
1000 REM *** APPLE ROUTINE ***
1001 REM APPLE NOT YET FALLEN 
1005 IF FX <>-1  THEN GO TO 1100
1010 LET FX =  INT ( RND *32): LET FY =  INT ( RND *22): LET FD = 40+ INT ( RND *30)
1015 IF M(FY + 1, FX + 1) <>0  THEN LET FX = -1 : LET FD = 0: RETURN 
1030 SPRITE 0, FX*8+32, FY*8+32, 0, 1: REM RED APPLE
1040 RETURN 
1100 REM APPLE ALREADY FALLEN
1105 LET FD = FD - 1
1110 IF FD = 8  THEN SPRITE 0, FX*8+32, FY*8+32, 2, 1 : RETURN : REM ROTTEN APPLE
1111 IF FD = 16  THEN SPRITE 0, FX*8+32, FY*8+32, 1, 1 : RETURN : REM YELLOW APPLE
1115 IF FD = 0  THEN SPRITE 0, 0, 0, 0, 0: LET FX = -1: REM HIDE APPLE
1130 RETURN 
1500 BORDER 0: PAPER 0: CLS 
1505 FOR I=0 TO 3: SPRITE I+2,120+16*I,32,I+4,1: NEXT I
1506 INK 7: PRINT AT 1,26; "v. 3.0": INK 5
1507 PRINT "Guide the snake "; : INK 4: PRINT CHR$ 145;  CHR$ 144;  CHR$ 144;  CHR$ 144; : INK 5: PRINT " through the"
1508 PRINT "garden, eating the apples   that": SPRITE 0,240,56,0,1
1509 PRINT "fall from the tree,  before they"
1510 PRINT "rot. Avoid the walls "; : PAPER 2: INK 6: PRINT CHR$ 146;  CHR$ 146;  CHR$ 146; : PAPER 0: INK 5: PRINT " and the"
1511 PRINT "mongoose   .": SPRITE 1, 104, 80, 3, 1
1512 PRINT 
1513 INK 7:PRINT "There are 8 different gardens:"
1514 INK 6: PRINT "press key from "; : INVERSE 1 : PRINT "1"; 
1515 INVERSE 0: PRINT " to "; : INVERSE 1: PRINT "8";: INVERSE 0
1516 PRINT " to choose."
1517 PRINT 
1518 GO SUB 3050
1519 INK 6: PRINT "Press "; : INVERSE 1: PRINT "S"; : INVERSE 0 : PRINT " to change speed."
1520 INK 7: PRINT : PRINT "Snake controls:": INK 6: INVERSE 1
1521 PRINT "Q"; : INVERSE 0: PRINT " OR "; : INVERSE 1: PRINT "UP"
1522 PRINT "A"; : INVERSE 0: PRINT " OR "; : INVERSE 1: PRINT "DOWN"
1523 PRINT "O"; : INVERSE 0: PRINT " OR "; : INVERSE 1: PRINT "LEFT"
1524 PRINT "P"; : INVERSE 0: PRINT " OR "; : INVERSE 1: PRINT "RIGHT"
1525 INVERSE 0: PRINT 
1527 INK 5: PRINT "Check out my other retro stuff @": INK 7
1528 PRINT "https://retrobits.altervista.org"
1529 PRINT #1;  AT 0, 0; "   https://retrobits.itch.io    "
1530 PRINT #1; "Enjoy!     Marco 'Pulce' Varesio"
1550 LET L$ =  INKEY$ 
1555 IF L$ >= "1" AND L$ <= "8" THEN GO TO 1570 
1557 IF L$ = "S" OR L$ = "s" THEN GO SUB 3100
1560 GO TO 1550
1570 FOR I=0 TO 63: SPRITE I,0,0,0,0: NEXT I 
1580 RETURN
1600 REM *** DRAW SELECTED GARDEN ***
1601 IF L$ = "1" THEN GO TO 1700
1602 IF L$ = "2" THEN RESTORE 2000
1603 IF L$ = "3" THEN RESTORE 2050
1604 IF L$ = "4" THEN RESTORE 2010
1605 IF L$ = "5" THEN RESTORE 2020
1606 IF L$ = "6" THEN RESTORE 2030
1607 IF L$ = "7" THEN RESTORE 2080
1608 IF L$ = "8" THEN RESTORE 2090
1610 PAPER 2: INK 6
1620 READ B
1630 FOR K = 1  TO B
1640 READ MINY: READ MINX: READ MAXY: READ MAXX
1650 FOR I = MINY  TO MAXY
1651 FOR J = MINX  TO MAXX
1660 PRINT AT I, J;  CHR$ 146
1665 LET M(I+1, J+1) = 2
1670 NEXT J
1671 NEXT I
1690 NEXT K
1700 FOR I = -15  TO 15  STEP 2
1701 BEEP .05, I
1702 NEXT I
1750 PAPER 4
1900 RETURN 
2000 DATA 4
2001 DATA 0, 0, 0, 31
2002 DATA 21, 0, 21, 31
2003 DATA 0, 0, 21, 0
2004 DATA 0, 31, 21, 31
2010 DATA 8
2011 DATA 0, 7, 9, 7
2012 DATA 5, 21, 5, 31
2013 DATA 11, 23, 21, 23
2014 DATA 16, 0, 16, 9
2015 DATA 0, 8, 9, 8
2016 DATA 6, 21, 6, 31
2017 DATA 11, 22, 21, 22
2018 DATA 15, 0, 15, 9
2020 DATA 4
2021 DATA 6, 7, 7, 24
2023 DATA 16, 7, 17, 24
2024 DATA 9, 6, 14, 7
2025 DATA 9, 24, 14, 25
2030 DATA 4
2031 DATA 0, 4, 2, 27
2032 DATA 19, 4, 21, 27
2033 DATA 0, 0, 21, 3
2034 DATA 0, 28, 21, 31
2050 DATA 8
2051 DATA 0, 0, 0, 31
2052 DATA 21, 0, 21, 31
2053 DATA 9, 3, 9, 28
2054 DATA 13, 3, 13, 28
2055 DATA 1, 0, 8, 0
2056 DATA 1, 31, 8, 31
2057 DATA 14, 0, 20, 0
2058 DATA 14, 31, 20, 31
2080 DATA 8
2081 DATA 13, 27, 17, 27
2082 DATA 17, 16, 17, 26
2083 DATA 4, 4, 8, 4
2084 DATA 4, 5, 4, 15
2085 DATA 13, 25, 15, 25
2086 DATA 15, 16, 15, 24
2087 DATA 6, 6, 8, 6
2088 DATA 6, 7, 6, 15
2090 DATA 11
2091 DATA 0, 0, 0, 14
2092 DATA 0, 17, 0, 31
2093 DATA 21, 0, 21, 14
2094 DATA 21, 17, 21, 31
2095 DATA 5, 3, 5, 28
2096 DATA 16, 3, 16, 28
2097 DATA 1, 0, 9, 0
2098 DATA 12, 0, 20, 0
2099 DATA 1, 31, 9, 31
2100 DATA 12, 31, 20, 31
2101 DATA 10, 15, 11, 16
2500 REM *** MONGOOSE ROUTINE ***
2503 IF F <> 0 AND F <> 64 AND F <> 128 AND F <> 192 THEN RETURN
2505 IF MX = -1  THEN GO TO 2600
2510 LET MR =  RND 
2515 IF MR > .6  THEN RETURN 
2520 SPRITE 1, 0, 0, 0, 0: REM HIDE MONGOOSE
2525 LET M(MY + 1, MX + 1) = 0
2530 LET MX = -1
2590 RETURN 
2600 LET MR =  RND 
2605 IF MR > .7  THEN RETURN 
2610 LET MX =  INT ( RND *32): LET MY =  INT ( RND *22)
2615 IF MX = S(H, 2)  OR MY = S(H, 1)  THEN LET MX = -1: RETURN 
2620 IF M(MY + 1, MX + 1) <>0  THEN LET MX = -1: RETURN 
2625 SPRITE 1, MX*8+32, MY*8+32, 3, 1: REM DRAW MONGOOSE
2630 LET M(MY + 1, MX + 1) = 3
2700 RETURN 
2800 REM *** WELCOME (LOADING SCREEN & MUSIC INTRO) ***
2805 LAYER OVER 0
2810 BORDER 0: PAPER 0: REM POKE 23739,244
2820 CLS:LAYER 2,1:LOAD "basnake.nxi" LAYER
2822 PAPER 5: INK 26: PRINT AT 23,0;"  ";CHR$ 127;"2017-2019 marco's retrobits  "
2825 PRINT AT 0,0;"baSnake 3.0 for ZX Spectrum NEXT"
2827 RUN AT 0: REM OUT 9275, 7: OUT 9531, 0
2830 LET a$="UX5000T60O5W3#FED#Cbab#C"
2831 LET b$="UX5000T60O3W6Dab#fgdga"
2832 LET c$=""
2833 LET d$=""
2834 LET e$="UX2500T60O3W3Dab#fgdga"
2835 LET f$="UX2500O4W6N3D#FAG#FD#FEDbDAGBAG"
2836 LET g$=""
2837 LET h$=""
2838 LET i$="UX5000T60O5W3D#Cbag#fge"
2840 PLAY a$,b$,c$,d$,e$,f$,g$,h$,i$
2850 RUN AT 2: REM OUT 9275, 7: OUT 9531, 2
2860 PRINT AT 0,0; "   PRESS  ANY  KEY  TO  START   "
2870 PAUSE 0
2880 LAYER 2,0: LAYER 0
2890 RETURN 
3050 REM *** PRINT TURBO MODE ***
3052 INK 7
3055 PRINT AT 11, 0; "Turbo mode: ";
3056 INK 5
3060 IF TRB = 11 THEN PRINT "ANDANTE ": GO TO 3070
3061 IF TRB = 9  THEN PRINT "MODERATO": GO TO 3070
3062 IF TRB = 7  THEN PRINT "ALLEGRO ": GO TO 3070
3063 IF TRB = 5  THEN PRINT "VIVACE  ": GO TO 3070
3064 IF TRB = 3  THEN PRINT "PRESTO  ": GO TO 3070
3070 INK 7
3090 RETURN 
3100 REM *** CHANGE TURBO MODE ***
3105 LET TRB = TRB - 2
3110 IF TRB = 1  THEN LET TRB = 11
3115 GO SUB 3050
3120 PAUSE 300
3130 RETURN 
4000 REM *** INPUT ***
4010 LET DT = D
4100 LET K = CODE(INKEY$) 
4210 IF (K = 11 OR K = 81 OR K = 113)  AND (D = 3 OR D = 4)  THEN LET DT = 1
4220 IF (K = 10 OR K = 65 OR K = 97)  AND (D = 3 OR D = 4)  THEN LET DT = 2
4230 IF (K = 8 OR K = 79 OR K = 111)  AND (D = 1 OR D = 2)  THEN LET DT = 3
4240 IF (K = 9 OR K = 80 OR K = 112)  AND (D = 1 OR D = 2)  THEN LET DT = 4
4280 IF ABS(PEEK 23672 - F) < TRB THEN GO TO 4100
4290 LET D = DT
4300 RETURN
5000 REM *** SPRITES LOAD & INITIALIZATION ***
5001 LOAD "basnake.spr" BANK 12
5010 SPRITE BANK 12
5020 SPRITE CLEAR
5030 SPRITE PRINT 1
5040 SPRITE BORDER 0
5050 RETURN
