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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>User-controllable objects</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
HREF="MakeGames.html"><LINK
REL="PREVIOUS"
TITLE="Game object classes"
HREF="games4.html"><LINK
REL="NEXT"
TITLE="Putting it all together"
HREF="games6.html"> <style type="text/stylesheet">
<!--
PRE.PROGRAMLISTING { background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin } -->
</style></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
>
<DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="games4.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="games6.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN158"
></A
>5. User-controllable objects</H1
><P
>So far you can create a Pygame window, and render a ball that will fly across the screen. The next step is to make some bats which
the user can control. This is potentially far more simple than the ball, because it requires no physics (unless your user-controlled
object will move in ways more complex than up and down, e.g. a platform character like Mario, in which case you'll need more physics).
User-controllable objects are pretty easy to create, thanks to Pygame's event queue system, as you'll see.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN161"
></A
>5.1. A simple bat class</H2
><P
>The principle behind the bat class is similar to that of the ball class. You need an <TT
CLASS="FUNCTION"
>__init__</TT
> function to initialise the
ball (so you can create object instances for each bat), an <TT
CLASS="FUNCTION"
>update</TT
> function to perform per-frame changes on the bat before
it is blitted the bat to the screen, and the functions that will define what this class will actually do. Here's some sample code:</P
><PRE
CLASS="PROGRAMLISTING"
>class Bat(pygame.sprite.Sprite):
"""Movable tennis 'bat' with which one hits the ball
Returns: bat object
Functions: reinit, update, moveup, movedown
Attributes: which, speed"""
def __init__(self, side):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_png('bat.png')
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.side = side
self.speed = 10
self.state = "still"
self.reinit()
def reinit(self):
self.state = "still"
self.movepos = [0,0]
if self.side == "left":
self.rect.midleft = self.area.midleft
elif self.side == "right":
self.rect.midright = self.area.midright
def update(self):
newpos = self.rect.move(self.movepos)
if self.area.contains(newpos):
self.rect = newpos
pygame.event.pump()
def moveup(self):
self.movepos[1] = self.movepos[1] - (self.speed)
self.state = "moveup"
def movedown(self):
self.movepos[1] = self.movepos[1] + (self.speed)
self.state = "movedown"</PRE
><P
>As you can see, this class is very similar to the ball class in its structure. But there are differences in what each function does.
First of all, there is a reinit function, which is used when a round ends, and the bat needs to be set back in its starting place,
with any attributes set back to their necessary values. Next, the way in which the bat is moved is a little more complex than with the
ball, because here its movement is simple (up/down), but it relies on the user telling it to move, unlike the ball which just keeps
moving in every frame. To make sense of how the ball moves, it is helpful to look at a quick diagram to show the sequence of events:</P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="event-flowchart.png"></P
></DIV
><P
>What happens here is that the person controlling the bat pushes down on the key that moves the bat up. For each iteration of the main
game loop (for every frame), if the key is still held down, then the <TT
CLASS="FUNCTION"
>state</TT
> attribute of that bat object will be set to
"moving", and the <TT
CLASS="FUNCTION"
>moveup</TT
> function will be called, causing the ball's y position to be reduced by the value of the
<TT
CLASS="FUNCTION"
>speed</TT
> attribute (in this example, 10). In other words, so long as the key is held down, the bat will move up the screen
by 10 pixels per frame. The <TT
CLASS="FUNCTION"
>state</TT
> attribute isn't used here yet, but it's useful to know if you're dealing with spin, or
would like some useful debugging output.</P
><P
>As soon as the player lets go of that key, the second set of boxes is invoked, and the <TT
CLASS="FUNCTION"
>state</TT
> attribute of the bat object
will be set back to "still", and the <TT
CLASS="FUNCTION"
>movepos</TT
> attribute will be set back to [0,0], meaning that when the <TT
CLASS="FUNCTION"
>update</TT
> function is called, it won't move the bat any more. So when the player lets go of the key, the bat stops moving. Simple!</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN180"
></A
>5.1.1. Diversion 3: Pygame events</H3
><P
>So how do we know when the player is pushing keys down, and then releasing them? With the Pygame event queue system, dummy! It's a
really easy system to use and understand, so this shouldn't take long :) You've already seen the event queue in action in the basic
Pygame program, where it was used to check if the user was quitting the application. The code for moving the bat is about as simple
as that:</P
><PRE
CLASS="PROGRAMLISTING"
>for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_UP:
player.moveup()
if event.key == K_DOWN:
player.movedown()
elif event.type == KEYUP:
if event.key == K_UP or event.key == K_DOWN:
player.movepos = [0,0]
player.state = "still"</PRE
><P
>Here assume that you've already created an instance of a bat, and called the object <TT
CLASS="FUNCTION"
>player</TT
>. You can see the familiar
layout of the <TT
CLASS="FUNCTION"
>for</TT
> structure, which iterates through each event found in the Pygame event queue, which is retrieved with
the <TT
CLASS="FUNCTION"
>event.get()</TT
> function. As the user hits keys, pushes mouse buttons and moves the joystick about, those actions are
pumped into the Pygame event queue, and left there until dealt with. So in each iteration of the main game loop, you go through
these events, checking if they're ones you want to deal with, and then dealing with them appropriately. The <TT
CLASS="FUNCTION"
>event.pump()</TT
>
function that was in the <TT
CLASS="FUNCTION"
>Bat.update</TT
> function is then called in every iteration to pump out old events, and keep the queue
current.</P
><P
>First we check if the user is quitting the program, and quit it if they are. Then we check if any keys are being pushed down, and if
they are, we check if they're the designated keys for moving the bat up and down. If they are, then we call the appropriate moving
function, and set the player state appropriately (though the states moveup and movedown and changed in the <TT
CLASS="FUNCTION"
>moveup()</TT
> and
<TT
CLASS="FUNCTION"
>movedown()</TT
> functions, which makes for neater code, and doesn't break <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>encapsulation</I
></SPAN
>, which means that you
assign attributes to the object itself, without referring to the name of the instance of that object). Notice here we have three
states: still, moveup, and movedown. Again, these come in handy if you want to debug or calculate spin. We also check if any keys have
been "let go" (i.e. are no longer being held down), and again if they're the right keys, we stop the bat from moving.</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"
><A
HREF="games4.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="MakeGames.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="games6.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Game object classes</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
> </TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Putting it all together</TD
></TR
></TABLE
>
</body>
</html>
</DIV
></BODY
></HTML
>
|