Game Development Reference
In-Depth Information
('a') , we make the left arrow key and the A key do the same thing. You may notice
that the W, A, S, and D keys are all used as alternates for changing the movement variables.
This is because some people may want to use their left hand to press the WASD keys
instead of their right hand to press the arrow keys. Our program offers them both!
Handling the KEYUP Event
58. if event.type == KEYUP:
When the user releases the key that they are holding down, a KEYUP event is generated.
59. if event.key == K_ESCAPE:
60. pygame.quit()
61. sys.exit()
If the key that the user released was the Esc key, then we want to terminate the program.
Remember, in Pygame you must call the pygame.quit() function before calling the
sys.exit() function. We want to do this when the user releases the Esc key, not when
they first Esc key down.
Lines 62 to 69 will set a movement variable to False if that direction's key was let go.
62. if event.key == K_LEFT or event.key == ord
('a'):
63. moveLeft = False
64. if event.key == K_RIGHT or event.key == ord
('d'):
65. moveRight = False
66. if event.key == K_UP or event.key == ord
('w'):
67. moveUp = False
68. if event.key == K_DOWN or event.key == ord
('s'):
69. moveDown = False
Teleporting the Player
If the user released one of the keys that moves the player, then we want to set the
movement variable that corresponds with the key to False . This will tell the later parts of
our program to no longer move the player's square on the screen.
70. if event.key == ord('x'):
71. player.top = random.randint(0,
WINDOWHEIGHT - player.height)
Search WWH ::




Custom Search