Code your own path-following Lemmings in Python | Wireframe issue 17
Learn how to create your own obedient lemmings that follow any path put in front of them. Raspberry Pi’s own Rik Cross explains how.
Lemmings is a puzzle-platformer, created at DMA Design, and first became available for the Amiga in 1991. The aim is to guide a number of small lemming sprites to safety, navigating traps and difficult terrain along the way. Left to their own devices, the lemmings will simply follow the path in front of them, but additional ‘special powers’ given to lemmings allow them to (among other things) dig, climb, build, and block in order to create a path to freedom (or to the next level, anyway).
Code your own lemmings
I’ll show you a simple way (using Python and Pygame) in which lemmings can be made to follow the terrain in front of them. The first step is to store the level’s terrain information, which I’ve achieved by using a two-dimensional list to store the colour of each pixel in the background ‘level’ image. In my example, I’ve used the ‘Lemcraft’ tileset by Matt Hackett (of Lost Decade Games) – taken from opengameart.org – and used the Tiled software to stitch the tiles together into a level.
The algorithm we then use can be summarised as follows: check the pixels immediately below a lemming. If the colour of those pixels is the same as the background colour, then the lemming is falling. In this case, move the lemming down by one pixel on the y-axis. If the lemming isn’t falling, then it’s walking. In this case, we need to see whether there is a non-ground, background-coloured pixel in front of the lemming for it to move onto.
If a pixel is found in front of the lemming (determined by its direction) that is low enough to get to (i.e. lower than its
climbheight), then the lemming moves forward on the x-axis by one pixel, and upwards on the y-axis to the new ground level. However, if no suitable ground is found to move onto, then the lemming reverses its direction.
The algorithm is stored as a
lemming’s update() method, which is executed for each lemming, each frame of the game. The sample
level.png file can be edited, or swapped for another image altogether. If using a different image, just remember to update the level’s
BACKGROUND_COLOUR in your code, stored as a (red, green, blue, alpha) tuple. You may also need to increase your lemming
climbheight if you want them to be able to navigate a climb of more than four pixels.
There are other things you can do to make a full Lemmings clone. You could try replacing the yellow-rectangle lemmings in my example with pixel-art sprites with their own walk cycle animation (see my article in issue #14), or you could give your lemmings some of the special powers they’ll need to get to safety, achieved by creating flags that determine how lemmings interact with the terrain around them.
Get your copy of Wireframe issue 17
You can read more features like this one in Wireframe issue 17, available now at Tesco, WHSmith, and all good independent UK newsagents.
Or you can buy Wireframe directly from Raspberry Pi Press — delivery is available worldwide. And if you’d like a handy digital version of the magazine, you can also download issue 17 for free in PDF format.
Make sure to follow Wireframe on Twitter and Facebook for updates and exclusive offers and giveaways. Subscribe on the Wireframe website to save up to 49% compared to newsstand pricing!
I guess there’s an error in this logic:
“…check the pixels immediately below a lemming. If the colour of those pixels isn’t the same as the background colour, then the lemming is falling.”
If the colour of the pixels below the lemming is different from the background colour, the the lemming is standing on some surface, therefore it is not falling.
I was going to say the same thing. It kind of threw me off when I read it.
I was going to say the same thi…. ohhhh, hang on a minute!! ;-)
You don’t need to throw them off, they happily fall off all by themselves ;-)
3… 2… 1… Oh no! *POP*
Ryan Lambie — post author
Hello! You’re quite right – apologies for the error. We’ve corrected this now.
It seems to be failing on PIL import.
Where/How can i get this?
Comments are closed