1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5
>Game object classes</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10
HREF="MakeGames.html"><LINK
12
TITLE="Kicking things off"
13
HREF="games3.html"><LINK
15
TITLE="User-controllable objects"
16
HREF="games5.html"> <style type="text/stylesheet">
18
PRE.PROGRAMLISTING { background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin } -->
32
SUMMARY="Header navigation table"
79
>4. Game object classes</H1
81
>Once you've loaded your modules, and written your resource handling functions, you'll want to get on to writing some game objects.
82
The way this is done is fairly simple, though it can seem complex at first. You write a class for each type of object in the game,
83
and then create an instance of those classes for the objects. You can then use those classes' methods to manipulate the objects,
84
giving objects some motion and interactive capabilities. So your game, in pseudo-code, will look like this:</P
86
CLASS="PROGRAMLISTING"
91
[resource handling functions here]
94
[ball functions (methods) here]
95
[e.g. a function to calculate new position]
96
[and a function to check if it hits the side]
99
[initiate game environment here]
101
[create new object as instance of ball class]
105
[check for user input]
107
[call ball's update function]
110
>This is, of course, a very simple example, and you'd need to put in all the code, instead of those little bracketed comments. But
111
you should get the basic idea. You crate a class, into which you put all the functions for a ball, including <TT
115
which would create all the ball's attributes, and <TT
118
>, which would move the ball to its new position, before blitting
119
it onto the screen in this position.</P
121
>You can then create more classes for all of your other game objects, and then create instances of them so that you can handle them
125
> function and the main program loop. Contrast this with initiating the ball in the <TT
129
function, and then having lots of classless functions to manipulate a set ball object, and you'll hopefully see why using classes is
130
an advantage: It allows you to put all of the code for each object in one place; it makes using objects easier; it makes adding new
131
objects, and manipulating them, more flexible. Rather than adding more code for each new ball object, you could simply create new
135
> class for each new ball object. Magic!</P
143
>4.1. A simple ball class</H2
145
>Here is a simple class with the functions necessary for creating a ball object that will, if the <TT
149
in the main loop, move across the screen:</P
151
CLASS="PROGRAMLISTING"
152
>class Ball(pygame.sprite.Sprite):
153
"""A ball that will move across the screen
155
Functions: update, calcnewpos
156
Attributes: area, vector"""
158
def __init__(self, vector):
159
pygame.sprite.Sprite.__init__(self)
160
self.image, self.rect = load_png('ball.png')
161
screen = pygame.display.get_surface()
162
self.area = screen.get_rect()
166
newpos = self.calcnewpos(self.rect,self.vector)
169
def calcnewpos(self,rect,vector):
171
(dx,dy) = (z*math.cos(angle),z*math.sin(angle))
172
return rect.move(dx,dy)</PRE
174
>Here we have the <TT
180
> function that sets the ball up, an <TT
184
function that changes the ball's rectangle to be in the new position, and a <TT
187
> function to calculate the ball's
188
new position based on its current position, and the vector by which it is moving. I'll explain the physics in a moment. The one other
189
thing to note is the documentation string, which is a little bit longer this time, and explains the basics of the class. These strings
190
are handy not only to yourself and other programmers looking at the code, but also for tools to parse your code and document it. They
191
won't make much of a difference in small programs, but with large ones they're invaluable, so it's a good habit to get into.</P
199
>4.1.1. Diversion 1: Sprites</H3
201
>The other reason for creating a class for each object is sprites. Each image you render in your game will be a sprite object, and so
202
to begin with, the class for each object should inherit the <TT
205
> class. This is a really nice feature of Python - class
206
inheritance. Now the <TT
209
> class has all of the functions that come with the <TT
212
> class, and any object
216
> class will be registered by Pygame as sprites. Whereas with text and the background, which don't
217
move, it's OK to blit the object onto the background, Pygame handles sprite objects in a different manner, which you'll see when we
218
look at the whole program's code. </P
220
>Basically, you create both a ball object, and a sprite object for that ball, and you then call the ball's update function on the
221
sprite object, thus updating the sprite. Sprites also give you sophisticated ways of determining if two objects have collided.
222
Normally you might just check in the main loop to see if their rectangles overlap, but that would involve a lot of code, which would
223
be a waste because the <TT
226
> class provides two functions (<TT
233
to do this for you.</P
242
>4.1.2. Diversion 2: Vector physics</H3
244
>Other than the structure of the <TT
247
> class, the notable thing about this code is the vector physics, used to calculate
248
the ball's movement. With any game involving angular movement, you won't get very far unless you're comfortable with trigonometry, so
249
I'll just introduce the basics you need to know to make sense of the <TT
254
>To begin with, you'll notice that the ball has an attribute <TT
257
>, which is made up of <TT
264
>. The angle is measured in radians, and will give you the direction in which the ball is moving. Z is the speed at which the ball
265
moves. So by using this vector, we can determine the direction and speed of the ball, and therefore how much it will move on the x and
271
SRC="radians.png"></P
274
>The diagram above illustrates the basic maths behind vectors. In the left hand diagram, you can see the ball's projected movement
275
represented by the blue line. The length of that line (<TT
278
>) represents its speed, and the angle is the direction in which
279
it will move. The angle for the ball's movement will always be taken from the x axis on the right, and it is measured clockwise from
280
that line, as shown in the diagram.</P
282
>From the angle and speed of the ball, we can then work out how much it has moved along the x and y axes. We need to do this because
283
Pygame doesn't support vectors itself, and we can only move the ball by moving its rectangle along the two axes. So we need to
287
> the angle and speed into its movement on the x axis (dx) and on the y axis (dy). This is a simple matter of
288
trigonometry, and can be done with the formulae shown in the diagram.</P
290
>If you've studied elementary trigonometry before, none of this should be news to you. But just in case you're forgetful, here are some
291
useful formulae to remember, that will help you visualise the angles (I find it easier to visualise angles in degrees than in radians!) </P
296
SRC="formulae.png"></P
306
SUMMARY="Footer navigation table"
326
HREF="MakeGames.html"
345
>Kicking things off</TD
355
>User-controllable objects</TD