Game Development Reference
In-Depth Information
Chapter 26
Animation
26.1 Introduction
In this chapter, we are going to show you how to add animations to your game.
In the games we have developed until now, game objects could move around on
the screen, but adding something like a running character to your game is slightly
more challenging. In this chapter, we will write a program that contains a character
walking on the bottom of the screen. The character can walk from the left to the
right and vice versa, based on which arrow key (left or right) the player is pressing.
26.2 What Is Animation?
Before we are going to look into how to program a character walking around on
the screen, we first have to think about what animation actually is. To grasp this,
we have to go back to the 1930s, when several animation studios (among which
Walt Disney) produced the first cartoons in black and white. Cartoons are actually
a very fast sequence of still images, also called frames . A television draws these
frames at a very high rate, about 25 times per second. By varying the image that
we draw each time, our brain interprets this as motion. This special 'feature' of the
human brain is very useful, especially when we want to program games that need to
contain moving or animated objects. We have also used this feature in the previous
games we developed in this topic. Every time the Draw method is called, we draw a
new 'frame' on the screen. By drawing sprites on different positions every time, we
get the impression that the sprites actually move. However, this is not really what
is happening. In fact, we are simply drawing the sprite at different positions many
times per second, which makes the player think that the sprite is moving. In a very
similar fashion, we can also draw a walking or running character. We do not only
move the sprite, we also draw a slightly different sprite every time. By drawing a
sequence of sprites that all represent a part of a walking motion, we can create the
illusion that a character is walking on the screen. An example of such a sequence of
sprites is given in Fig. 26.1 .
 
Search WWH ::




Custom Search