So in the previous section we were able to get all of the pellets drawn on the maze. In this section we'll convince Pacman that he needs to eat these pellets.
Eating the pellets is really easy. During each Pacman update we just need to check if he has collided with any of the pellets. To do that we do what is called a circle to circle collision check. A circle to circle collision check is probably one of the easiest and fastest collision checks out there. Let's say you have circle A and circle B. Circle A has radius RA and circle B has radius RB. If the sum of their radii or RA + RB = D where D is the actual distance from circle A to circle B as measured from their centers, then you know that the circles are touching. If RA + RB < D, then the circles can't be touching. If RA + RB > D, then you know that the circles are overlapping, therefore a collision is occurring.
For our purposes, circle A is Pacman and circle B is any of the pellets. We just loop through all of the pellets until we find one that collides with Pacman. If we do find a pellet that collides with Pacman, then we just remove it. We probably want to do a few other things as well, like increase the player's score. Later on we'll also check to see if the pellet Pacman ate was a power pellet, which allows us to eat the ghosts. But let's not get ahead of ourselves, we don't even have ghosts yet.
Circle A has a radius of RA. Circle B has a radius of RB.
Distance D is the actual distance between the two circles. If the distance D is greater than the sum of the circle's radii, then the circles can't be colliding. RA + RB < D.
If the the distance D is less than or equal to the sum of the circle's radii, then the circles are colliding. RA + RB >= D.
In the init method we're going to add this variable to define Pacman's collision radius. The collision radius can be the same as Pacman's actual physical radius, but it looks better if we shrink the collision radius a bit so it looks like the pellet is being ingested instead of just disappearing as soon as Pacman touches it.
We're going to create a new method that takes the pellet list and loops through each pellet until we find one that Pacman is colliding with. If we find a pellet that he is colliding with, then we just return that pellet. If Pacman isn't colliding with any pellets, we simply return None.
Notice that we are comparing the square of the distances rather than the actual distances. This is to avoid taking the square root which is an expensive operation. Comparing the square of the distances is just as valid and faster.
We will create a new method that handles all of the pellet events. In this case we're sending the whole pellet list to Pacman and he returns the pellet (if any) that he is colliding with. If the pellet variable is anything other than None, then we just remove that pellet from the list.
In the update method we'll call this new method right after we update the pellets.
Run the code now and you'll see that when you collide with the pellets, the pellets disappear.