1. pygame
  2. Untitled project
  3. pygame

Source

pygame / docs / tut / tom / MakeGames.html

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!--
TUTORIAL:Tom Chance's Making Games Tutorial
-->
<HTML
><HEAD
><TITLE
>Making games with Pygame</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="NEXT"
TITLE="Revision: Pygame fundamentals"
HREF="games2.html"> <style type="text/stylesheet">
	<!--
	PRE.PROGRAMLISTING	{ background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin }	-->
 </style></HEAD
>
<BODY
CLASS="ARTICLE"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
>

<DIV
CLASS="ARTICLE"
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="TITLE"
><A
NAME="AEN2"
></A
>Making games with Pygame</H1
><DIV
CLASS="REVHISTORY"
><TABLE
WIDTH="100%"
BORDER="0"
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
COLSPAN="3"
><B
>Revision History</B
></TH
></TR
><TR
><TD
ALIGN="LEFT"
>Revision 1.1</TD
><TD
ALIGN="LEFT"
>5th March 2003</TD
><TD
ALIGN="LEFT"
></TD
></TR
><TR
><TD
ALIGN="LEFT"
COLSPAN="3"
>Corrections, included full code in 6.3</TD
></TR
><TR
><TD
ALIGN="LEFT"
>Revision 1.0</TD
><TD
ALIGN="LEFT"
>2nd March 2003</TD
><TD
ALIGN="LEFT"
></TD
></TR
><TR
><TD
ALIGN="LEFT"
COLSPAN="3"
>First release</TD
></TR
></TABLE
></DIV
></DIV
>

<DIV>
<p>The most current version of this document will always be found at <a href="http://tom.acrewoods.net/writing/pygame">http://tom.acrewoods.net/writing/pygame</a>.<br>
<HR></DIV>
<DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>1. <A
HREF="MakeGames.html#AEN12"
>Introduction</A
></DT
><DD
><DL
><DT
>1.1. <A
HREF="MakeGames.html#AEN44"
>A note on coding styles</A
></DT
></DL
></DD
><DT
>2. <A
HREF="games2.html"
>Revision: Pygame fundamentals</A
></DT
><DD
><DL
><DT
>2.1. <A
HREF="games2.html#AEN51"
>The basic Pygame game</A
></DT
><DT
>2.2. <A
HREF="games2.html#AEN59"
>Basic Pygame objects</A
></DT
><DT
>2.3. <A
HREF="games2.html#AEN68"
>Blitting</A
></DT
><DT
>2.4. <A
HREF="games2.html#AEN73"
>The event loop</A
></DT
><DT
>2.5. <A
HREF="games2.html#AEN79"
>Ta-da!</A
></DT
></DL
></DD
><DT
>3. <A
HREF="games3.html"
>Kicking things off</A
></DT
><DD
><DL
><DT
>3.1. <A
HREF="games3.html#AEN87"
>The first lines, and loading modules</A
></DT
><DT
>3.2. <A
HREF="games3.html#AEN91"
>Resource handling functions</A
></DT
></DL
></DD
><DT
>4. <A
HREF="games4.html"
>Game object classes</A
></DT
><DD
><DL
><DT
>4.1. <A
HREF="games4.html#AEN117"
>A simple ball class</A
></DT
><DD
><DL
><DT
>4.1.1. <A
HREF="games4.html#AEN127"
>Diversion 1: Sprites</A
></DT
><DT
>4.1.2. <A
HREF="games4.html#AEN138"
>Diversion 2: Vector physics</A
></DT
></DL
></DD
></DL
></DD
><DT
>5. <A
HREF="games5.html"
>User-controllable objects</A
></DT
><DD
><DL
><DT
>5.1. <A
HREF="games5.html#AEN161"
>A simple bat class</A
></DT
><DD
><DL
><DT
>5.1.1. <A
HREF="games5.html#AEN180"
>Diversion 3: Pygame events</A
></DT
></DL
></DD
></DL
></DD
><DT
>6. <A
HREF="games6.html"
>Putting it all together</A
></DT
><DD
><DL
><DT
>6.1. <A
HREF="games6.html#AEN198"
>Let the ball hit sides</A
></DT
><DT
>6.2. <A
HREF="games6.html#AEN210"
>Let the ball hit bats</A
></DT
><DT
>6.3. <A
HREF="games6.html#AEN225"
>The Finished product</A
></DT
></DL
></DD
></DL
></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN12"
></A
>1. Introduction</H1
><P
>First of all, I will assume you have read the <A
HREF="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html"
TARGET="_top"
><I
CLASS="CITETITLE"
>Line
By Line Chimp</I
></A
> 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.</P
><P
>All the code in this tutorial works toward implementing <A
HREF="http://www.tomchance.uklinux.net/projects/pong.shtml"
TARGET="_top"
><I
CLASS="CITETITLE"
>TomPong</I
></A
>, 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.</P
><P
>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:</P
><P
></P
><UL
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>Load modules</B
></SPAN
> which are required in the game. Standard stuff, except that you should
	remember to import the Pygame local names as well as the Pygame module itself</P
></LI
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>Resource handling classes</B
></SPAN
>; define some classes to handle your most basic resources,
	which will be loading images and sounds, as well as connecting and disconnecting to and from networks, loading save game
	files, and any other resources you might have.</P
></LI
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>Game object classes</B
></SPAN
>; define the classes for your game object. In the pong example,
	these will be one for the player's bat (which you can initialise multiple times, one for each player in the game), and one
	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
	menu class.</P
></LI
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>Any other game functions</B
></SPAN
>; define other necessary functions, such as scoreboards, menu
	handling, etc. Any code that you could put into the main game logic, but that would make understanding said logic harder, should
	be put into its own function. So as plotting a scoreboard isn't game logic, it should be moved into a function.</P
></LI
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>Initialise the game</B
></SPAN
>, including the Pygame objects themselves, the background, the game
	objects (initialising instances of the classes) and any other little bits of code you might want to add in.</P
></LI
><LI
><P
><SPAN
CLASS="bold"
><B
CLASS="EMPHASIS"
>The main loop</B
></SPAN
>, into which you put any input handling (i.e. watching for users hitting
	keys/mouse buttons), the code for updating the game objects, and finally for updating the screen.</P
></LI
></UL
><P
>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 "<TT
CLASS="FUNCTION"
>objects.py</TT
>", 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 "<TT
CLASS="FUNCTION"
>resources.py</TT
>". You can then "<TT
CLASS="FUNCTION"
>from objects,resources import *</TT
>" to import all of the
	classes and functions.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN44"
></A
>1.1. A note on coding styles</H2
><P
>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:</P
><PRE
CLASS="PROGRAMLISTING"
>player1.score += scoreup	# Add scoreup to player1 score</PRE
><P
>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.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="games2.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Revision: Pygame fundamentals</TD
></TR
></TABLE
>

</body>
</html>



</DIV
>

</BODY
></HTML
>