Game Development Reference
In-Depth Information
Line 151 moves the mouse cursor to the same position as the player's character. The
pygame.mouse.set_pos() function moves the mouse cursor to the X and Y
coordinates that you pass it. Specifically, the cursor will be right in the middle of the
character's Rect object because we pass the centerx and centery attributes of
playerRect for the coordinates. The mouse cursor still exists and can be moved, even
though it is invisible because we called pygame.mouse.set_visible(False) on
line 47.
The reason we want the mouse cursor to match the location of the player's character is to
avoid sudden jumps. Imagine that the mouse cursor and the player's character are at the
same location on the left side of the window. When the player holds down the right arrow
key, the character moves to the right edge of the window but the mouse cursor would stay
at the left edge of the screen. If the player then moves the mouse just a little bit, the player's
character would immediately jump to the location of the mouse cursor on the left edge of
the screen. By moving the mouse cursor along with the player's character, any mouse
movements would not result in a sudden jump across the window.
153. # Move the baddies down.
154. for b in baddies:
Now we want to loop through each baddie data structure in the baddies list to move
them down a little.
155. if not reverseCheat and not slowCheat:
156. b['rect'].move_ip(0, b['speed'])
If neither of the cheats have been activated (by the player pushing the "z" or "x" keys
which sets reverseCheat or slowCheat to True , respectively), then move the
baddie's location down a number of pixels equal to its speed, which is stored in the
'speed' key.
Implementing the Cheat Codes
157. elif reverseCheat:
158. b['rect'].move_ip(0, -5)
If the reverse cheat has been activated, then the baddie should actually be moved up by
five pixels. Passing -5 for the second argument to move_ip() will move the Rect
object upwards by five pixels.
159. elif slowCheat:
160. b['rect'].move_ip(0, 1)
Search WWH ::




Custom Search