Hello, Pygame

Having exhausted everything there is to do with plain old Python, the Raspberry Pi, BBC micro:bit (ok, maybe not :), one area of Python that the students I see are interested in - that I know nothing about - is pygame. So maybe I need to do some investigation.

First, download and install as you would expect. I went with the standard windows machine install as the easiest for me to get going with quickly. Then comes the canonical (or as close as we can get) hello world skeleton.

This doesn’t do much but sets us up a nice walking skeleton we can add to later.

hello

A black screen isn’t terribly exciting so, let’s draw a square:

block

Now, how about a bit of interaction? We can read from the keyboard in the message pump, looking for pygame.KEYDOWN and pygame.K_SPACE.

block

Very interactive but maybe moving the block around might be more what we want. Let’s use the arrow keys to move the block left, right, up and down the screen. Using the key.get_pressed() function lets us keep a key pressed down rather than having to explicitly push and release each time we want to move the block.

For good measure we should prevent the coordinates going outside of the screen.

block

Hooray, now we can change the block colour and move it around using the keyboard. It’s a real game :)

One thing to note is using the Clock tick function to stop the while loop running as fast as possible on your hardware and bringing it down to speeds suitable for human interaction.

If you liked this post you can tweet it or follow me on Twitter!