A downloadable game

Eat as much food as you can. Every time you eat, the snake grows. Use the "O" and "P" keys to turn left or right. If you collide with the edges of the screen or with the snake's body, the game ends.

Variables:

P$: String for the game-over sound effect.

S(): Array with the VRAM positions of each segment of the snake's body.

D(): Array with the necessary sum to move the snake in each direction.

H: VRAM position of the head.

F$: Game-over message.

I: General counter.

T: Index of the snake's tail.

L: Length of the snake.

J$: Stores the key that is pressed.

D: Direction of the snake's head (0, 1, 2, 3 > right, down, left, up).

V: Stores the value of a VRAM position to check if the snake collides with

itself, the screen border, or food.


Code explanation:

1 KEYOFF:SCREEN1,,0:COLOR6,11,11:DEFINTA-Z:DEFFNR(X)=INT(RND(1)*X):P$="S14M25T

Initializations. The function R is defined to generate random numbers, avoiding repetitive code in line 10. The variable P$ is assigned a value (done this way to maximize the use of gaps in the lines).

2 DIMS(100):D(0)=1:D(1)=32:D(2)=-1:D(3)=-32:H=6511:L=4:T=1:S(0)=H:F$=CHR$(27)

More initializations. Some variables that start at 0 are not initialized because the interpreter always assigns them that value by default. The array D stores sums that, in VRAM, make the head point to the right, down, left, or right. This simplifies everything (fewer IFs and less code). The variable F$ is assigned.

3 FORI=0TO31:VPOKE(6144+I),6:VPOKE(6880+I),6:NEXT:F$=F$+"Y+#GAME OVER, SCORE =

4 FORI=0TO21:VPOKE(6176+I*32),6:VPOKE(6207+I*32),6:NEXT:GOSUB10:VPOKE8192,203

The screen boundaries are drawn. More initializations.


5 VPOKE(S((T+1)MODL)),32:VPOKE(H),1:J$=INKEY$:IFJ$=""THEN7

Start of the main loop. Refreshes the snake's animation.


6 IFINSTR("Pp",J$)THEND=(D+1)AND3ELSEIFINSTR("Oo",J$)THEND=(D-1)AND3

Checks if "P, p" (right) or "O, o" (left) has been pressed. If so, the snake's direction is rotated. The AND 3 ensures that the values of the variable D are restricted to 0, 1, 2, or 3.

7 H=H+D(D):V=VPEEK(H):IFV=6ORV=1THEN9ELSEIFVPEEK(H)=249THENL=L+1:GOSUB10

Calculates the new position of the snake's head, reads what is in that position, and checks if it is colliding with the borders, the snake, or the food, acting accordingly.

8 IFL=100THEN9ELSET=(T+1)MODL:S(T)=H:FORI=0TO200-L-L:NEXT:GOTO5

Checks if the snake has grown to the limit. Updates the tail pointer, adds the new head position to the array S. Makes a short pause that varies depending on how long the snake is (as the snake grows longer, it moves faster). End of the main loop.

9 PRINTF$;L-4:P$=P$+"150O":PLAYP$+"6F6R6F6",P$+"2F6R6F6":FORI=0TO9525:NEXT:RUN

Displays the game-over message and the score (using escape sequences to position the message, avoiding the use of LOCATE and allowing more code to fit, (lines 2 and 3). Plays a game-over sound, pauses for 5 seconds, and restarts the game.

10 BEEP:I=6210+FNR(28)+32*FNR(20):IFVPEEK(I)<>32THEN10ELSEVPOKEI,249:RETURN

Places new food on the screen.


BASIC PROGRAM:

1 KEYOFF:SCREEN1,,0:COLOR6,11,11:DEFINTA-Z:DEFFNR(X)=INT(RND(1)*X):P$="S14M25T
2 DIMS(100):D(0)=1:D(1)=32:D(2)=-1:D(3)=-32:H=6511:L=4:T=1:S(0)=H:F$=CHR$(27)
3 FORI=0TO31:VPOKE(6144+I),6:VPOKE(6880+I),6:NEXT:F$=F$+"Y+#GAME OVER, SCORE =
4 FORI=0TO21:VPOKE(6176+I*32),6:VPOKE(6207+I*32),6:NEXT:GOSUB10:VPOKE8192,203
5 VPOKE(S((T+1)MODL)),32:VPOKE(H),1:J$=INKEY$:IFJ$=""THEN7
6 IFINSTR("Pp",J$)THEND=(D+1)AND3ELSEIFINSTR("Oo",J$)THEND=(D-1)AND3
7 H=H+D(D):V=VPEEK(H):IFV=6ORV=1THEN9ELSEIFVPEEK(H)=249THENL=L+1:GOSUB10
8 IFL=100THEN9ELSET=(T+1)MODL:S(T)=H:FORI=0TO200-L-L:NEXT:GOTO5
9 PRINTF$;L-4:P$=P$+"150O":PLAYP$+"6F6R6F6",P$+"2F6R6F6":FORI=0TO9525:NEXT:RUN
10 BEEP:I=6210+FNR(28)+32*FNR(20):IFVPEEK(I)<>32THEN10ELSEVPOKEI,249:RETURN
Published 3 days ago
StatusReleased
AuthorBASIC 10Liner
GenreAction
Tags10liner, 8-Bit, basic, MSX

Download

Download
10lines.dsk 720 kB
Download
10lines.txt 3.7 kB

Install instructions

Load the game using: run"10lines.bas

Comments

Log in with itch.io to leave a comment.

very good