/* jquery */ /* jquery accordion style*/ /* jquery init */

24 June 2013

Learn Python - PyGame Init Code

Before we can use any of the PyGame module's features we'll need to a few initialisation statements.

Here's a simple example:

pygame.init()
screen = pygame.display.set_mode([500,400])
screen.fill(pygame.Color('black'))
pygame.display.set_caption("My Game")
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.

Next a 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.

The pygame.display.set_caption statement 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.