We have all of the Pacman animations defined, except for his death sequence. Initially, you would think that it would just be a simple matter of adding a new animation to the PacmanSprites class. We'll do that, but there's more that we need to do to make sure everything looks right.
Before we can add the new death sequence animation we need to update the Animator class. The main reason is because unlike the other animations, the death animation does not loop when it reaches the end of the animation. So I want the animation to just stop when it reaches the end. We can do that in several ways, but what I'm doing here is adding a new input variable to the Animator class called 'loop'. This is True be default since all of our animations at this point loop. But for the death animation we'll set it to False. We also want to know when the death animation has finished so we'll add a 'finished' variable and set it to False initially.
We'll also add a new method called 'reset'. I'll show you in a bit why we'll need this. This just sets the current frame back to 0 and finished back to False.
In the update method we'll modify it so that we only ask for the next animation frame if the animation is not finished. Then when we detect that we've reached the last frame in the animation we check to see if it's supposed to loop or not using the new 'loop' variable. If it is supposed to loop like all of the animations at this point then we do what we were doing before and set the current frame back to 0. However, if it's not supposed to loop like in the death animation then we'll set finished to True and subtract 1 from the current frame. The reason we subtract 1 is because we would get an out-of-bounds error if we didn't. Let's say our animation was 5 frames long. The inidices that refer to those frames are 0, 1, 2, 3, and 4. Index 4 is the last frame, it refers to the 5th frame. At this point in the code our current_frame would be 5 because we're asking "when does the current frame equal the total number of frames in the animation?" When we're looping we just set it back to 0, but if we're not looping we need to set it back 1 which just puts it back onto the last frame in the animation.
Lets add a DEATH constant at the top of the file where we added the other two constants. Then we can create a DEATH animation. We'll set the death animation speed to 6 which means that each frame will take 1/6th of a second. Then we set the loop to False as discussed above.
Then in the update we'll check to see if Pacman is alive or not. (We'll get to that in a second). If he's alive, then we do the normal animations that we defined previously. If not, then we show the death animation.
Then there's a reset method which we'll use to reset all of the animations. We'll call this when we need to reset a level, for example.
First off, in the Entity class we'll add a new variable called 'alive.' It's True be default, but we'll set it to False if an Entity dies. The only Entity that can die is Pacman though. But I'll place it here in case we need some other Entity to die as well.
Then in the Pacman class when we reset Pacman, we need to also reset back to his starting image and reset the sprites animations to their initial conditions.
Then, we'll create a new method called 'die', which just sets alive to False and makes Pacman STOP.
In the checkGhostEvents method when we detect that Pacman should die we will call his die method and then also hide the ghosts. We do that because we want to see Pacman's death sequence play out. He dies when he overlaps a ghost so we just hide all the ghosts for simplicity sake. It's also what the original Pacman game does as well.
If you try playing the game at this point then you'll be disappointed because you won't see the death animation. Why is that you ask? Well, because when Pacman dies we paused the game for 3 seconds before restarting the level. That means Pacman's update is paused as well. So we need to take Pacman out of the section as shown below and put him into his own condition where we first check to see if he's alive or not. If he is alive then he'll pause and unpause just like before. However, if he is not alive then he does not pause with the rest of the entities.