Roll Your Own Space Invaders

Author(s): Paul Ruiterman
Publication: ZX-Appeal
Date: January 1986
Pages: 4-5, 12

‘Tis time to present an other short but sweet machine language program for the ZX81.

I call it “Roll your own, -space invaders-” because it gives you the ability to program your own version of this classic, in BASIC, using machine language routines to really speed things up. The machine language program listed here does 4 things.

1) It scans the keyboard and it

2) fires rounds of ammo, it then moves those rounds up to the top of the screen. It also moves the gun left and right, all depending on the keys pressed.

3) It moves everything put anywhere on the screen above line 22 down, one line at a time. Everything except the bullets that is.

4) And finally if anything on the screen is moved from line 21 to line 22 the game is over and you get a BOOM.

What does all this mean? Simple, it means that a short BASIC program (see example) can, in SLOW mode, present you with a ripsnorting game of space invaders and the beauty is YOU control everything.

What do you control? Well, the number of invaders for instance and what they look like. Also the speed with which the invaders come down the screen compared to how fast you can move the gun and fire it. You can even move the gun to a random location on the bottom line, during the game, to make things more interesting

Shall we get down to business?

Machine code listing 4082-40FF
Machine code listing 4082-40FF
4082  [illegible]          JR    4088
4084  [illegible]          JR    40DD
4086  [illegible]          JR    4100
4088  [illegible]    02    CALL  02BB
408B  [illegible]          LD    B,H
408C  [illegible]          LD    [illegible],L
408D  [illegible]          LD    [illegible],C
408E  [illegible]          INC   D
408F  [illegible]          RET   Z
4090  [illegible]    07    CALL  07BD
4093  [illegible]          LD    A,(HL)
4094  [illegible]          CP    3F
4096  [illegible]          JR    Z,40F1
4098  [illegible]          CP    12
409A  [illegible]          JR    NZ,40A1
409C  [illegible]    40    CALL  40F1
409F  [illegible]          JR    40B1
40A1  [illegible]          CP    13
40A3  [illegible]          JR    NZ,40AA
40A5  [illegible]    40    CALL  40F1
40A8  [illegible]          JR    40C2
40AA  [illegible]          CP    33
40AC  [illegible]          JR    Z,40C2
40AE  [illegible]          CP    32
40B0  [illegible]          RET   NZ
40B1  [illegible]    40    CALL  40D3
40B4  [illegible]          INC   HL
40B5  [illegible]          LD    A,83
40B7  [illegible]          CP    (HL)
40B8  [illegible]          RET   NZ
40B9  [illegible]          LD    (HL),80
40BB  [illegible]    40    LD    (407B),HL
40BE  [illegible]          DEC   HL
40BF  [illegible]          LD    (HL),83
40C1  [illegible]          RET
40C2  [illegible]    40    CALL  40D3
40C5  [illegible]          DEC   HL
40C6  [illegible]          LD    A,83
40C8  [illegible]          CP    (HL)
40C9  [illegible]          RET   NZ
40CA  [illegible]          LD    (HL),80
40CC  [illegible]    40    LD    (407B),HL
40CF  [illegible]          INC   HL
40D0  [illegible]          LD    (HL),83
40D2  [illegible]          RET
40D3  [illegible]    40    LD    HL,(407B)
40D6  [illegible]          LD    A,80
40D8  [illegible]          CP    (HL)
40D9  [illegible] 51 41    JP    NZ,4151
40DC  [illegible]          RET
40DD  [illegible] 10 40    LD    HL,(4010)
40E0  [illegible]          LD    A,80
40E2  [illegible]          LD    B,83
40E4  [illegible]          DEC   HL
40E5  [illegible]          DEC   B
40E6  [illegible] 02       JR    NZ,40EA
40E8  [illegible]          RST   08
40E9  [illegible]          RRCA
40EA  [illegible]          CP    (HL)
40EB  [illegible]          JR    NZ,40E4
40ED  [illegible]    40    LD    (407B),HL
40F0  [illegible]          RET
40F1  [illegible]    40    CALL  40D3
40F4  [illegible]    00    LD    DE,0021
40F7  [illegible]          XOR   A
40F8  [illegible]          SBC   HL,DE
40FA  [illegible]          LD    (HL),0B
40FC  [illegible]    41    CALL  415E
40FF  [illegible]          RET

The BASIC Sample program.

1000 SLOW
1005 POKE 16418,0
1010 PRINT AT 22,0;"[row of inverse spaces]"
1015 PRINT AT 22,25*RND;"█"
1020 RAND USR 16516
1030 FOR F=1 TO 30
1055 RAND USR 16514
1057 RAND USR 16514
1063 PRINT AT 0,25*RND;"▒▒"
1065 RAND USR 16514
1067 RAND USR 16514
1070 RAND USR 16518
1075 NEXT F
2000 GOTO 1010

I guess the first thing to discuss is how to get the machine program in your machine. I am going to be short and sweet about this, you have about 260 bytes to enter in the number 1 REM statement so your first order of business is to set up a 260 byte REM statement.

