So we have actual images representing Pacman and the ghosts instead of the circles that we had previously. But we are only using one image for each object. So they are just static images moving around the maze. During the game Pacman's mouth is moving and he also points in the direction he is moving. The ghosts also have different images depending on which direction they are moving. The only difference in their images is which way their eyes are looking. They also have some animation to them. Pacman is a simple game, so that's all the animation there is. But, once we learn how to do it for Pacman, then it will be easier to apply that knowledge to other more complex animations.
This section is all about writing the animation code, then we'll apply it to the Pacman and the Ghost objects in the next section. You don't need to read any books on animation to understand this section. As long as you understand that I'm defining animation as a series of images then you'll be fine.
I think we've all done some kind of animation in the past, or at the very least we know the very basics of how to animate something. As a kid we would have little flip books that we would use to draw little stick figures on each page. Each page represented one animation frame. As you flipped the book you would see the stick figure walk or something. The faster you flipped the book, the faster the animation. The smoothness of the animation depended on how many images you drew depicting a certain motion. The more images you use and the smaller the change between each image, the smoother the animation.
The Animator class is fairly simple. It just takes a list of (x,y) pair data which represents the location on the spritesheet of the sprite you need and stores those as the frames. We set the current frame to be the first frame in that list. We set the speed of the animation that we want. We're actually going to use the inverse of this number. So if we set the speed to 20, that means each frame will last 1/20th of a second. You can adjust this number to whatever looks good.
The update method simply steps through the frames list at the chosen speed. It will return whatever frame that it needs to return. It works with the nextFrame method which just increments the current_frame counter when needed.
We'll start by animating Pacman. All of the animations happen in the sprites file which makes sense since that's where we handle all of the sprites and animation is closely linked to sprites.
We need to define the Pacman animations by indicating where the images are in the spritesheet.
In the PacmanSprites class we'll create two new methods. In one of the methods we'll define all of Pacman's possible animations. Right now the only animations we care about are when he is moving UP, DOWN, LEFT, or RIGHT. Pacman's LEFT animation looks like the following:
This way his mouth will open and close continuously. We'll do similar things for his RIGHT, UP, and DOWN animations.
Finally, in the update method we will call the animation update method depending on which direction he is moving and that will tell us which image we can extract from the spritesheet.If we detect that pacman is not moving, then the image we want to show is the image at (8,0) where his mouth is closed.
The only thing we need to do in the Pacman class is update his sprites in the update method.
Play the game now and Pacman should be animating correctly. It gives the game more life, doesn't it? Now, lets work on the ghosts.