Python Programming: Use Turtle Library to Realize Animation

In the previous post, I use Scratch 3.0 to create animation with Pen functionality. Could we do the same thing using Python? Definitely Yes. We could use Turtle library to generate the same effect, but with more concise code structure. In this post, Let me introduce how to implement it.

If you would like to compare Python code with the Scratch version, you could visit Scratch project at this address:

The final effect could be like this:

Now let’s jump into the code to explain them in detail.

From line 1 to line 10, the code is used to initiate turtle screen, title and background color. You will see a list called “color_list”. It is used to store color for star pattern edges, which we will use at line 57.

At line 15, the program calls “turtle.tracer(0,0)”. This line is very important since it disables screen refreshing until all the work is done. By default, the turtle program will update drawing from every change. The final result is that program runs a bit slow. Using “tracer(0,0)”, the program does not update until it calls “wn.mainloop()” at line 66. Therefore, it eliminates the millisecond delays.

From line 17 to line 28, the program creates two turtle objects and names them “tess” and “alex” respectively. Each turtle object has their unique features such as color, pensize and shape.

From line 30 to line 62, the program defines function “change_size()”. It sets global variable “step” and increases it by 1. It uses turtle “tess” iterate twelve times to draw the clock face. Please note the usage of expression “tess.forward (150 + abs((step mod 12) – 6) * 5)”. This line will make the forward distance change within a range from 150 to 180, so that the clock face changes size in a cyclical way, just like what Scratch project is doing. As to how the expression “abs((step mod 12 – 6) * 5)” could realize this effect, you could refer to the following table to illustrate the change of value:

You could see that after every 12 numbers, the result is the same. For example, when step = 0 and step 12, the final result of “abs (step % 12 – 6) * 5” is equal to 30. When step = 3 and step = 15, the final result is 15.

From line 50 to line 69, the program is drawing the star pattern. Please note that the program is not using “step” or other variables to control the spinning angle. This is because for “alex” turtle object, when the program calls “alex.clear()”, it just removes the drawing of alex, but its direction is still kept. Therefore, every time “change_size()” function is called, alex direction is using the final direction from previous cycle and is different for each cycle. We will see the star pattern spin.

At line 62, the program calls “wn.ontimer(change_size, 100)”. This line ensures that the function “change_size” is called automatically every 100 milliseconds.

At line 65, the program calls “turtle.update()” to refresh the display of the whole screen.

That is all for the Python code to implement the animation. Pretty concise, right? There are two key points for this project. One is to ensure that you call “turtle.tracer(0,0)” to turn off refreshing, the other point is to use global variable “step” to generate cyclical motion effect. Keep tuned and we will present more code samples in future.

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.