1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!--
2
TUTORIAL:Tom Chance's Making Games Tutorial
7
>Making games with Pygame</TITLE
10
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
12
TITLE="Revision: Pygame fundamentals"
13
HREF="games2.html"> <style type="text/stylesheet">
15
PRE.PROGRAMLISTING { background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin } -->
36
>Making games with Pygame</H1
66
>Corrections, included full code in 6.3</TD
91
<p>The most current version of this document will always be found at <a href="http://www.tomchance.uklinux.net/pygame">http://www.tomchance.uklinux.net/pygame</a>.<br>
92
A PDF of this document (unfortunately with messed up images) can be downloaded from <a href="http://www.tomchance.uklinux.net/pygame/pygame.pdf">http://www.tomchance.uklinux.net/pygame/pygame.pdf</a><br><br>
103
HREF="MakeGames.html#AEN12"
110
HREF="MakeGames.html#AEN44"
111
>A note on coding styles</A
118
>Revision: Pygame fundamentals</A
124
HREF="games2.html#AEN51"
125
>The basic Pygame game</A
129
HREF="games2.html#AEN59"
130
>Basic Pygame objects</A
134
HREF="games2.html#AEN68"
139
HREF="games2.html#AEN73"
144
HREF="games2.html#AEN79"
152
>Kicking things off</A
158
HREF="games3.html#AEN87"
159
>The first lines, and loading modules</A
163
HREF="games3.html#AEN91"
164
>Resource handling functions</A
171
>Game object classes</A
177
HREF="games4.html#AEN117"
178
>A simple ball class</A
184
HREF="games4.html#AEN127"
185
>Diversion 1: Sprites</A
189
HREF="games4.html#AEN138"
190
>Diversion 2: Vector physics</A
199
>User-controllable objects</A
205
HREF="games5.html#AEN161"
206
>A simple bat class</A
212
HREF="games5.html#AEN180"
213
>Diversion 3: Pygame events</A
222
>Putting it all together</A
228
HREF="games6.html#AEN198"
229
>Let the ball hit sides</A
233
HREF="games6.html#AEN210"
234
>Let the ball hit bats</A
238
HREF="games6.html#AEN225"
239
>The Finished product</A
254
>First of all, I will assume you have read the <A
255
HREF="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html"
262
> tutorial, which introduces the basics of Python and Pygame. Give it a read before reading this
263
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
264
who understand how to make a ridiculously simple little "game", and who would like to make a relatively simple game like Pong.
265
It introduces you to some concepts of game design, some simple mathematics to work out ball physics, and some ways to keep your
266
game easy to maintain and expand.</P
268
>All the code in this tutorial works toward implementing <A
269
HREF="http://www.tomchance.uklinux.net/projects/pong.shtml"
275
>, a game I've written. By the end of the tutorial, you should not only have a firmer grasp of Pygame, but
276
you should also understand how TomPong works, and how to make your own version.</P
278
>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
291
> which are required in the game. Standard stuff, except that you should
292
remember to import the Pygame local names as well as the Pygame module itself</P
300
>Resource handling classes</B
302
>; define some classes to handle your most basic resources,
303
which will be loading images and sounds, as well as connecting and disconnecting to and from networks, loading save game
304
files, and any other resources you might have.</P
312
>Game object classes</B
314
>; define the classes for your game object. In the pong example,
315
these will be one for the player's bat (which you can initialise multiple times, one for each player in the game), and one
316
for the ball (which can again have multiple instances). If you're going to have a nice in-game menu, it's also a good idea to make a
325
>Any other game functions</B
327
>; define other necessary functions, such as scoreboards, menu
328
handling, etc. Any code that you could put into the main game logic, but that would make understanding said logic harder, should
329
be put into its own function. So as plotting a scoreboard isn't game logic, it should be moved into a function.</P
337
>Initialise the game</B
339
>, including the Pygame objects themselves, the background, the game
340
objects (initialising instances of the classes) and any other little bits of code you might want to add in.</P
350
>, into which you put any input handling (i.e. watching for users hitting
351
keys/mouse buttons), the code for updating the game objects, and finally for updating the screen.</P
355
>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
356
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
357
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
358
idea to source certain sections into module files. Putting the game object classes into a file called "<TT
362
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
363
to put that into "<TT
366
>". You can then "<TT
368
>from objects,resources import *</TT
369
>" to import all of the
370
classes and functions.</P
378
>1.1. A note on coding styles</H2
380
>The first thing to remember when approaching any programming project is to decide on a coding style, and stay consistent. Python
381
solves a lot of the problems because of its strict interpretation of whitespace and indentation, but you can still choose the size
382
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
383
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
384
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
385
seen plenty of people do the following:</P
387
CLASS="PROGRAMLISTING"
388
>player1.score += scoreup # Add scoreup to player1 score</PRE
390
>The worst code is poorly laid out, with seemingly random changes in style, and poor documentation. Poor code is not only annoying
391
for other people, but it also makes it difficult for you to maintain.</P
400
SUMMARY="Footer navigation table"
441
>Revision: Pygame fundamentals</TD