How do you start writing game programs? Surprisingly, the game often only develops while actually writing the program. This back-to-front method is not suited to larger machines, but it works well for the ZX80. Let’s write a game.
I often start by drawing a board on the screen. Enter:
30 GO SUB 140
40 FOR A=1 TO 5
50 PRINT CHR$(128);
60 FOR B=1 TO 30
70 PRINT CHR$(0);
80 NEXT B
90 PRINT CHR$(128)
100 NEXT A
110 GO SUB 140
120 CLEAR
130 GO TO 180
140 FOR C=1 TO 32
150 PRINT CHR$(128);
160 NEXT C
170 RETURN RUN
The subroutine at 140 draws either the top or bottom lines. I space the lines well apart, in tens; use variables in order, A, B, C; and use the CLEAR instruction where variables are no longer needed. CLEAR reclaims RAM that might be useful later on.
Changing line 40 makes the board larger. Changing lines 60 and 140 make it squarer.
What are the addresses of the current board’s squares? Square addresses in the middle line are given by:
190 LET E=0
210 INPUT F
240 LET E=100+PEEK(16396)+PEEK(16397)*256+F
250 POKE E,50
Line 190 defines a variable E that will hold the value of the actual address. It will probably end up around 17000, as a guess. INPUT F allows the numbers of the squares in the middle row to be in the range 1 to 30. So enter only numbers between 1 & 30 in reply to the prompt when choosing a square in the middle line.
Line 240 is the standard way to find the address of a location in the display file. (E must be previously defined.)
The offset, 100 in this case, goes along the display file 100 locations.
Finally, add in the distance along the line, F.
Line 250 will put an “M” in the chosen square.
It’s important to define no new variables between the address calculation (line 240) and any line in which it is used. If you do, the calculated addresses will be wrong.
Now, at last, the idea of this game is forming. Let the computer choose a square on the middle line and then let the player try to find it. A line like:
180 LET D=RND(30)
will certainly do the choosing for a 4K ROM. And a line like:
260 IF ABS(D-F)>2 THEN GO TO 210
allows the player to win if he gets either of the 2 squares before, the square itself, or the 2 after. (ABS makes any number positive.) So par for the game is a win in 6 tries.
Now we tidy it all up and put in some frills. First, a title. Something like “BOMBER OVERHEAD” might do–to play you try to shoot the invisible bomber out of the sky. Add these lines:
10 PRINT "****** BOMBER OVERHEAD ******"
20 PRINT
Now we must change the offset in line 240. No longer is it 100, but 134 because we added 34 extra characters to the display file before the board:
240 LET E=134+PEEK(16396)+PEEK(16397)*256+F
To add some instructions:
200 PRINT "SHOOT 1-30"
(or something more elaborate if you wish.)
The input should be validated so that you cannot enter numbers with no meaning to this program, so add:
230 IF F<1 OR F>30 THEN GO TO 210
The computer will wait for a valid entry.
Another important feature to add erases your last shot:
220 POKE E,0
Note that on the first run through 0 is poked into location 0 since E is initially set to 0. This can sometimes lead to problems. You can try to POKE into ROM addresses, 0-4095, because nothing will actually happen. But in some programs you might want to initialize E to some other value. For example, initializing E to 16384 (the first RAM address) would be asking for trouble. (Note–see PEEK-POKE article, Jun.81–AZ)
So far our program falls through the loop 210-260 if the target is hit, but goes round and round the loop if the shot misses. We need some lines to show a hit. Try, for example:
270 POKE E,20 (the square)
280 POKE E-33,20 (above)
290 POKE E+33,20 (below)
We have 1 slight problem to overcome if the squares before and after are to be filled on success. I do not like to see the board being overwritten–it’s bad programming. So test for the edge-condition (if the square is at the edge) before POKEing the “before” and “after” squares:
300 IF NOT F=1 THEN POKE E-1,20
310 IF NOT F=30 THEN POKE E+1,20
Let’s add an end message:
320 PRINT
330 PRINT "** A GONER **"
and a restart mechanism:
340 INPUT A$
350 CLS
360 RANDOMISE
370 RUN
Syntactic Sum: -25280 4K
This program is very simple, but it shows good features. POKEing to the display file is easy as long as you don’t break the rules.
Media
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.