Parts: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
Writing your first ever computer program on the Raspberry Pi.
We’re going to write a game that tests your reactions – press a key when you see green, but don’t when you see red.
Today we see some of what we have been waiting for – a genuine bona-fide green circle, made by you!
We’re going to need some random numbers, so edit your program in LeafPad, and add a line, just before import pygame near the top:
This makes the “random” module available to us so we can make some numbers later.
Remember we had a function called “wait”, but it never did anything? It was supposed to wait for a random amount of time before we showed our green or red shape. Let’s write it now. Find the empty wait function and replace it with:
def wait(): time_to_wait = random.randint( 1500, 3000 ) # Between 1.5 and 3 seconds pygame.time.wait( time_to_wait ) # Note bug: can't quit during this time
The first line makes a variable time_to_wait and puts a random number into it. The random.randint function gives us a random number between the two numbers we supplied, so here between 1,500 and 3,000. time_to_wait is a time in milliseconds, so this means between one and half and three seconds.
After the closing bracket, we have a hash symbol #, and then some writing. This is a “comment”, and it is completely ignored by Python. It’s just for us.
[As time goes on, I hope you will begin to see programming more and more as talking to other people, not just to the computer. It’s fairly easy to write a computer program, but much harder to understand one written by someone else. Most programs live a long time, and people need to understand them to keep them up-to-date, so making them as easy to understand as possible is very important. Comments are one way to help people understand, but in a way they are a last resort – if possible, the code itself should be so easy to understand that you don’t need many comments. Here, I thought that the translation between seconds and milliseconds might be helpful to someone looking at this later.]
The next line uses a function inside PyGame’s time module to wait for the amount of time we give it (in milliseconds, stored in time_to_wait). Note that this is not the same wait function we have seen before, pygame.event.wait. That one waits forever for an event to happen, but this one waits (and can’t be interrupted) for the amount of time we say.
I’ve added another comment to this line saying that there’s a bug in our program: if we write it like this, you can’t actually quit the game by closing the window while we’re waiting. The pygame.time.wait function won’t be interrupted by the window being closed, so we’ll ignore it. This is almost unbearably rude, but don’t worry – we’ll fix it soon (ish).
And now for the really exciting part: we’re going to draw a green shape on the screen. Let’s make a function, just above the shape function, called green_shape:
def green_shape(): green = pygame.Color( "green" ) centre = ( screen.get_width() / 2, screen.get_height() / 2 ) radius = screen.get_width() / 3 screen.fill( pygame.Color( "white" ) ) pygame.draw.circle( screen, green, centre, radius, 0 ) pygame.display.flip()
This code makes a variable green holding onto the colour green, one called centre holding the co-ordinates of the centre of the screen, and one called radius holding the size of the circle we want to draw.
Then it uses the fill function on screen to colour in the screen white, and then draws our circle with a call to pygame.draw.circle, using the variables we have prepared as arguments, telling it where to draw the circle, in what colour, and what size.
Finally it uses flip as before to tell PyGame we have finished.
The last piece of today’s jigsaw is just to call the function we created above. Find the empty shape function, and make it look like this:
def shape(): green_shape()
This literally just means run the green_shape function.
Take a deep breath, prepare to be excited, open LXTerminal and run our new program in the usual way:
If all has gone well, the ready screen will appear for a couple of seconds, before a white screen with a big green circle on it appears. This will then go away when you press a key.
If something goes wrong, check back what you typed, and compare your version against mine: redgreen.py.
Next time, we’ll find out whether you pressed a key or were too slow!