SNAKE

by Sam Goodwin

from Popular Computing Weekly vol.1 #16 (5th August 1982)



Learn to eat as much as you can



Sam Goodwin presents some machine code tips and a new game called 'Snake'.





Although lower case lettering often improves programs, it can also lead

to trouble. Some programs will act on the upper case letter 'A' being

pressed, but they will not recognise lower case 'a' as being the same.

To avoid this problem on the ZX Spectrum you can either test both 

conditions:

	100 LET i$=INKEY$

	110 IF i$="A" OR i$="a" THEN

or switch on the CAPS LOCK in the program:

	100 POKE 23658,8: REM CAPS LOCK ON

	110 IF INKEY$="A" THEN

POKE 23658,0 will set CAPS LOCK OFF.



	The one graphics command I miss is PLOT 23,45 TO 67,89. To do

this on the Spectrum you always need to  know where the end point is in

relation to the starting point. This is because the command used for 

drawing lines (DRAW) will act from the last recorded point. The above

instruction would be enterd as PLOT 23,45: DRAW 44,44.



	The following program sketches a fine system of spirals by

subtracting the previous PLOT position from the new one so lines can 

be joined as absolute coordinates:

	10 LET x=23677: LET y=23678

	20 PLOT 128, 88

	30 FOR t=0 TO 377

	40   LET r=SIN (t*3/4)

	50   DRAW SIN t*r*80+128-PEEK x,COS t*r*80+88-PEEK y

	60 NEXT t



Machine Code

All the registers can be used except for the HL' pair. If you are 

going to change their contents during the program it is wise to 

restore their original contents before returning to BASIC. *

	For machine code that requires critical timing, such as 

printer output and sound effects, it may be necessary to disable

the clock and keyboard scans. This is done with the DI instruction

(Disable Interrupt, code 243). The keyboard scan must be Enabled

(code 251) before returning, otherwise the system will hang when a 

keyboard input is required.

	This feature could be used to protect programs from being

copied, as disabling the BREAK key would make it impossible to stop

the program without switching off the computer.



	Instructions can be stored in the User Definable Graphics 

memory area. This would allow the program to be retained after the 

NEW command.



	The following machine code program does a smooth horizontal 

scroll right to left across the middle of the screen:

	ld BC,window size

	ld HL,lowest point

    --> rl (HL)

	dec HL

	djnz --> 

	dec C

	jr NZ --> 

	ret



	The machine code could be entered in the following way:

	10 FOR a=USR "S" TO USR "U"

	20   INPUT b

	30   POKE a,b

	40 NEXT a



	   RUN



	1 8 0 33 255 79 203 22 43 16 251 13 32 248 201 0 0



	(The information has been stored in an area that will

remain unaffected by the instruction NEW.)



	   NEW



	10 FOR a=0 TO 26 STEP 1

	20   PLOT 255,SIN a*30+80+USR USR "S"

	30 NEXT a

	40 CLS: LIST: LIST

	50 LET a=USR USR "S"

	60 GOTO 50



	The above program should produce a gently moving sine

wave that glides across the screen and then display the

program rolling the middle section smoothly from right to left.



	The one bug I have found in the Spectrum is hardly

worth worrying about. In fact, it could prove to be 

beneficial. It appears that when a Ctrl+H (back space) is 

printed at the beginning of a line the previous line loses 

one of its bytes.



	The idea of being able to edit a program while it is

being RUN offers tremendous potential, but somehow I do not 

think it was an intended feature.



	Finally, I have devised a game called 'Snake'. You

are a slowly growing snake, kept in a confined space. The

object of the game is to eat as much of the food as

possible while avoiding the obstacles. When entering the 

program it is important to type in line 50 with exactly 33 

spaces in the PRINT statement. The character printed in 

lines 186 and 230 is a user-definable 'A'.



--- END ---


* Update by Sam's brother Simon, September 2023: in fact
the only register other than HL' (the alternate HL pair)
that must be restored if altered is IY, to 23610. It is
not safe for IY to have any other value unless interrupts
are temporarily disabled, with the instructions Sam gives.
