~ubuntu-branches/ubuntu/feisty/pygame/feisty

« back to all changes in this revision

Viewing changes to docs/tut/tom/MakeGames.html

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2004-08-08 00:10:10 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040808001010-nts9g4z69jf1ryb1
Tags: 1.6-2
* Actually build-depend on python (for dh_python)
  * Closes: #264086

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!--
 
2
TUTORIAL:Tom Chance's Making Games Tutorial
 
3
-->
 
4
<HTML
 
5
><HEAD
 
6
><TITLE
 
7
>Making games with Pygame</TITLE
 
8
><META
 
9
NAME="GENERATOR"
 
10
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
 
11
REL="NEXT"
 
12
TITLE="Revision: Pygame fundamentals"
 
13
HREF="games2.html"> <style type="text/stylesheet">
 
14
        <!--
 
15
        PRE.PROGRAMLISTING      { background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin }   -->
 
16
 </style></HEAD
 
17
>
 
18
<BODY
 
19
CLASS="ARTICLE"
 
20
BGCOLOR="#FFFFFF"
 
21
TEXT="#000000"
 
22
LINK="#0000FF"
 
23
VLINK="#840084"
 
24
ALINK="#0000FF"
 
25
>
 
26
 
 
27
<DIV
 
28
CLASS="ARTICLE"
 
29
><DIV
 
30
CLASS="TITLEPAGE"
 
31
><H1
 
32
CLASS="TITLE"
 
33
><A
 
34
NAME="AEN2"
 
35
></A
 
36
>Making games with Pygame</H1
 
37
><DIV
 
38
CLASS="REVHISTORY"
 
39
><TABLE
 
40
WIDTH="100%"
 
41
BORDER="0"
 
42
><TR
 
43
><TH
 
44
ALIGN="LEFT"
 
45
VALIGN="TOP"
 
46
COLSPAN="3"
 
47
><B
 
48
>Revision History</B
 
49
></TH
 
50
></TR
 
51
><TR
 
52
><TD
 
53
ALIGN="LEFT"
 
54
>Revision 1.1</TD
 
55
><TD
 
56
ALIGN="LEFT"
 
57
>5th March 2003</TD
 
58
><TD
 
59
ALIGN="LEFT"
 
60
></TD
 
61
></TR
 
62
><TR
 
63
><TD
 
64
ALIGN="LEFT"
 
65
COLSPAN="3"
 
66
>Corrections, included full code in 6.3</TD
 
67
></TR
 
68
><TR
 
69
><TD
 
70
ALIGN="LEFT"
 
71
>Revision 1.0</TD
 
72
><TD
 
73
ALIGN="LEFT"
 
74
>2nd March 2003</TD
 
75
><TD
 
76
ALIGN="LEFT"
 
77
></TD
 
78
></TR
 
79
><TR
 
80
><TD
 
81
ALIGN="LEFT"
 
82
COLSPAN="3"
 
83
>First release</TD
 
84
></TR
 
85
></TABLE
 
86
></DIV
 
87
></DIV
 
88
>
 
89
 
 
90
<DIV>
 
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>
 
93
<HR></DIV>
 
94
<DIV
 
95
CLASS="TOC"
 
96
><DL
 
97
><DT
 
98
><B
 
99
>Table of Contents</B
 
100
></DT
 
101
><DT
 
102
>1. <A
 
103
HREF="MakeGames.html#AEN12"
 
104
>Introduction</A
 
105
></DT
 
106
><DD
 
107
><DL
 
108
><DT
 
109
>1.1. <A
 
110
HREF="MakeGames.html#AEN44"
 
111
>A note on coding styles</A
 
112
></DT
 
113
></DL
 
114
></DD
 
115
><DT
 
116
>2. <A
 
117
HREF="games2.html"
 
118
>Revision: Pygame fundamentals</A
 
119
></DT
 
120
><DD
 
121
><DL
 
122
><DT
 
123
>2.1. <A
 
124
HREF="games2.html#AEN51"
 
125
>The basic Pygame game</A
 
126
></DT
 
127
><DT
 
128
>2.2. <A
 
129
HREF="games2.html#AEN59"
 
130
>Basic Pygame objects</A
 
