Pausing a game isn't that hard of a concept. We want to draw everything to the screen, we just don't update their positions so the objects appear to remain in a frozen state. We will control the pausing and unpausing of the game with the spacebar. It's nice to be able to pause the game, especially when we're developing it. From the player viewpoint, it's nice to pause in case you need a quick break or something distracting is happening. The original Pacman game didn't have a pause, but players expect to be able to pause their game at any point nowadays.
The following is a list of events that make the game pause:
We'll be able to take care of 1-3 above and then 4 and 5 in a later section.
We're going to make a class, because why not? There is a paused variable which can be True or False. There's also a timer and a pauseTime. We have those because we may (and do) want to have the functionality to only pause for a specific period of time. Like I mentioned above, there's several cases that we need that for. Normally pauseTime will be None which means that there is no time limit for a pause like when the player presses the space bar. It will stay paused forever or until the player presses the space bar again. The func variable is interesting, albeit not very descriptive. That's my fault. Normally it's None, but there may (will) be a time when we want a certain method to run after a pause is finished. So this will just hold a reference to any method.
The update method is just there for the pauses that do have a pauseTime. It's just your standard timer we've seen several times already. Notice that when the pause is finished we return the method (if any) to the calling object.
The setPause method is what we'll be calling for our various pausing needs. It's good to know if this is the result of the player pausing the game or an in-game pause. Also, if there is a function we want to run after the game is unpaused, then we pass that in as well.
The flip method does just that, it flips the paused value. If it's True, then it gets flipped to False. If it is False, then it gets flipped to True.
So we import the new class and make an object out of it. We set the initial pause to True. That way when we start the game, the game will already be paused. When we're ready to play the game we will press the space bar to unpause the game. In the checkEvents method we added the lines where we check to see if the space bar was pressed. If it was then we just call the setPause method and let it know that it was the player who paused the game. The game will either pause or unpause depending on the current state.
In the update method we need to check if the game is paused before we update anything in the game. If the game is paused, then we don't want to do anything other than draw things to the screen and check for the basic events which are closing the game and unpausing the game.
In the checkGhostEvents method when we find out that a ghost has been eaten, we will pause the game for 1 second.
We'll finally add something else to this method now. Here we're just going to check if the user ever presses the space bar. If he does, then we'll simply either pause or unpause the game depending on the current game state. Also, when we pause the game we'll hide all of the entities. When we unpause it we show them again.
We'll add the two methods which will either hide or show the entities when we need to. Also, when Pacman collides with a ghost who is in FREIGHT mode then we temporarily hide both Pacman and the ghost he is colliding with. After the pause time of 1 second, we'll show them again.
Play the game now and you'll notice that when the game starts nothing is moving. That's a good thing. Everything is in their initial positions. Press the spacebar to start the game. Press it again to pause the game. Eat a power pellet and then eat a ghost. The game should pause for 1 second before continuing. Also, when pausing with the spacebar, Pacman and the ghosts should disappear. When Pacman eats a ghost only him and the ghost he's eating should disappear during the pause and then return when the pause is over.