Writing your first ever computer program on the Raspberry Pi.
Today we will get an actual window to appear, with nothing on it!
Last time we wrote a magic spell describing the bare outline of what how our game will work. It’s going to pause, show a shape, and then the player must either press a key (if it’s green) or not press a key (if it’s red).
The program we’ve written calls several mini-programs called functions, but they don’t exist yet, and if we run it, it fails with an error. The names of the functions we have imagined are start, ready_screen, wait, shape and end.
The first thing we need to do is write those functions, so that the program runs correctly, even though it does nothing.
Open up LeafPad again and open redgreen.py from inside the pi folder. At the top, above the code we’ve already written (which uses the functions) type exactly this:
def start(): pass def ready_screen(): pass def wait(): pass def shape(): pass def end(): pass
Notice that the word pass is “indented” by 4 spaces. This is how Python knows which lines of code are part of your function, and which are another part of the program. It’s very important that all lines are indented by the same amount, and that you never mix “tab” characters with spaces. To make life simple, I suggest always using 4 spaces.
What we’ve done is define 5 functions (the ones we call further down). def just means define a function, and we’ve followed that by the name of the function, then (). So far, each of our functions is empty: they are all just one line, which says pass, which means “do nothing”.
If we’ve typed this correctly, our program should now run correctly, and do nothing. Let’s try it. Open LXTerminal and type:
If it’s working, the computer should say absolutely nothing to you, and give you another prompt, ending with $.
If you get an error message, read it (especially the last line) and try to work out what you typed wrong.
Now we’re going to write some actual useful code, and make a black screen appear.
At the very top, above everything else we’ve done today, add this line:
This tells Python we want to use the PyGame library, which is some code written by someone else, designed to help us write games in Python. The Raspberry Pi comes with PyGame already installed, so to use it all we need to do is add this line.
Just below that line, type this:
screen_size = 640, 480
This is how we define a “variable”. A variable is a thing with a name and a value that we can refer back to later. The variable’s name is screen_size and its value is “640, 480”. We’re going to use this variable to remember how big we want our window to be.
Now we need to modify our start function to bring up a black screen. Replace both lines of the start function with this:
def start(): pygame.init() screen = pygame.display.set_mode( screen_size )
The first line, starting with def is the same as before – it means we are defining a function called “start”.
Where we used to have just pass, we now have 2 lines (both indented by 4 spaces as before). The pygame.init() line just tells PyGame to get ready to start. We need something like this in our program whenever we use PyGame. The second line creates a window of the size we want. It calls a function inside pygame. The dots mean we should “look inside” the thing on the left, so pygame.display.set_mode means something like “look inside pygame for something called display, then look inside display for something called set_mode”. set_mode is a function, which we are calling (that is what the brackets mean).
Unlike other functions we have called, which just use “()”, set_mode takes an “argument” which means we are passing information to it. The information we are passing is the size of the screen, which we have already stored in the variable screen_size. So the value “640, 480” gets passed in to set_mode, telling it how big to make the window.
We’ve made a window, but if we leave things as they are now, we will open it, then immediately finish, so it will go away again. We need to wait a while.
Edit the end function to look like this:
def end(): pygame.event.clear() pygame.event.wait()
This code means “wait until something happens”.
The first line (clear) tells PyGame we’re not interested in anything that happened before – clear the list of events that has built up – and the second line (wait) means wait until something happens. In this case we are waiting until the person using the program presses a key, closes the window, or even moves the mouse in front of the window.
Let’s try it out. Open up LXTerminal as before, and type the same thing we’ve typed before:
Make sure you don’t move the mouse or press any keys after you’ve pressed Return. If all has gone well, a small, black, empty window will appear. Then if you press a key or move the mouse in front of it, it should disappear.
If this isn’t what happens, check back and make sure you typed everything exactly as above. Your complete file should look like this: redgreen.py.
Well done! You made a window appear. Next time, if we’re lucky, we’ll put something in it…