Code a homage to Lunar Lander | Wireframe #37
Shoot for the moon in our Python version of the Atari hit, Lunar Lander. Mark Vanstone has the code.
First released in 1979 by Atari, Lunar Lander was based on a concept created a decade earlier. The original 1969 game (actually called Lunar) was a text-based affair that involved controlling a landing module’s thrust to guide it safely down to the lunar surface; a later iteration, Moonlander, created a more visual iteration of the same idea on the DEC VT50 graphics terminal.
Given that it appeared at the height of the late-seventies arcade boom, though, it was Atari’s coin-op that became the most recognisable version of Lunar Lander, arriving just after the tenth anniversary of the Apollo 11 moon landing. Again, the aim of the game was to use rotation and thrust controls to guide your craft, and gently set it down on a suitably flat platform. The game required efficient control of the lander, and extra points were awarded for parking successfully on more challenging areas of the landscape.
The arcade cabinet was originally going to feature a normal joystick, but this was changed to a double stalked up-down lever providing variable levels of thrust. The player had to land the craft against the clock with a finite amount of fuel with the Altitude, Horizontal Speed, and Vertical Speed readouts at the top of the screen as a guide. Four levels of difficulty were built into the game, with adjustments to landing controls and landing areas.
Making the game
To write a game like Lunar Lander with Pygame Zero, we can replace the vector graphics with a nice pre-drawn static background and use that as a collision detection mechanism and altitude meter. If our background is just black where the Lander can fly and a different colour anywhere the landscape is, then we can test pixels using the Pygame function image.get_at() to see if the lander has landed. We can also test a line of pixels from the Lander down the Y-axis until we hit the landscape, which will give us the lander’s altitude.
The rotation controls of the lander are quite simple, as we can capture the left and right arrow keys and increase or decrease the rotation of the lander; however, when thrust is applied (by pressing the up arrow) things get a little more complicated. We need to remember which direction the thrust came from so that the craft will continue to move in that direction even if it is rotated, so we have a direction property attached to our lander object. A little gravity is applied to the position of the lander, and then we just need a little bit of trigonometry to work out the movement of the lander based on its speed and direction of travel.
To judge if the lander has been landed safely or rammed into the lunar surface, we look at the downward speed and angle of the craft as it reaches an altitude of 1. If the speed is sufficiently slow and the angle is near vertical, then we trigger the landed message, and the game ends. If the lander reaches zero altitude without these conditions met, then we register a crash. Other elements that can be added to this sample are things like a limited fuel gauge and variable difficulty levels. You might even try adding the sounds of the rocket booster noise featured on the original arcade game.
The direction of thrust could be done in several ways. In this case, we’ve kept it simple, with one directional value which gradually moves in a new direction when an alternative thrust is applied. You may want to try making an X- and Y-axis direction calculation for thrust so that values are a combination of the two dimensions. You could also add joystick control to provide variable thrust input.
Get your copy of Wireframe issue 36
You can read more features like this one in Wireframe issue 37, available directly from Raspberry Pi Press — we deliver worldwide.
And if you’d like a handy digital version of the magazine, you can also download issue 37 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!
Ooooh, I’d completely forgotten about this, yet it was one of the first games I ever played.
I rewrote ‘lunal lander’ for a programmable Casio calculator, withe 6 memory registers, although it only had 32 programming steps. OK, there were no graphics, but writing a lander game with such constraints, was to me, pretty amazing. I have lost neurons over the years, and have no idea how I managed that at 14 years old.
I see you have made ‘nicer graphics’, but I really loved that ‘vector CRT’ look, which was also a key feature of Asteroids.
Now there was some kind of gunfighter game, in the mid 70s, where cowboys would shoot at each other from across the screen. That was my first ever game. Could anyone give me an idea what it was? Thank you.
Raspberry Pi Staff Ashley Whittaker
Retro gaming gods have spoken and they suggest you’re thinking of…. ‘Gun Fight’
Hi Jon, I think that two cowboys were named Outlaws vs Howitzer (Howitzer for tanks battle) that I played on 800xl
The version I recall was on a DEC GT40 vector graphics device which also used a light pen. Once safely landed the astronaut ordered a big mac.
I played a similar version called Jupiter Lander on the Vic-20! For some reason I didn’t enjoy it because of the way the thrusters worked (opposite of where you’re going), but it was a teaching moment in space travel.
i think it was called el bandido.my fave game is defo, i robot by atari,would love to see a rewrite of that with extra levels!