Missile Command
Set up a game development system
DEVELOPMENT
David Bolton shows you how to set up a Raspberry Pi as a game development machine with a game controller.
OUR EXPERT
David Bolton has been programming games on Raspberry Pis for several years and has nearly finished writing an ebook about learning to program games in C.
QUICK TIP
Note that the responsiveness of a Raspberry Pi is affected by the quality of SD card. This can be quite confusing but the A-rated SD cards, such as A2, are among the fastest. You don’t need the likes of V90, which are better suited for video cameras. A faster SD card means compiles are faster and programs load more.
This is very much a learning process. Once you’ve created your first game, it gets a lot easier to do your second and third. We’re creating a simplified version of Atari’s classic arcade game Missile Command.
You have to defend six cities from incoming missiles with your batteries of ABMs (anti-ballistic missiles). Using the mouse, you click a point to intercept a missile and fire an ABM. Once the ABM reaches that point, it explodes, and if the explosion hits the incoming missile, it is destroyed.
As it stands, the game could be expanded on. Here are some ideas of how you could enhance it. For instance, you might give it difficulty levels. Maybe when you start the game the first level only has five missiles. After those are fired, you’re on level two and have more missiles. Also, you could increase the difficulty by making the missiles faster. You could have a difficulty multiplier that goes from 1.0 to 2.5 by 0.1 per level. Multiply xvel and yvel by this and your missiles will move faster.
Another way to make the game more difficult is by reducing the time between missiles being fired at higher levels. A high score table would be nice, too, with score, initials and date saved out to a text file and reloaded each time the game is run.
Going around in circles
Before you write any code, you need to figure out whether the game can work on your hardware. Arcade games have hardware assistance for sprites and fast graphics. The Raspberry Pi 4B has a GPU, but knowing what it is doesn’t really tell you what it can do.
The main game mechanic used is a rapidly expanding explosion and a little experimentation discovered that drawing the pixels as concentric circles with a radius from 2 to 300 took about 0.028 seconds. Drawing everything in a frame should take under 1/60 of a second (0.0167 seconds) so 0.028 is far too slow. The code for DrawCircle() has been left in missilecommand.c for you to look at, but it isn’t used in the game.