Before we can use any of the PyGame module's features we'll need to a few initialisation statements.
Here's a simple example:
screen = pygame.display.set_mode([500,400])
clock = pygame.time.Clock()
One the first line there a call to the
pygame.init function. This does a whole load of very clever stuff that we don't need to worry about. That's the power of using modules. Suffice to say, after this call we only need a few additional lines of code.
screen object is created using the
pygame.display.set_mode function. The parameter is a list (hence the square brackets) which contains two numbers. These numbers correspond to the width and height of the screen in pixels. So, here the screen object created will be
500 pixels wide and
400 pixels high.
Now we have a
screen object we can do things with it. As an example the following line calls the
screen.fill function to set a background colour to
black. We'll be using the
screen object again later on.
pygame.display.set_captionstatement simply sets the window title, as defined by the text string parameter.
And finally a
clock object is created using the
pygame.time.Clock() function. A clock object allows to adjust the timing of drawing events within the game. Once again, we'll see how this is used later.
A post from my Learn Python on the Raspberry Pi tutorial.