Pygame Tutorials
Windows Executables

by Pete Shinners
pete@shinners.org

Revision 1.2, January 28th, 2002



Introduction

One drawback to creating your game with pygame is the large amount of dependencies that your game requires. In order for people to play your game, they need a lot of libraries installed. For unix users, this isn't too bad, since most unix distributions come with their own package and dependency management system. On Windows there is nothing like this, and it is more difficult for a Windows user to just download your python source and run your game.
 
The best solution is to create a collection of all the needed files for your game to run. With python this can mean a lot of files. This document will show you the tools you need to create a standalone version of your game. It is not difficult.
 
 

Download The Tools

The first thing you need to do is download the tools to build the executable. We will use the excellent PY2EXE tool. This package extends the distutils to turn your python code into anup named RenderPlain. This sprite group can draw all the sprites it contains to the screen. It is called RenderPlain because there are actually more advanced Render groups. But for our game, we just need simple drawing. We create the group named "allsprites" by passing a list with all the sprites that should belong in the group. We could later on add or remove sprites from this group, but in this game we won't need to.

The clock object we create will be used to help control our game's framerate. we will use it in the main loop of our game to make sure it doesn't run too fast.

Main Loop

Nothing much here, just an infinite loop.

while 1:
    clock.tick(60)

All games run in some sort of loop. The usual order of things is to check on the state of the computer and user input, move and update the state of all the objects, and then draw them to the screen. You'll see that this example is no different.

We also make a call to our clock object, which will make sure our game doesn't run faster than 60 frames per second.


Handle All Input Events

This is an extremely simple case of working the event queue.

for event in pygame.event.get():
    if event.type is QUIT:
        return
    elif event.type is KEYDOWN and event.key is K_ESCAPE:
        return
    elif event.type is MOUSEBUTTONDOWN:
        if fist.punch(chimp):
            punch_sound.play() #punch
            chimp.punched()
        else:
            whiff_sound.play() #miss
    elif event.type is MOUSEBUTTONUP:
        fist.unpunch()

First we get all the available Events from pygame and loop through each of them. The first two tests see if the user has quit our game, or pressed the escape key. In these cases we just return from the main() function and the program cleanly ends.

Next we just check to see if the mouse button was pressed or released. If the button was pressed, we ask the fist object if it has collided with the chimp. We play the appropriate sound effect, and if the monkey was hit, we tell him to start spinning (by calling his punched() method).


Update the Sprites


allsprites.update()

Sprite groups have an update() method, which simply calls the update method for all the sprites it contains. Each of the objects will move around, depending on which state they are in. This is where the chimp will move one step side to side, or spin a little farther if he was recently punched.



Draw The Entire Scene

Now that all the objects are in the right place, time to draw them.

screen.blit(background, (0, 0))
allsprites.draw(screen)
pygame.display.flip()

The first blit call will draw the background onto the entire screen. This erases everything we saw from the previous frame (slightly inefficient, but good enough for this game). Next we call the draw() method of the sprite container. Since this sprite container is really an instance of the "DrawPlain" sprite group, it knows how to draw our sprites. Lastly, we flip() th