| 1 |
Event Hooks |
| 2 |
=========== |
| 3 |
|
| 4 |
Pygame Zero will automatically pick up and call event hooks that you define. |
| 5 |
This approach saves you from having to implement the event loop machinery |
| 6 |
yourself. |
| 7 |
|
| 8 |
Game Loop Hooks |
| 9 |
--------------- |
| 10 |
|
| 11 |
A typical game loop looks a bit like this:: |
| 12 |
|
| 13 |
while game_has_not_ended(): |
| 14 |
process_input() |
| 15 |
update() |
| 16 |
draw() |
| 17 |
|
| 18 |
Input processing is a bit more complicated, but Pygame Zero allows you to |
| 19 |
easily define the ``update()`` and ``draw()`` functions within your game |
| 20 |
module. |
| 21 |
|
| 22 |
.. function:: draw() |
| 23 |
|
| 24 |
Called by Pygame Zero when it needs to redraw your game window. |
| 25 |
|
| 26 |
``draw()`` must take no arguments. |
| 27 |
|
| 28 |
Pygame Zero attempts to work out when the game screen needs to be redrawn |
| 29 |
to avoid redrawing if nothing has changed. On each step of the game loop |
| 30 |
it will draw the screen in the following situations: |
| 31 |
|
| 32 |
* If you have defined an ``update()`` function (see below). |
| 33 |
* If a clock event fires. |
| 34 |
* If an input event has been triggered. |
| 35 |
|
| 36 |
One way this can catch you out is if you attempt to modify or animate |
| 37 |
something within the draw function. For example, this code is wrong: the |
| 38 |
alien is not guaranteed to continue moving across the screen:: |
| 39 |
|
| 40 |
def draw(): |
| 41 |
alien.left += 1 |
| 42 |
alien.draw() |
| 43 |
|
| 44 |
The correct code uses ``update()`` to modify or animate things and draw |
| 45 |
simply to paint the screen:: |
| 46 |
|
| 47 |
def draw(): |
| 48 |
alien.draw() |
| 49 |
|
| 50 |
def update(): |
| 51 |
alien.left += 1 |
| 52 |
|
| 53 |
.. function:: update() or update(dt) |
| 54 |
|
| 55 |
Called by Pygame Zero to step your game logic. This will be called |
| 56 |
repeatedly, 60 times a second. |
| 57 |
|
| 58 |
There are two different approaches to writing an update function. |
| 59 |
|
| 60 |
In simple games you can assume a small time step (a fraction of a second) |
| 61 |
has elapsed between each call to ``update()``. Perhaps you don't even care |
| 62 |
how big that time step is: you can just move objects by a fixed number of |
| 63 |
pixels per frame (or accelerate them by a fixed constant, etc.) |
| 64 |
|
| 65 |
A more advanced approach is to base your movement and physics calculations |
| 66 |
on the actual amount of time that has elapsed between calls. This can give |
| 67 |
smoother animation, but the calculations involved can be harder and you |
| 68 |
must take more care to avoid unpredictable behaviour when the time steps |
| 69 |
grow larger. |
| 70 |
|
| 71 |
To use a time-based approach, you can change the update function to take a |
| 72 |
single parameter. If your update function takes an argument, Pygame Zero |
| 73 |
will pass it the elapsed time in seconds. You can use this to scale your |
| 74 |
movement calculations. |
| 75 |
|
| 76 |
|
| 77 |
Event Handling Hooks |
| 78 |
-------------------- |
| 79 |
|
| 80 |
Similar to the game loop hooks, your Pygame Zero program can respond to input |
| 81 |
events by defining functions with specific names. |
| 82 |
|
| 83 |
Somewhat like in the case of ``update()``, Pygame Zero will inspect your |
| 84 |
event handler functions to determine how to call them. So you don't need to |
| 85 |
make your handler functions take arguments. For example, Pygame Zero will |
| 86 |
be happy to call any of these variations of an ``on_mouse_down`` function:: |
| 87 |
|
| 88 |
def on_mouse_down(): |
| 89 |
print("Mouse button clicked") |
| 90 |
|
| 91 |
def on_mouse_down(pos): |
| 92 |
print("Mouse button clicked at", pos) |
| 93 |
|
| 94 |
def on_mouse_down(button): |
| 95 |
print("Mouse button", button, "clicked") |
| 96 |
|
| 97 |
def on_mouse_down(pos, button): |
| 98 |
print("Mouse button", button, "clicked at", pos) |
| 99 |
|
| 100 |
It does this by looking at the names of the parameters, so they must be spelled |
| 101 |
exactly as above. Each event hook has a different set of parameters that you |
| 102 |
can use, as described below. |
| 103 |
|
| 104 |
.. function:: on_mouse_down([pos], [button]) |
| 105 |
|
| 106 |
Called when a mouse button is depressed. |
| 107 |
|
| 108 |
:param pos: A tuple (x, y) that gives the location of the mouse pointer |
| 109 |
when the button was pressed. |
| 110 |
:param button: A :class:`mouse` enum value indicating the button that was |
| 111 |
pressed. |
| 112 |
|
| 113 |
.. function:: on_mouse_up([pos], [button]) |
| 114 |
|
| 115 |
Called when a mouse button is released. |
| 116 |
|
| 117 |
:param pos: A tuple (x, y) that gives the location of the mouse pointer |
| 118 |
when the button was released. |
| 119 |
:param button: A :class:`mouse` enum value indicating the button that was |
| 120 |
released. |
| 121 |
|
| 122 |
.. function:: on_mouse_move([pos], [rel], [buttons]) |
| 123 |
|
| 124 |
Called when the mouse is moved. |
| 125 |
|
| 126 |
:param pos: A tuple (x, y) that gives the location that the mouse pointer |
| 127 |
moved to. |
| 128 |
:param rel: A tuple (delta_x, delta_y) that represent the change in the |
| 129 |
mouse pointer's position. |
| 130 |
:param buttons: A set of :class:`mouse` enum values indicating the buttons |
| 131 |
that were depressed during the move. |
| 132 |
|
| 133 |
|
| 134 |
To handle mouse drags, use code such as the following:: |
| 135 |
|
| 136 |
def on_mouse_move(rel, buttons): |
| 137 |
if mouse.LEFT in buttons: |
| 138 |
# the mouse was dragged, do something with `rel` |
| 139 |
... |
| 140 |
|
| 141 |
|
| 142 |
.. function:: on_key_down([key], [mod], [unicode]) |
| 143 |
|
| 144 |
Called when a key is depressed. |
| 145 |
|
| 146 |
:param key: An integer indicating the key that was pressed (see |
| 147 |
:ref:`below <buttons-and-keys>`). |
| 148 |
:param unicode: Where relevant, the character that was typed. Not all keys |
| 149 |
will result in printable characters - many may be control |
| 150 |
characters. In the event that a key doesn't correspond to |
| 151 |
a Unicode character, this will be the empty string. |
| 152 |
:param mod: A bitmask of modifier keys that were depressed. |
| 153 |
|
| 154 |
.. function:: on_key_up([key], [mod]) |
| 155 |
|
| 156 |
Called when a key is released. |
| 157 |
|
| 158 |
:param key: An integer indicating the key that was released (see |
| 159 |
:ref:`below <buttons-and-keys>`). |
| 160 |
:param mod: A bitmask of modifier keys that were depressed. |
| 161 |
|
| 162 |
|
| 163 |
.. function:: on_music_end() |
| 164 |
|
| 165 |
Called when a :ref:`music track <music>` finishes. |
| 166 |
|
| 167 |
Note that this will not be called if the track is configured to loop. |
| 168 |
|
| 169 |
|
| 170 |
.. _buttons-and-keys: |
| 171 |
|
| 172 |
Buttons and Keys |
| 173 |
'''''''''''''''' |
| 174 |
|
| 175 |
Built-in objects ``mouse`` and ``keys`` can be used to determine which buttons |
| 176 |
or keys were pressed in the above events. |
| 177 |
|
| 178 |
Note that mouse scrollwheel events appear as button presses with the below |
| 179 |
``WHEEL_UP``/``WHEEL_DOWN`` button constants. |
| 180 |
|
| 181 |
.. class:: mouse |
| 182 |
|
| 183 |
A built-in enumeration of buttons that can be received by the |
| 184 |
``on_mouse_*`` handlers. |
| 185 |
|
| 186 |
.. attribute:: LEFT |
| 187 |
.. attribute:: MIDDLE |
| 188 |
.. attribute:: RIGHT |
| 189 |
.. attribute:: WHEEL_UP |
| 190 |
.. attribute:: WHEEL_DOWN |
| 191 |
|
| 192 |
.. class:: keys |
| 193 |
|
| 194 |
A built-in enumeration of keys that can be received by the ``on_key_*`` |
| 195 |
handlers. |
| 196 |
|
| 197 |
.. attribute:: BACKSPACE |
| 198 |
.. attribute:: TAB |
| 199 |
.. attribute:: CLEAR |
| 200 |
.. attribute:: RETURN |
| 201 |
.. attribute:: PAUSE |
| 202 |
.. attribute:: ESCAPE |
| 203 |
.. attribute:: SPACE |
| 204 |
.. attribute:: EXCLAIM |
| 205 |
.. attribute:: QUOTEDBL |
| 206 |
.. attribute:: HASH |
| 207 |
.. attribute:: DOLLAR |
| 208 |
.. attribute:: AMPERSAND |
| 209 |
.. attribute:: QUOTE |
| 210 |
.. attribute:: LEFTPAREN |
| 211 |
.. attribute:: RIGHTPAREN |
| 212 |
.. attribute:: ASTERISK |
| 213 |
.. attribute:: PLUS |
| 214 |
.. attribute:: COMMA |
| 215 |
.. attribute:: MINUS |
| 216 |
.. attribute:: PERIOD |
| 217 |
.. attribute:: SLASH |
| 218 |
.. attribute:: K_0 |
| 219 |
.. attribute:: K_1 |
| 220 |
.. attribute:: K_2 |
| 221 |
.. attribute:: K_3 |
| 222 |
.. attribute:: K_4 |
| 223 |
.. attribute:: K_5 |
| 224 |
.. attribute:: K_6 |
| 225 |
.. attribute:: K_7 |
| 226 |
.. attribute:: K_8 |
| 227 |
.. attribute:: K_9 |
| 228 |
.. attribute:: COLON |
| 229 |
.. attribute:: SEMICOLON |
| 230 |
.. attribute:: LESS |
| 231 |
.. attribute:: EQUALS |
| 232 |
.. attribute:: GREATER |
| 233 |
.. attribute:: QUESTION |
| 234 |
.. attribute:: AT |
| 235 |
.. attribute:: LEFTBRACKET |
| 236 |
.. attribute:: BACKSLASH |
| 237 |
.. attribute:: RIGHTBRACKET |
| 238 |
.. attribute:: CARET |
| 239 |
.. attribute:: UNDERSCORE |
| 240 |
.. attribute:: BACKQUOTE |
| 241 |
.. attribute:: A |
| 242 |
.. attribute:: B |
| 243 |
.. attribute:: C |
| 244 |
.. attribute:: D |
| 245 |
.. attribute:: E |
| 246 |
.. attribute:: F |
| 247 |
.. attribute:: G |
| 248 |
.. attribute:: H |
| 249 |
.. attribute:: I |
| 250 |
.. attribute:: J |
| 251 |
.. attribute:: K |
| 252 |
.. attribute:: L |
| 253 |
.. attribute:: M |
| 254 |
.. attribute:: N |
| 255 |
.. attribute:: O |
| 256 |
.. attribute:: P |
| 257 |
.. attribute:: Q |
| 258 |
.. attribute:: R |
| 259 |
.. attribute:: S |
| 260 |
.. attribute:: T |
| 261 |
.. attribute:: U |
| 262 |
.. attribute:: V |
| 263 |
.. attribute:: W |
| 264 |
.. attribute:: X |
| 265 |
.. attribute:: Y |
| 266 |
.. attribute:: Z |
| 267 |
.. attribute:: DELETE |
| 268 |
.. attribute:: KP0 |
| 269 |
.. attribute:: KP1 |
| 270 |
.. attribute:: KP2 |
| 271 |
.. attribute:: KP3 |
| 272 |
.. attribute:: KP4 |
| 273 |
.. attribute:: KP5 |
| 274 |
.. attribute:: KP6 |
| 275 |
.. attribute:: KP7 |
| 276 |
.. attribute:: KP8 |
| 277 |
.. attribute:: KP9 |
| 278 |
.. attribute:: KP_PERIOD |
| 279 |
.. attribute:: KP_DIVIDE |
| 280 |
.. attribute:: KP_MULTIPLY |
| 281 |
.. attribute:: KP_MINUS |
| 282 |
.. attribute:: KP_PLUS |
| 283 |
.. attribute:: KP_ENTER |
| 284 |
.. attribute:: KP_EQUALS |
| 285 |
.. attribute:: UP |
| 286 |
.. attribute:: DOWN |
| 287 |
.. attribute:: RIGHT |
| 288 |
.. attribute:: LEFT |
| 289 |
.. attribute:: INSERT |
| 290 |
.. attribute:: HOME |
| 291 |
.. attribute:: END |
| 292 |
.. attribute:: PAGEUP |
| 293 |
.. attribute:: PAGEDOWN |
| 294 |
.. attribute:: F1 |
| 295 |
.. attribute:: F2 |
| 296 |
.. attribute:: F3 |
| 297 |
.. attribute:: F4 |
| 298 |
.. attribute:: F5 |
| 299 |
.. attribute:: F6 |
| 300 |
.. attribute:: F7 |
| 301 |
.. attribute:: F8 |
| 302 |
.. attribute:: F9 |
| 303 |
.. attribute:: F10 |
| 304 |
.. attribute:: F11 |
| 305 |
.. attribute:: F12 |
| 306 |
.. attribute:: F13 |
| 307 |
.. attribute:: F14 |
| 308 |
.. attribute:: F15 |
| 309 |
.. attribute:: NUMLOCK |
| 310 |
.. attribute:: CAPSLOCK |
| 311 |
.. attribute:: SCROLLOCK |
| 312 |
.. attribute:: RSHIFT |
| 313 |
.. attribute:: LSHIFT |
| 314 |
.. attribute:: RCTRL |
| 315 |
.. attribute:: LCTRL |
| 316 |
.. attribute:: RALT |
| 317 |
.. attribute:: LALT |
| 318 |
.. attribute:: RMETA |
| 319 |
.. attribute:: LMETA |
| 320 |
.. attribute:: LSUPER |
| 321 |
.. attribute:: RSUPER |
| 322 |
.. attribute:: MODE |
| 323 |
.. attribute:: HELP |
| 324 |
.. attribute:: PRINT |
| 325 |
.. attribute:: SYSREQ |
| 326 |
.. attribute:: BREAK |
| 327 |
.. attribute:: MENU |
| 328 |
.. attribute:: POWER |
| 329 |
.. attribute:: EURO |
| 330 |
.. attribute:: LAST |
| 331 |
|
| 332 |
Additionally you can access a set of constants that represent modifier keys: |
| 333 |
|
| 334 |
.. class:: keymods |
| 335 |
|
| 336 |
Constants representing modifier keys that may have been depressed during |
| 337 |
an ``on_key_up``/``on_key_down`` event. |
| 338 |
|
| 339 |
.. attribute:: LSHIFT |
| 340 |
.. attribute:: RSHIFT |
| 341 |
.. attribute:: SHIFT |
| 342 |
.. attribute:: LCTRL |
| 343 |
.. attribute:: RCTRL |
| 344 |
.. attribute:: CTRL |
| 345 |
.. attribute:: LALT |
| 346 |
.. attribute:: RALT |
| 347 |
.. attribute:: ALT |
| 348 |
.. attribute:: LMETA |
| 349 |
.. attribute:: RMETA |
| 350 |
.. attribute:: META |
| 351 |
.. attribute:: NUM |
| 352 |
.. attribute:: CAPS |
| 353 |
.. attribute:: MODE |
| 354 |
|