beanz Magazine

Make a Bird Wave!

A simple animation is a fun way to play with and learn the Python programming language.

When watching your favourite cartoon, or the latest superhero blockbuster, it’s tempting to think that the characters on screen are actually moving. Everything just looks so fluid! In reality, video is a clever deception. It’s actually a series of a still images flashed so quickly our brains is tricked into thinking that the images are one smooth sequence.

Of course, the difference between neighbouring images has to be very small, or the motion looks jerky. There also has to be enough images — or ‘frames’ — per second for the illusion to hold. 24 frames per second (FPS) is the industry standard, but research suggests that our minds can create a smooth video out of as little as 16 FPS.

Today, we’re going to make a simple animation with Python!


First, open up your browser and navigate to and then type ‘Python’ or ‘Python3’ into the search box and hit enter.

Drawing Frames

The most important part of animation is creating the images. To make things simple, we’re going to use ‘ASCII art’. This is a fancy of way of saying we’re making images out of typed letters, numbers, and symbols. For example:

:-) :-D :-O

\(^u^)/ _(^u^)_ _(^.^)_ \(^.^)/

><(((* > ><(((* < ><(((^ < ><(((^ >

ASCII is an abbreviation of ‘American Standard Code for Information Interchange’. It’s one of several systems programmers used to encode text on a computer.

Remember: since these images are strung together, the difference between them needs to be small!

The Code

Write the code snippet on the next page into the left-hand window at the website:

import time

frames = ["\(^u^)/",
i = 0
for time_instant in range(2500):
   print("\r" + frames[i])
   i = (i + 1) % len(frames)

Where the frames variable contains the ASCII images you’re going to animate. In order for the computer to read the letters properly, we have to explicitly tell the computer it’s dealing with text. We do this by adding quotation marks (both single and double are fine) around each image:

‘><(((* >’

Don’t worry — the quotation marks won’t show up in the animation.

When you’ve finished setting up your images on the website, hit the ‘Run’ button at the top-left of the screen, and watch the animation!

How the Code Works

The Loop

In order to animate the pictures, we have to tell the computer when to display each one. Instead of using complicated timers, our code uses a loop. At each time_instant, the code inside the loop is executed, and the print statement displays a new image onscreen. Over and over, for a total of 25 images.

If you change range(25) to range(50), the animation will run twice as long, because it shows twice as many images. Try it yourself!


Computer can do millions of operations in a single second. Left to its own devices, it would animate our pictures far too quickly for us to see anything. time.sleep(0.2) tells the computer to pause — to do literally nothing — for 0.2 seconds. Play around with the number inside the brackets for faster and slower animations!

Indexing and Modular Arithmetic

Our frames are stored in a list. Behind the scenes, the computer assigns each picture a number. So you get frame 0, frame 1, frame 2, etc. In this code, the letter i is going to vary between numbers 0 and 3. On the first run, i = 0. The next time, i = 1. Once i reaches 3, it loops back to 0 for the next round. Because we only have 4 frames, it wouldn’t make sense for i go any larger than 3! (In computer science, numbering systems start at 0. Which means that the 4th frame is at position 3. Weird, I know.)

Which brings us to the most bizarre line in the program : i = (i + 1) % len(frames).

Well, the (i+1) part seems pretty straightforward; we’re increasing i by one point. len(frames) is counting the number of pictures in our list. So if we have 4 pictures, len(frames) = 4.

The percentage sign is the mod operator.

If the addition operator (+) is an industrious builder, than the mod operator is a big scary guy with an axe. Any number bigger than the pre-defined bound (in this case, len(frames)) is chopped back down to 0. So 4 % 4 becomes 0, whereas 5 % 4 becomes 1. In other words, modular arithmetic is used to keep numbers within two boundaries, 0 or 1.

A Note on ASCII Characters

The ASCII system contains all the characters you expect: letters (a-z, A-Z), numbers (0-9) and symbols (/, *, &, %). But it also has whitespace character; things like spaces, tabs, and newlines). To make these characters more readable, ASCII uses a special notation.

Character Description
\s space
\t tab
\n newline
\r carriage return

The carriage return moves the cursor to the start of the line. The next frame prints superimposed on top of the previous frame to create the illusion of animation.

Except some computers interpret ‘\r’ as a newline character, same as ‘\n’. This has to do with low-level operating system decisions we can’t really control. In those cases, the animation won’t be seamless. But you still see your character move down the screen.

Now — go animate things!

Learn More

Also In The June 2017 Issue

Can we measure the time and steps required for things to happen?

This Canadian experiment used a robot to explore how people respond to robots and technology.

An amazing new book turns math problems into shapes and illustrations.

This pen and paper project helps organize ideas into stories with a finite state machine.

While you can't use soap and water on your code, you can keep your code as sparkly clean as any dish or silverware.

This project explores the basics of using Google's Static Map software to display your own maps.

Most people love cookies. But these cookies are the kind that make the internet possible.

Links from the bottom of all the June 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for June 2017.

Software languages don't magically appear. They're created by design. First in a series.