Game Development Reference
In-Depth Information
BADDIEMINSPEED and BADDIEMAXSPEED pixels per iteration through the game
loop. And a new baddie will be added to the top of the window every
ADDNEWBADDIERATE iterations through the game loop.
14. PLAYERMOVERATE = 5
The PLAYERMOVERATE will store the number of pixels the player's character moves in
the window on each iteration through the game loop (if the character is moving). By
increasing this number, you can increase the speed the character moves. If you set
PLAYERMOVERATE to 0 , then the player's character won't be able to move at all (the
player would move 0 pixels per iteration). This wouldn't be a very fun game.
Defining Functions
We will create several functions for our game. By putting code into functions, we can
avoid having to type the same code several times in our program. And because the code is
in one place, if we find a bug the code only needs to be fixed in one place.
16. def terminate():
17. pygame.quit()
18. sys.exit()
There are several places in our game that we want to terminate the program. In our other
programs, this just required a single call to sys.exit() . But since Pygame requires that
we call both pygame.quit() and sys.exit() , we will put them into a function
called terminate() and just call the function. This keeps us from repeating the same
code over and over again. And remember, the more we type, the more likely we will make
a mistake and create a bug in our program.
20. def waitForPlayerToPressKey():
21. while True:
22. for event in pygame.event.get():
There are also a couple places where we want the game to pause and wait for the player
to press a key. We will create a new function called waitForPlayerToPressKey()
to do this. Inside this function, we have an infinite loop that only breaks when a KEYDOWN
or QUIT event is received. At the start of the loop, we call pygame.event.get() to
return a list of Event objects to check out.
23. if event.type == QUIT:
24. terminate()
Search WWH ::




Custom Search