Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

186 Chapter 8


Figure 8-5: In this first attempt at animation, our
smiley image will streak off the screen.

We still call each drawing a frame, and the speed of our
animation is how many frames per second (fps) we draw. A video
game might run 60–120 frames per second, like high-definition
television. Older, standard-definition TVs in the United States
run at 30 fps, and many film projectors run at 24 fps (newer high-
definition digital projectors can run at 60 fps or higher).
If you’ve ever made or seen a flip-book animation (in which you
draw on the corners of pages in a notebook and then flip through
them to create a mini-cartoon), you’ve seen that the illusion of
motion can be created at many different frame rates. We’ll aim
for a rate around 60 fps, fast enough to create smooth animations.

Moving a Smiley


We can create simple motion in our while loop by drawing the smiley
face image at different locations over time. In other words, in our
game loop, we just need to update the (x, y) location of the picture
and then draw it at that new location each time through the loop.
We’ll add two variables to ShowPic.py: picx and picy, for the
x- and y-coordinates of the image on the screen. We’ll add these
at the end of the setup portion of our program and then save the
Free download pdf