How to program a simple snake game for the Raspberry Pi using a coding language called Python
**Please note that this feature contains code that your web browser may try to run.
The promise of the Raspberry Pi sub-£30 microcomputer is that it will usher in a new age of computer education not seen since the home-computing boom of the 1980s. If you followed our introduction to the Raspberry Pi last issue, you will now have a working computer connected to a keyboard, mouse, network and monitor or TV – but where do you go from there?
In this project, we will explain how to program a game for the Pi. Do not worry if you have absolutely no programming experience – we’ll be walking through each step in a simple, plain-English style.
Read more: Raspberry Pi beginner's guide
Make a games controller for the Raspberry Pi
Make a simple LED game
Getting started with Python
The Raspberry Pi Foundation, the organisation responsible for the Raspberry Pi, has chosen a computer programming language called Python to be its primary focus. It is this language, named after Monty Python’s Flying Circus, that we’ll use to write a version of the unending food-munching, tail-lengthening Snake game, popularised by Nokia phones.
Python is a powerful programming language, but it is also designed to be as easy as possible to learn. It is also where the Raspberry Pi gets the second part of its name – Python shortened to Pi.
The Raspberry Pi Foundation recommends using Raspbian for an operating system. This comes with several software packages already installed – including Python and Pygame, a Python add-on that adds the relevant tools for writing games.
Idle
To write the required code, we will use a tool called Idle – named after Monty Python member Eric Idle – that is included in the Raspbian operating system.
To load Idle, double-click the icon on the Raspbian desktop. If two Idle icons are visible, don’t worry. The first, labelled ‘Idle’, is the one to double-click; the second, ‘Idle 3’, is a special tool for programming in the latest version of Python – it includes features not needed for now, so ignore it.
Idle has two main parts: an interactive portion, which operates a bit like a super-powerful calculator and is called the ‘shell’, and an editing portion. Having double-clicked the Idle icon, the shell appears. Test this by typing in a simple sum, such as 1+2, and pressing the Enter key – the answer 3 should appear.
The shell is useful for trying out simple commands, but not for more detailed work. Click the File menu and choose New Window to open an editing window; this is like a text editor or word processor. Multiple lines can be typed in this window, and pressing Enter inserts a new line beneath the text cursor (rather than immediately executing the command, as with the shell).
Take a moment to click the File menu in the editor window and then choose Save As. In the box that appears, type raspberrysnake.py as the file name and click Save. As you work, periodically return to the File menu and click Save; this ensures that you will not lose too much work if the Pi experiences a power cut.
Start writing code
Programming a game is like writing a story – it needs a beginning, a middle and an end, and everything needs to occur in a specific order so that the reader does not become confused. Here, the reader is the Raspberry Pi and the story is a series of instructions.
To start the game’s story, certain extras are required to extend the capabilities of the Python programming language. These packages, known as modules, include the Pygame add-on described earlier as well as tools for producing random numbers, measuring time and controlling the Python system.
Article tags
Related articles
Content Recommendation
Q.Why is Windows Backup skipping files?
Q.Why do my scanned documents display gibberish?
Q.How can I convert MTS files to edit in Windows Movie...
Updating your subscription status