The MagPi - July 2018

(Steven Felgate) #1

Tutorial


(^60) July 2018 raspberrypi.org/magpi
WALKTHROUGH
BRIAN BEUKEN
Very old game programmer now
teaching very young game programmers
a lot of bad habits at Breda University
of Applied Science in Breda NL.
scratchpadgames.net
Animate a character and start making a platform game
e’ve gone as far as we can with the basic
bat-and-ball game, so it’s time to introduce
another stalwart of the 2D era and create a
simple platform game – this time with a character we
can control, who can walk, jump, and climb, to avoid
some fairly unintelligent baddies.
Animation 101
What is animation? It might seem obvious, but why
is it that cartoons and movies allow us to see multiple
individual frames as smooth(ish) motion?
All screen-based visuals are based on a simple concept
called image retention, where we trick the brain into
thinking that a sequence of rapidly changing frames
(each not massively different from the previous one)
appear to be in motion. As long as we can keep the frame
rate up, these individual still frames will seem to move
smoothly; our brain will fill in the missing frames.
The rate of animation can be quite variable, but
most people start to see ‘jerkiness’ if an animation
is less than 15–20 fps (frames per second). Individual
perception limits vary quite a bit from person to
person. Cartoons and movies mostly rely on 24 fps,
but there are a few people who see that as jerky. We
will generally set a 20 or 30 fps rate on animation,
even if the game is updating at 60 fps.
Like traditional cartoons, all computer graphics,
even 3D, rely on there being enough frames to give the
impression of the motion you want to represent over a
sensible time for that motion to occur. You can make a
character walk with just two images changing every 30
frames, but it looks better with four changing every 15,
and better still with eight every seven or eight frames.
The more images for the sequence you have, the
smoother the animation, but you are eating up your
memory by storing so many individual sprite images.
So a balance must be found.
Figure 1 shows two simple four-frame animations.
Since we’re only going to use a few frames for walking,
we could cheat and reuse them for walking, standing,
falling, and jumping, though at the moment we do
need left- and right-facing. Each of these different
actions we will call a ‘state’, but each state needs to
keep track of what sequence of animation it is doing
W



Code::Blocks
sudo apt-get
codeblocks
stb_image.h
You’ll
Need
CODING GAMES
ON THE RASPBERRY PI
IN C/C++
PART 07
Figure 1 Simple individual frames
KEEP
ASSETS TIDY
At the moment
we’re not really
doing that other
than keeping
them in a folder.
We’ll discuss
this soon.


Free download pdf