Game Development Reference
In-Depth Information
18 - Collision Detection and Input
When you run this code, this is what the program looks like. The white square (the
bouncer) will bounce around the window, and when it collides with the green squares (the
food) will disappear from the screen.
Figure 18-1: The Collision Detection program.
Importing the Modules
1. import pygame, sys, random
2. from pygame.locals import *
The collision detection program imports the same things as the Animation program in the
last chapter, along with the random module.
The Collision Detection Function
4. def doRectsOverlap(rect1, rect2):
In order to do collision detection, we will need a function that can determine if two
rectangles intersect each other or not. Here is a picture of intersecting rectangles (on the
left) and rectangles that do not intersect (on the right):
Search WWH ::




Custom Search