As to how to go about entering the bytes I am going to assume by now you have some sort of a HEX POKE program laying around somewhere. If not have a look in the MARCH ’85 ZXAppeal (the centronics article) or else you could use the method used in “Beethoven”, in the November issue.

The program comes with 3 routines CALLable from BASIC, via the vectors at the top of the listing.

For those of you not into “vectors”, don’t worry, all it means is that, when you RAND USR xxx, you use an address different from where the actual routine lives.

For those of you who know, yes my vectors are set up kind`o weird, it’s what happens when you “hack” instead of program.

Let’s look at the routines one by one.

RAND USR 16516 locates the gun and puts its address in the famous spare bytes at 16512. This gives you the opportunity to place the gun (Char.80h or graphic space) any where on line 22, even DURING the game. Just make sure you call this routine every time you move the gun with a BASIC routine and at least once at the beginning of the game.

4100  AF          XOR   A
4101  32 21 40    LD    (4021),A
4104  2A 10 40    LD    HL,(4010)
4107  11 42 00    LD    DE,0042
410A  ED 52       SBC   HL,DE
410C  11 21 00    LD    DE,0021
410F  CD 25 41    CALL  4125
4112  E5          PUSH  HL
4113  21 21 40    LD    HL,4021
4116  CB 4E       BIT   1,(HL)
4118  E1          POP   HL
4119  20 36       JR    NZ,4151
411B  06 15       LD    B,15
411D  C5          PUSH  BC
411E  CD 25 41    CALL  4125
4121  C1          POP   BC
4122  10 F9       DJNZ  411D
4124  C9          RET
4125  06 20       LD    B,20
4127  2B          DEC   HL
4128  AF          XOR   A
4129  2B          DEC   HL
412A  BE          CP    (HL)
412B  28 21       JR     Z,414E
412D  3E 08       LD    A,08
412F  BE          CP    (HL)
4130  28 1C       JR     Z,414E
4132  7E          LD    A,(HL)
4133  19          ADD   HL,DE
4134  F5          PUSH  AF
4135  3E 08       LD    A,08
4137  BE          CP    (HL)
4138  20 07       JR    NZ,4141
413A  AF          XOR   A
413B  ED 52       SBC   HL,DE
413D  77          LD    (HL),A
413E  F1          POP   AF
413F  18 0D       JR    414E
4141  F1          POP   AF
4142  77          LD    (HL),A
4143  AF          XOR   A
4144  ED 52       SBC   HL,DE
4146  77          LD    (HL),A
4147  E5          PUSH  HL
4148  21 21 40    LD    HL,4021
414B  CB CE       SET   1,(HL)
414D  E1          POP   HL
414E  10 D8       DJNZ  4128
4150  C9          RET
4151  CD 2A 0A    CALL  0A2A
4154  2A 0C 40    LD    HL,(400C)
4157  11 5B 01    LD    DE,015B
415A  19          ADD   HL,DE
415B  36 A7       LD    (HL),A7
415D  23          INC   HL
415E  36 B4       LD    (HL),B4
4160  23          INC   HL
4161  36 B4       LD    (HL),B4
4163  23          INC   HL
4164  36 B2       LD    (HL),B2
4166  E1          POP   HL
4167  CD 46 0F    CALL  0F46
416A  38 FB       JR     C,4167
416C  CF          RST   08
416D  FF          RST   38
416E  06 15       LD    B,15
4170  AF          XOR   A
4171  ED 52       SBC   HL,DE
4173  BE          CP    (HL)
4174  28 04       JR     Z,417A
4176  77          LD    (HL),A
4177  19          ADD   HL,DE
4178  77          LD    (HL),A
4179  C9          RET
417A  36 08       LD    (HL),08
417C  19          ADD   HL,DE
417D  77          LD    (HL),A
417E  AF          XOR   A
417F  ED 52       SBC   HL,DE
4181  10 ED       DJNZ  4170
4183  C9          RET

The routine at USR 16518 moves every character it finds on the screen above line 22 down one line. It ignores the bullets (Char. 11h or ” quotation mark) and when it moves something from line 21 to line 22 it automatically ends the game by going into the “BOOM” routine. If and when the dreaded “BOOM” routine catches up with you the only way out is via the BREAK key.

When you RAND USR 16514 the keyboard is scanned. if no key is pressed the routine returns without any action. Pressing keys, while the keyboard is being scanned, will initiate the following actions.

N moves the gun left and
M moves it right.
Z fires the gun when it is stationary and
shifted N or M fires the gun while it moves.

It must be clear to everybody that the ratio of KBscans to screen moves makes the game more or less difficult. In the sample game the ratio is 4 KBscans to one screen move. Also my aliens all start on line 0 (you could start them anywhere). To keep things interesting I RNDomize the gun placement each time the loop at line 1030 finishes.

I realize some questions may have slipped by me, if so mea culpa and come to the next meet to ask them.

Paul R

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.