Vent your hatred of rocks in our Pygame Zero salute to the 16-bit hit. Mark Vanstone has the code in the brand new edition of Wireframe magazine, available now.
The original rock-blasting arcade shooter was, of course, Asteroids, released by Atari in 1979. But since then, many clones; one notable example was Super Stardust, developed by Finnish developer Bloodhouse and released by Team17 for the Amiga platform in 1994 (for more on that studio’s history, see page 28). The game featured considerably better graphics and sound than Asteroids, and got great reviews. One of the differences between the two is that Asteroids uses vector graphics to draw the screen objects whereas Stardust uses sprites.
If you have a look back at Wireframe issue four, you’ll find a Source Code article describing how to make a spaceship move using thrust, Asteroids style. Rather than go over old ground, this example will cover how to code the asteroids that are broken into pieces by the ship’s lasers. We’ll use animated sprites for the asteroids, much the same as in the Super Stardust version of the game. We’ll need three sizes of asteroids – the ones here are 128 × 128, 64 × 64, and 32 × 32 pixels. The twelve animation frames of each size of asteroids have been produced using Blender 3D, which you can download for free from blender.org.
We set up four asteroids as Actors, make them into a list, and place them in a square around the centre of the screen. We can set them at different angles so that when we move them in the
update() function, they’ll go in different directions. We also add a status flag that will tell us if we need to draw the asteroid or not. If we set a different start frame for the animation of each asteroid, then they won’t look too much like they’re all turning together. When we update each asteroid, we use a bit of trigonometry to move each one in the direction they’re pointing. We also check to see if they’ve gone off the screen and, if so, make them appear on the opposite side of the screen. Each
update() call, we increment the frame being displayed by the asteroid Actor to make it look like it’s rotating as it moves.
Now we have our moving, spinning asteroids, we need a ship to shoot them. We’ll plop the ship in the middle of the screen and use the arrow keys to rotate it. If you want to add thrust to get the ship to move around the screen, have a look back at the issue four Source Code article mentioned above. We’ll use the SPACE bar to fire a laser Actor which we’ll put in a list. If we assign the opposite rotation of the ship to the bullet, then we can move it in the direction it was fired until it hits an asteroid or goes off the screen. In this sample, there’s a cleaning mechanism in the bullet update which filters out all the bullets that are no longer required, dropping them from the list.
To make asteroids break up, we detect when a bullet hits an asteroid actor, and when that happens, it’s removed from the list to be drawn by setting its status to 1. Then we create two more smaller asteroids and set their angle to be at right angles to where the bullet came from. This makes them fly off in opposite directions, then in turn, if they’re shot, we do the same but create the smallest size of asteroid. If the small asteroids are shot, they have their status set to 1 and no more asteroids are created. When all the asteroids have a status of 1, the level’s been cleared. You might want to add some collision detection between the ship and the asteroids, perhaps a scoring system, and moving the ship around the screen, but we’ll leave those for you to add.
Get your copy of Wireframe #64
And if you prefer your magazines in digital form, you can also download Wireframe issue 64 as a free PDF!