Game Development Reference
In-Depth Information
18 - Collision Detection and Input
109. pygame.display.update()
110. mainClock.tick(40)
This program looks identical to the collision detection program earlier in this chapter. But
in this program, the bouncer only moves around when we hold down keys on the keyboard.
Holding down the "W" key moves the bouncer up. The "A" key moves the bouncer to the
left and the "D" key moves the bouncer to the right. The "S" key moves the bouncer down.
You can also move the bouncer by holding down the arrow keys on the keyboard. The user
can also use the keyboard's arrow keys.
We can also click anywhere in the GUI window and create new food objects at the
coordinates where we clicked. In addition, the ESC key will quit the program and the "X"
key will teleport the bouncer to a random place on the screen.
Setting Up the Window and Data Structures
First, we set the caption of the window's title bar to the string to 'Mouse' on line 12. We
set the caption of the window with a call to pygame.display.set_caption() the
same way as we did in our previous Pygame programs. Next we want to set up some
variables that track the movement of the bouncer.
28. # set up movement variables
29. moveLeft = False
30. moveRight = False
31. moveUp = False
32. moveDown = False
We are going to use four different boolean variables to keep track of which of the arrow
keys are being held down. For example, when the user pushes the left arrow key on her
keyboard, we will set the moveLeft variable to True . When she lets go of the key, we will
set the moveLeft variable back to False . The "W" key affects the moveUp variable, the
"S" key affects the moveDown variable, and the "D" key affects the moveRight variable
in a similar way.
Lines 34 to 43 are identical to code in the previous Pygame programs. These lines handle
the start of the game loop and handling what to do when the user wants to quit the program.
We will skip the explanation for this code here since we have already covered it in the last
chapter.
Search WWH ::




Custom Search