Linux Format

Invaders, possibly from space

We’ve resurrecte­d a classic shoot-em-up project from a long lost Linux magazine. All you need to do is save the world.

-

Once upon a time, there was a magazine called

Linux User & Developer (LUD). It was a reasonable title, but not in the same highcalibr­e league as Linux Format. Noting it could never be LXF, LUD did the honourable thing and ordered its parent company to pledge fealty and hand over all assets to Future Towers. So for a while LUD was part of the Future family, and then a spreadshee­t in London cried out in panic and suddenly LUD was no more. (I’m pretty sure none of that really happened – Ed). Anyway in issue 136 of LUD, a young Russell Barnes introduced a great project called Pivaders, a

Space Invaders clone written in about 300 lines of code. Then in the next issue he added some bells and whistles, in particular animations and sound.

It was a great project and thanks to Future Towers’ infinite content funnel we can reproduce it here with impunity. The only problem is LUD136 is from February 2014 and in that era writing code in Python 3 was seen as a little postmodern. In this era, most distros have done away with Python 2 and attempts to install it often lead to problems later. Especially on Raspberry Pi OS if our reader letters are anything to go by. Anyway, we digress. So with as light a touch as possible, we set about updating Russell’s dusty old code. After not too much hassle, we got it into a workable shape and now you can download it from our GitLab:

$ git clone https://gitlab.com/LXFjb/pivaders.git

Here we used GitLab’s import feature, which enables us to keep the commit history and everything from the original GitHub site (https://github.com/russb78/ pivaders). If you look at the commit entitled “Update to Python 3”, you can see all the changes we had to make in order to get it working. But don’t worry about that for now, because it would be more fun to see this project in action. Pivaders uses the Pygame module, so you’ll need to install that first:

$ sudo apt install python3-pygame

Everyone knows how Space Invaders work, but for nostalgia value we’ll quote Lrrr in the 50th episode of Futurama, “Raiders of the Lost Arcade”, who describes the invaders’ strategy: “Increase speed, drop down, and reverse direction!”

Ready, player one?

We can verify Lrrr’s laconic descriptio­n by starting Pivaders, which has been hidden one subdirecto­ry deeper than it needs to be:

$ cd pivaders/pivaders

$ python pivaders.py

You should see an enticing splash screen. Hit Space and you’ll be thrust into the action. Use the cursor keys and space bar to save the world! Then, when you’re done playing, open up the pivaders.py file in Geany (or the editor of your choosing) and we can start seeing how this works. The Pygame module takes a great deal of the headache away from games programmin­g. It provides classes for sprites, controller­s, sound effects and so many other things that would otherwise make coding a game from scratch much less fun.

Turning to the code itself you’ll see near the beginning of the file that a lot of things are defined in block capitals. This is to distinguis­h them as global variables, as opposed to the local variables within functions which can’t be accessed from without. Global variables are ones that are used regularly and that it would be inefficien­t and ugly to have to manually pass around functions. If you want, try changing some of the bullet, block or barrier sizes.

Central to our game is that aliens, barriers and our own ship are destroyed by bullets. Thanks to Pygame’s

sprite detection, wiring this logic into the code is simple. We use Pygame’s Group class (see lines 116-120) to group the different kinds of sprites together so that they can all be checked for collisions without having to do any awkward geometry.

You’ll note the class keyword appears many times throughout the code. The aliens, barriers, bullets, player and even the game itself all have their own classes. This is a hallmark of so-called object oriented programmin­g,

where we define a bunch of classes and then by, instantiat­ing them, make objects. A class can have its own methods (for example the __init__() method is called whenever that class is brought to life). It’s easy to see why classes might be useful for blocks and bullets, because we’re going to need lots. But it turns out they’re equally useful for things that are only going to exist once.

For objects that move in our game, the associated classes all have an update() method. For example, the Ammo class is updated (lines 82-85) by increasing its y co-ordinate according to its speed. If the bullet goes off the top or bottom of the screen, the object is destroyed. Speaking of the Ammo class, it’s worth noting that this is used for both alien missiles and the player’s bullets. Classes can inherit methods from other, parent classes. Indeed, the Ammo class itself extends Pygame’s own Sprite class, which is where all the collision-detecting magic takes place.

Blits and pieces

Pygame makes it easy to use our own assets. The graphics, fonts and sounds used in Pivaders are all open source, but feel free to experiment with alternativ­es. Graphics are dumped on the screen (technicall­y a surface) using the blit() method. Blit (block transfer, sometimes called bit-blit) is the traditiona­l method for copying bitmaps (or parts of bitmaps) from one surface to another. In order for coin-op, bullet hell arcade classics to be so hellish, it was common for them to have their own blitter chip. Robotron:2084 from 1982 included two Blitter chips, allowing for a (then) mindbendin­g 80 objects moving on the screen at once.

It might sound like a dated method, but as long as we only care about two dimensions then blitting is the simplest way to move things around. Behind the scenes Pygame will use your GPU (or fast software techniques if it can’t) to speed up the blits. Considerat­ion is still required though. Every time anything in our game moves, part of the background will be covered up and another part of it revealed. In order that the movement appears smooth we blit the background after each display update (the pygame.display.flip() call that’s on line 266).

We do hope you’ve enjoyed Pivaders, and we do hope you’ve enjoyed this brief foray into the world of Python coding. Of course, Python is not the only fruit, and one may equally well have coded something similar in C (or C++) or x86 assembly. That might make for a faster game, but there would be more code and we’d struggle to explain most of it. PHP, JavaScript and Perl all have reputation­s as ‘web languages’, but they’re all capable of manifestin­g a Space Invaders clone. In our Raspberry Pi temperatur­e monitoring tutorial (see page 50), we’ll look at combining Python with Bash scripting, so that we can use features of the shell (for example. Sed, Grep and Awk) to automate, analyse and hopefully ameliorate.

 ?? ?? The verdant intro for our action-packed space shooter.
The verdant intro for our action-packed space shooter.
 ?? ?? On this occasion, our attempts to save the world were not successful…
On this occasion, our attempts to save the world were not successful…

Newspapers in English

Newspapers from Australia