131
></DT
 
132
><DT
 
133
>2.3. <A
 
134
HREF="games2.html#AEN68"
 
135
>Blitting</A
 
136
></DT
 
137
><DT
 
138
>2.4. <A
 
139
HREF="games2.html#AEN73"
 
140
>The event loop</A
 
141
></DT
 
142
><DT
 
143
>2.5. <A
 
144
HREF="games2.html#AEN79"
 
145
>Ta-da!</A
 
146
></DT
 
147
></DL
 
148
></DD
 
149
><DT
 
150
>3. <A
 
151
HREF="games3.html"
 
152
>Kicking things off</A
 
153
></DT
 
154
><DD
 
155
><DL
 
156
><DT
 
157
>3.1. <A
 
158
HREF="games3.html#AEN87"
 
159
>The first lines, and loading modules</A
 
160
></DT
 
161
><DT
 
162
>3.2. <A
 
163
HREF="games3.html#AEN91"
 
164
>Resource handling functions</A
 
165
></DT
 
166
></DL
 
167
></DD
 
168
><DT
 
169
>4. <A
 
170
HREF="games4.html"
 
171
>Game object classes</A
 
172
></DT
 
173
><DD
 
174
><DL
 
175
><DT
 
176
>4.1. <A
 
177
HREF="games4.html#AEN117"
 
178
>A simple ball class</A
 
179
></DT
 
180
><DD
 
181
><DL
 
182
><DT
 
183
>4.1.1. <A
 
184
HREF="games4.html#AEN127"
 
185
>Diversion 1: Sprites</A
 
186
></DT
 
187
><DT
 
188
>4.1.2. <A
 
189
HREF="games4.html#AEN138"
 
190
>Diversion 2: Vector physics</A
 
191
></DT
 
192
></DL
 
193
></DD
 
194
></DL
 
195
></DD
 
196
><DT
 
197
>5. <A
 
198
HREF="games5.html"
 
199
>User-controllable objects</A
 
200
></DT
 
201
><DD
 
202
><DL
 
203
><DT
 
204
>5.1. <A
 
205
HREF="games5.html#AEN161"
 
206
>A simple bat class</A
 
207
></DT
 
208
><DD
 
209
><DL
 
210
><DT
 
211
>5.1.1. <A
 
212
HREF="games5.html#AEN180"
 
213
>Diversion 3: Pygame events</A
 
214
></DT
 
215
></DL
 
216
></DD
 
217
></DL
 
218
></DD
 
219
><DT
 
220
>6. <A
 
221
HREF="games6.html"
 
222
>Putting it all together</A
 
223
></DT
 
224
><DD
 
225
><DL
 
226
><DT
 
227
>6.1. <A
 
228
HREF="games6.html#AEN198"
 
229
>Let the ball hit sides</A
 
230
></DT
 
231
><DT
 
232
>6.2. <A
 
233
HREF="games6.html#AEN210"
 
234
>Let the ball hit bats</A
 
235
></DT
 
236
><DT
 
237
>6.3. <A
 
238
HREF="games6.html#AEN225"
 
239
>The Finished product</A
 
240
></DT
 
241
></DL
 
242
></DD
 
243
></DL
 
244
></DIV
 
245
><DIV
 
246
CLASS="SECT1"
 
247
><H1
 
248
CLASS="SECT1"
 
249
><A
 
250
NAME="AEN12"
 
251
></A
 
252
>1. Introduction</H1
 
253
><P
 
254
>First of all, I will assume you have read the <A
 
255
HREF="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html"
 
256
TARGET="_top"
 
257
><I
 
258
CLASS="CITETITLE"
 
259
>Line
 
260
By Line Chimp</I
 
261
></A
 
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
 
267
><P
 
268
>All the code in this tutorial works toward implementing <A
 
269
HREF="http://www.tomchance.uklinux.net/projects/pong.shtml"
 
270
TARGET="_top"
 
271
><I
 
272
CLASS="CITETITLE"
 
273
>TomPong</I
 
274
></A
 
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
 
277
><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
 
279
six sections:</P
 
280
><P
 
281
></P
 
282
><UL
 
283
><LI
 
284
><P
 
