Game Development Reference
In-Depth Information
110. score = 0
The KEYUP event is created whenever the player stops pressing down on a keyboard key
and it returns to its normal, up position. KEYUP objects with a type of KEYUP also have a
key attribute just like KEYDOWN events.
On line 105, we check if the player has released the "z" key, which will deactivate the
reverse cheat. In that case, we set reverseCheat to False and reset the score to 0 . The
score reset is to discourage the player for using the cheats.
Lines 108 to 110 do the same thing for the "x" key and the slow cheat. When the "x" key
is released, slowCheat is set to False and the player's score is reset to 0 .
111. if event.key == K_ESCAPE:
112. terminate()
At any time during the game, the player can press the Esc key on the keyboard to quit the
game. Here we check if the key that was released was the Esc key by checking event.key
== K_ESCAPE . If so, we call our terminate() function which will exit the program.
114. if event.key == K_LEFT or event.key == ord
('a'):
115. moveLeft = False
116. if event.key == K_RIGHT or event.key ==
ord('d'):
117. moveRight = False
118. if event.key == K_UP or event.key == ord
('w'):
119. moveUp = False
120. if event.key == K_DOWN or event.key == ord
('s'):
121. moveDown = False
Lines 114 to 121 check if the player has stopped holding down one of the arrow keys (or
the corresponding WASD key). In that event, we will set the corresponding movement
variable to False . For example, if the player was holding down the left arrow key, then the
moveLeft would have been set to True on line 93. When they release it, the condition on
line 114 will evaluate to True , and the moveLeft variable will be set to False .
The move_ip() Method for Rect objects
123. if event.type == MOUSEMOTION:
124. # If the mouse moves, move the player
where the cursor is.
Search WWH ::




Custom Search