Game Development:
Intro to Computer Science

CSC 105
Union College
Fall 2009

Exercises about dictionaries and to practice for the final are on Codelab.

Helpdesk in Olin 102, Sun-Thu 7-9pm.

Animated Pictures

The goal of this project is to create an animated image (more specifically, a walking character where the feet are moving) using a list. We will talk in class about the general ideas.

Download, the following program as a starting point: walking_guy_v0.py. You also need all of these pictures:

These images were created by George W. Clingerman. I got them from his great site of XNA tutorials.

Step 1:

In the code I gave you, the variable guy refers to a single (still) picture. We now need it to refer to a list, which contains all of the images that are part of our animation sequence. And the images need to be ordered in the order that we want to show them in.

Change the code where the picture is loaded, such that a list is created which contains all loaded images. (Call this list guy.) Then write code that goes through the list and converts each image to pygame's internal format.

Step 2:

Now that we have a list of pictures instead of just a single picture, we need a variable to keep track of which picture we want to show. We can do that by having this variable hold the index of the picture in the list.

Create a variable called cur_idx and set it to 0. Find the right place in your code where this variable should be created? Inside the game loop or outside? Why?

Step 3:

Change the command that actually draws the picture, such that it draws the picture from the list that we currently want to show (as indicated by the cur_idx variable.

Now try it out. You should see the same thing as before. Just one still picture of the character. (But it is working with the list now, instead of just one single picture.)

Step 4:

Now we need to increase cur_idx by 1 every so often and reset it to 0 every time we reach the end of the list.

At first, increase cur_idx by 1 in every frame (i.e., every pass through the game loop). And get the code right for resetting it to 0 when reaching the end of the list.

Try it. The timing will not be very good, but you should see the feet moving very, very fast.

Step 5:

Now, slow down the animation.

To do that introduce a variable called anim_delay, which specifies how many frames you want to wait before switching to the next picture. In addition to that, you need a timer/counter variable (let's call it anim_timer, which counts how many frames have passed since the last time we switched out the picture.

If that timer reaches your threshold (specified by anim_delay), cur_idx get changed.