It's funny we've gotten this far and so far haven't discussed what happens when Pacman eats all of the pellets. Right now nothing will happen. He'll just continue on his merry way in a pellet-less world. We need to detect when all of the pellets have been eaten and if so start the level over again. In the original Pacman game you just keep playing the same level over and over and over and over and ... over again. The only difference between the levels were the ghosts got slightly faster, the ghosts were in FREIGHT mode not as long, and different fruit.
That's fine and that's what we'll do for the most part. But we'll want to include other mazes as well instead of just replaying the same maze over and over again. We won't worry about other mazes yet, but just know that I will show you how to include all the mazes you want, just later. For now we'll detect when a level has been cleared and just start the level over again with all of the pellets replaced.
We'll introduce a variable called 'level' which will keep track of what level we're on. It start with 1 and will increment from there. In the future this will determine which maze to use. But for now we only have 1 maze.
The new method "nextLevel" just increments the level, pauses the game, and starts the game again. We'll return to this method in the future, but for now it's fairly simple.
In the checkPelletEvents method when it has been detected that we just ate the last pellet, we'll pause the game for 3 seconds. When that pause time has finished, we want to call the nextLevel method which we defined above. That will advance us to the next level.
Go ahead and play the game again and make sure to eat all of the pellets. Don't worry about the ghosts, they can't hurt you yet. But eating all of the pellets will pause the game for a few seconds and then start all over. At least we can progress now.