Pygame - Part 3 Animation
Pygame - Part 3 Animation
Animations
• Now that we know how to get the Pygame
framework to draw to the screen, let’s
learn how to make animated pictures.
• A game with only still, unmoving images
would be fairly dull.
• Sales of the game “Look At This Rock”
have been disappointing.
• Animated images are the result of drawing
an image on the screen, then a split
second later drawing a slightly different
image on the screen.
Animation
• Imagine the program’s window was 6 pixels wide
and 1 pixel tall, with all the pixels white except
for a black pixel at 4, 0. It would look like this:
Animation
Our First Animation
Program
• We will save it as
catanimation.py
• You will need to have the
image file cat.png to be in the
same folder as
catanimation.py
Catanimation.py
The frame rate or refresh rate is the number of pictures that the
program draws per second, and is measured in FPS or frames per second.
On computer monitors, the common name for FPS is hertz.
Many monitors have a frame rate of 60 hertz, or 60 frames per second.)
A low frame rate in video games can make the game look choppy or
umpy.
If the program has too much code to run to draw to the screen frequently
enough, then the FPS goes down.
The games we will cover won’t have this as an issue.
Pygame.time.clock()
• A pygame.time.Clock object can help us make
sure our program runs at a certain maximum FPS.
• This Clock object will ensure that our game
programs don’t run too fast by putting in small
pauses on each iteration of the game loop.
• If we didn’t have these pauses, our game
program would run as fast as the computer could
run it. This is often too fast for the player, and as
computers get faster they would run the game
faster too.
Pygame.time.clock()
• A call to the tick() method of a
Clock object in the game loop
can make sure the game runs at
the same speed no matter how
fast of a computer it runs on.
• The Clock object is created on
line 7 of the catanimation.py
program.
Catanimation.py
Catanimation.py
47. fpsClock.tick(FPS)
Sound