Making games with Pygame

Revision History
Revision 1.15th March 2003
Corrections, included full code in 6.3
Revision 1.02nd March 2003
First release

The most current version of this document will always be found at http://www.tomchance.org.uk/writing/pygame.


Table of Contents
1. Introduction
1.1. A note on coding styles
2. Revision: Pygame fundamentals
2.1. The basic Pygame game
2.2. Basic Pygame objects
2.3. Blitting
2.4. The event loop
2.5. Ta-da!
3. Kicking things off
3.1. The first lines, and loading modules
3.2. Resource handling functions
4. Game object classes
4.1. A simple ball class
4.1.1. Diversion 1: Sprites
4.1.2. Diversion 2: Vector physics
5. User-controllable objects
5.1. A simple bat class
5.1.1. Diversion 3: Pygame events
6. Putting it all together
6.1. Let the ball hit sides
6.2. Let the ball hit bats
6.3. The Finished product

1. Introduction

First of all, I will assume you have read the Line By Line Chimp tutorial, which introduces the basics of Python and Pygame. Give it a read before reading this tutorial, as I won't bother repeating what that tutorial says (or at least not in as much detail). This tutorial is aimed at those who understand how to make a ridiculously simple little "game", and who would like to make a relatively simple game like Pong. It introduces you to some concepts of game design, some simple mathematics to work out ball physics, and some ways to keep your game easy to maintain and expand.

All the code in this tutorial works toward implementing TomPong, a game I've written. By the end of the tutorial, you should not only have a firmer grasp of Pygame, but you should also understand how TomPong works, and how to make your own version.

Now, for a brief recap of the basics of Pygame. A common method of organising the code for a game is to divide it into the following six sections:

Every game you make will have some or all of those sections, possibly with more of your own. For the purposes of this tutorial, I will write about how TomPong is laid out, and the ideas I write about can be transferred to almopst any kind of game you might make. I will also assume that you want to keep all of the code in a single file, but if you're making a reasonably large game, it's often a good idea to source certain sections into module files. Putting the game object classes into a file called "objects.py", for example, can help you keep game logic separate from game objects. If you have a lot of resource handling code, it can also be handy to put that into "resources.py". You can then "from objects,resources import *" to import all of the classes and functions.

1.1. A note on coding styles

The first thing to remember when approaching any programming project is to decide on a coding style, and stay consistent. Python solves a lot of the problems because of its strict interpretation of whitespace and indentation, but you can still choose the size of your indentations, whether you put each module import on a new line, how you comment code, etc. You'll see how I do all of this in the code examples; you needn't use my style, but whatever style you adopt, use it all the way through the program code. Also try to document all of your classes, and comment on any bits of code that seem obscure, though don't start commenting the obvious. I've seen plenty of people do the following:

player1.score += scoreup	# Add scoreup to player1 score

The worst code is poorly laid out, with seemingly random changes in style, and poor documentation. Poor code is not only annoying for other people, but it also makes it difficult for you to maintain.