Game Development Reference
In-Depth Information
Topics Covered In This Chapter:
— Collision Detection
— Don't Modify a List While Iterating Over It
— Keyboard Input in Pygame
— Mouse Input in Pygame
A very common behavior in most graphical games is collision detection. Collision
detection is figuring when two things on the screen have touched (that is, collided with)
each other. This is used very often in computer games. For example, if the player touches
an enemy they may lose health or a game life. Or we may want to know when the player
has touched a coin so that they automatically pick it up. Collision detection can help
determine if the game character is standing on solid ground, or if there is nothing but empty
air underneath them. In our games, collision detection is determining if two rectangles are
overlapping each other or not. Our next example program will cover this basic technique.
Later in this chapter, we will look at how our Pygame programs can accept input from
the user through the keyboard and the mouse. It's a bit more complicated than calling the
input() function like we did for our text programs. But using the keyboard is much more
interactive in GUI programs, and using the mouse isn't even possible in our text games.
Knowing these two concepts will make our games more advanced and exciting!
The Collision Detection Program's Source Code
Much of this code is similar to the animation program, so we will skip over explaining
how to make the bouncer move and bounce off of the walls. (See the animation program in
Search WWH ::




Custom Search