Coding Cosmic Invaders in BBC Basic
Nate Drake invites you to stave off the alien menace by coding your very own Space Invaders clone in Basic, because it’s 2025!
OUR EXPERT Nate Drake is a tech journalist specialising in cybersecurity and retro gaming. His first experience of video games was the cocktail cabinet version of Space Invaders in his father’s local.
Part four!
Catch up with previous issues –see page 64!
S o far in this series, we have covered how the classic programming language Basic S (Beginners’ All Purpose Symbolic Instruction Code) can still be used today to code games like Mark Buckwell’s Cosmic Invaders.
In the first part, we created code to establish a basic title screen. The next tutorial covered loading elements such as the player ship and bases, and the invaders.
If you followed the steps in the third part and/or ran the sample script COSMIC10.bas, your player ship is now capable of firing missiles. The script also has some rudiments of collision detection in that missiles are capable of destroying invaders and shields on impact.
In this final part, we focus on the code for the alien bombs, related collision detection, and scoring.
As always, we recommend using the b2 BBC Micro emulator, available as a Snap package via the Ubuntu Software Center. Once installed, use Edit > OSRDCH Paste (+Return) to paste any of the code samples listed here from the clipboard. Enter RUN to begin.
Bombs array!
Fortunately, for this coding tutorial we already have a copy of the completed game. Access this via http://bit.ly/cosmicinvaders to paste and run it in b2 or your chosen BBC Basic emulator.
Park your player behind one of the shields and focus on the alien bombs. As you’ll see, these consist of simple # characters that drop down, seemingly at random. As the bombs enter different bands on the screen, they change colour. If they collide with a shield, that portion of the shield is damaged. If they collide with the player’s ship, they lose a life. For efficiency, only the current bottom row of aliens drops bombs.
If you take the time to examine the original CosmicInvaders.bas script, you’ll see that there are several bomb dropping procedures, chief of which is PROC_Bombs:
820 DEF PROC_Bombs
830 LOCAL D% : IF Bomb%
New_Bomb
840 IF Bomb%=0 GOTO 910
850 FOR D%=1 TO MBomb%
860 IF Bombs%(D%,1)<>0 THEN PROC_Look_Bomb
870 NEXT
880 FOR D%=1 TO MBomb%
890 IF Bombs%(D%,1)=23 THEN PRINTTAB(BBombs%
(D%,0),Bombs%(D%,1));” “; : Bombs%(D%,1)=0 : Bombs%=Bombs%-1
900 NEXT
910 ENDPROC
Bombs are only dropped by the bottom-most row of invaders, even if aliens higher in the grid have a clear line of fire.
If you’ve been following this series, you’ll note that D% on line 830 is a local integer. The procedure loops through this from 1 to another variable MBomb%, which governs the maximum number of bombs that can appear on screen at a time.
This is worth examining, because if you look at PROC_Setup_game, you’ll see that MBomb% is calculated dynamically as: MBomb%=Start%+6