285
><SPAN
 
286
CLASS="bold"
 
287
><B
 
288
CLASS="EMPHASIS"
 
289
>Load modules</B
 
290
></SPAN
 
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
 
293
></LI
 
294
><LI
 
295
><P
 
296
><SPAN
 
297
CLASS="bold"
 
298
><B
 
299
CLASS="EMPHASIS"
 
300
>Resource handling classes</B
 
301
></SPAN
 
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
 
305
></LI
 
306
><LI
 
307
><P
 
308
><SPAN
 
309
CLASS="bold"
 
310
><B
 
311
CLASS="EMPHASIS"
 
312
>Game object classes</B
 
313
></SPAN
 
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
 
317
        menu class.</P
 
318
></LI
 
319
><LI
 
320
><P
 
321
><SPAN
 
322
CLASS="bold"
 
323
><B
 
324
CLASS="EMPHASIS"
 
325
>Any other game functions</B
 
326
></SPAN
 
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
 
330
></LI
 
331
><LI
 
332
><P
 
333
><SPAN
 
334
CLASS="bold"
 
335
><B
 
336
CLASS="EMPHASIS"
 
337
>Initialise the game</B
 
338
></SPAN
 
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
 
341
></LI
 
342
><LI
 
343
><P
 
344
><SPAN
 
345
CLASS="bold"
 
346
><B
 
347
CLASS="EMPHASIS"
 
348
>The main loop</B
 
349
></SPAN
 
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
 
352
></LI
 
353
></UL
 
354
><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
 
359
CLASS="FUNCTION"
 
360
>objects.py</TT
 
361
>", for
 
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
 
364
CLASS="FUNCTION"
 
365
>resources.py</TT
 
366
>". You can then "<TT
 
367
CLASS="FUNCTION"
 
368
>from objects,resources import *</TT
 
369
>" to import all of the
 
370
        classes and functions.</P
 
371
><DIV
 
372
CLASS="SECT2"
 
373
><H2
 
374
CLASS="SECT2"
 
375
><A
 
376
NAME="AEN44"
 
377
></A
 
378
>1.1. A note on coding styles</H2
 
379
><P
 
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
 
386
><PRE
 
387
CLASS="PROGRAMLISTING"
 
388
>player1.score += scoreup       # Add scoreup to player1 score</PRE
 
389
><P
 
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
 
392
></DIV
 
393
></DIV
 
394
></DIV
 
395
><DIV
 
396
CLASS="NAVFOOTER"
 
397
><HR
 
398
ALIGN="LEFT"
 
399
WIDTH="100%"><TABLE
 
400
SUMMARY="Footer navigation table"
 
401
WIDTH="100%"
 
402
BORDER="0"
 
403
CELLPADDING="0"
 
404
CELLSPACING="0"
 
405
><TR
 
406
><TD
 
407
WIDTH="33%"
 
408
ALIGN="left"
 
409
VALIGN="top"
 
410
>&nbsp;</TD
 
411
><TD
 
412
WIDTH="34%"
 
413
ALIGN="center"
 
414
VALIGN="top"
 
415
>&nbsp;</TD
 
416
><TD
 
417
WIDTH="33%"
 
418
ALIGN="right"
 
419
VALIGN="top"
 
420
><A
 
421
HREF="games2.html"
 
422
ACCESSKEY="N"
 
423
>Next</A
 
424
></TD
 
425
></TR
 
426
><TR
 
427
><TD
 
428
WIDTH="33%"
 
429
ALIGN="left"
 
430
VALIGN="top"
 
431
>&nbsp;</TD
 
432
><TD
 
433
WIDTH="34%"
 
434
ALIGN="center"
 
435
VALIGN="top"
 
436
>&nbsp;</TD
 
437
><TD
 
438
WIDTH="33%"
 
439
ALIGN="right"
 
440
VALIGN="top"
 
441
>Revision: Pygame fundamentals</TD
 
442
></TR
 
443
></TABLE
 
444
>
 
445
 
 
446
</body>
 
447
</html>
 
448
 
 
449
 
 
450
 
 
451
</DIV
 
452
>
 
453
 
 
454
</BODY
 
455
></HTML
 
456
>