It wouldn't be a Pacman game if Pacman couldn't eat the ghosts. After they have been eaten, the ghosts need to be able to respawn or it would be a pretty boring game if there was permadeath. This section is all about giving Pacman a fighting chance. Since he's able to eat the pellets he can now eat the power pellets and chase the ghosts around who are now trying to get away from him. We'll start this section off by introducing the FREIGHT mode which leaves the ghosts vulnerable to being eaten by Pacman.
The ghosts currently have two modes that they can be in: CHASE or SCATTER. These modes have timers and switch back and forth when their timers expire. The ghost gets this from the MainMode object we defined earlier.
FREIGHT mode, however, is a mode that can happen at any time. Well, it can only happen four times during a level since there are only four power pellets. This is what I like to call an interrupt mode. When Pacman eats a power pellet the ghosts can be in either SCATTER or CHASE mode. When this happens we force all of the ghosts into FREIGHT mode whether they are in SCATTER or CHASE mode. FREIGHT mode also has a timer and initially only lasts for 7 seconds. This timer decreases as the levels increase to the point where it's practically non-existent. In our game though, it will never drop below 1 second. When the timer for FREIGHT mode expires the ghosts will go back to either CHASE or SCATTER mode depending on which mode the MainMode object is in.
Also, in FREIGHT mode, the ghosts move a lot slower. They move about 50% of their maximum speed and they choose directions randomly so they don't have a particular goal that they're trying to reach. It's almost as if they are dazed and confused.
In this section we'll send the ghosts into FREIGHT mode by eating a power pellet, but we still can't eat the ghosts. We'll have to take care of another mode before we can do that.
We're first going to modify the checkPelletEvents in run.py. We're already looping through each pellet to see which one was just eaten. When we find that pellet we remove it. Now we can ask if that pellet was a power pellet. If it was then we can start the freight mode for the ghost.
This new method will get called only when the ghost enters FREIGHT mode. It first makes sure that it is in either SCATTER or CHASE mode. If so, then we reset the timer to 0 and set the time to 7 seconds.
If, however, we are already in FREIGHT mode (which can happen if pacman eats 2 power pellets within 7 seconds), then just reset the timer to 0.
Make sure to place this in the ModeController class.
We'll need to update the mode controller's update method so that we can keep track of how long we are in FREIGHT mode. Once FREIGHT mode is over we'll go back to either SCATTER or CHASE.
We'll create 2 new methods that define various things about the FREIGHT mode and a normal mode. We call the setFreightMode that we just defined above and if the result of that puts us into FREIGHT mode (there are some cases we'll look at shortly that won't put us into FREIGHT mode), then we reduce the ghosts speed and make it move around randomly.
Normal mode just resets things back to normal, how the ghost normally moves around the maze at the normal speed.
When running the game now you'll notice that not only will the ghost switch between SCATTER and CHASE modes, but if you eat a power pellet, he'll slow down and move around randomly. Then he'll go back to either SCATTER or CHASE when that's finished.