Game Development Reference
In-Depth Information
20 - Dodger
125. playerRect.move_ip(event.pos[0] -
playerRect.centerx, event.pos[1] - playerRect.centery)
Now that we have handled the keyboard events, let's handle any mouse events that may
have been generated. In the Dodger game we don't do anything if the player has clicked a
mouse button, but the game does respond when the player moves the mouse. This gives the
player two ways of controlling the player character in the game: the keyboard and the
mouse.
If the event's type is MOUSEMOTION , then we want to move the player's character to the
location of the mouse cursor. The MOUSEMOTION event is generated whenever the mouse is
moved. Event objects with a type of MOUSEMOTION also have an attribute named pos .
The pos attribute stores a tuple of the X and Y coordinates of where the mouse cursor
moved in the window.
The move_ip() method for Rect objects will move the location of the Rect object
horizontally or vertically by a number of pixels. For example, playerRect.move_ip
(10, 20) would move the Rect object 10 pixels to the right and 20 pixels down. To
move the Rect object left or up, pass negative values. For example,
playerRect.move_ip(-5, -15) will move the Rect object left by 5 pixels and up
15 pixels.
The "ip" at the end of move_ip() stands for "in place". This is because the method
changes the Rect object itself, in its own place. There is also a move() method which does
not change the Rect object, but instead creates a new Rect object that has the new
location. This is useful if you want to keep the original Rect object's location the same but
also have a Rect object with the new location.
Adding New Baddies
127. # Add new baddies at the top of the screen, if
needed.
128. if not reverseCheat and not slowCheat:
129. baddieAddCounter += 1
On each iteration of the game loop, we want to increment the baddieAddCounter
variable by one. However, we only want to do this if the cheats are not enabled. Remember
that reverseCheat and slowCheat : are only set to True as long as the "z" and "x"
keys are being held down, respectively. And while those keys are being held down,
baddieAddCounter is not incremented. This means that no new baddies will appear at
the top of the screen.
130. if baddieAddCounter == ADDNEWBADDIERATE:
131. baddieAddCounter = 0
Search WWH ::




Custom Search