Python Turtle Tutorial and Animations

Python Turtle is something that evolved from Logo programming language, invented in 1966 by Wally Feurzig. With the aid of Object Oriented Programming approach, we can create an impressive set of animations easily.

The following animation was created by Python Turtle:

lmc-introduction

 

The Requirements

Before using Python Turtle for animations, please take the following steps to install the environment:

  •   Download the latest version of Python from here.
  •   Load Python IDLE - Integrated Development and Learning Environment - from Windows.
  •   Open a new file and save it with .py extension.
  •   Write down import turtle at the top of the file in order to import the module - classes and methods.
  •   Write down the code and make impressive animations.

The best way to learn the Python turtle is running set of codes, from the simplest to the more advanced gradually, rather than making an effort to understand the simulator fully at first. This is the approach adopted in this tutorial.

The documentation of Python turtle is here.

Here are some of the turtle methods; they direct the turtle what to do:

InstructionsMeaning
turtle.forward(number)Move forward
turtle.back(number)Move backward
turtle.right(angle)Turn clockwise
turtle.left(angle)Turn anti-clockwise
turtle.pencolor(colour string)Drawing colour
turtle.pensize(number)Choosing size of pen nib
turtle.circle(radius)Drawing a circle
turtle.speed(number)Choosing speed - 1 to 10
turtle.write(message,font)Writing on the screen
turtle.ht()Hiding the turtle
turtle.setpos(x,y)Changing the position of turtle

 

Drawing a right-angle

The following animation shows the turtle at work in producing a right-angle on the screen:

python turtle - right-angle

 

This is the code for the above animation:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.forward(150)    #drawing a line of 200 pixels
trtl.right(90)    #asking turtle to turn 90 degrees
trtl.forward(150)    #drawing a line of 200 pixels
trtl.penup()    # preparing for moving pen without drawing
trtl.setpos(-140,-120)    # making the new position of the turtle
trtl.pendown()   # bringing the pen down for drawing again
trtl.pencolor('green')    # choosin the pen colour as green
trtl.write('Vivax Solutions', font=("Arial", 20, "bold"))    # chosing the font
trtl.penup()
trtl.ht()    # hiding the turtle from the screen

# sign indicates the comments in Python scripts.

Drawing a Square

The following animation shows how the turtle draws a square on the screen:

python turtle - right-angle

 

This is the code for the above animation:

import turtle # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.forward(100)    #top line
trtl.right(90)
trtl.forward(100)    # right vertical line
trtl.right(90)
trtl.forward(100)   # bottom line
trtl.right(90)
trtl.forward(100)   # left vertical line
# information printing
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()

Although, the above code produces a square, it is not good programming practice due to repetition of the code, which could have been tackled by a simple loop. Therefore, the code can be revised to produce the same shape with efficiency as follows:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
for i in range(4): # for loop to minimize the same lines of codes being written
      trtl.forward(100)   # for lines
      trtl.right(90)    # for turning
# information printing
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()

The for loop makes the drawing of the square much easier. It role is really important when we create other polygons.

 

Drawing Polygons

In order to change the above code to draw polygons, we need to take into account some concepts in geometry. As you can see, with each turn, the turtle moves through a certain angle, specified by right(angle). This is the exterior angle of the polygon, which is the same as 360/n, where n is the number of sides.

python turtle - exterior angle

 

With for and while loops, the following code produces a set of polygons on screen - from a triangle to a decagon.

lmc-introduction

 

The code for the above animation is as follows:

import turtle     # importing the module
import time    #importing the time module trtl = turtle.Turtle()     #making a turtle object of Turtle class for drawing
screen=turtle.Screen()     #making a canvas for drawing
screen.setup(620,470)     #choosing the screen size
screen.bgpic('bg.gif')     #making canvas black
trtl.pencolor('red')     #making colour of the pen red
trtl.pensize(5)     #choosing the size of pen nib
trtl.speed(1)     #choosing the speed of drawing
trtl.shape('turtle')    #choosing the shape of pen nib
time.sleep(12)
n=3    starting for a triangle
shapes=['Triangle','Square','Pentagon','Hexagon','Heptagon','Octagon','Nonagon','Decagon']
while n<11: #    limiting to a decagon
        for i in range(n):     # for loop to minimize the same lines of codes being written
            trtl.pencolor('red')
            trtl.forward(100)     #top line
            trtl.right(360/n)    #determining the exterior angle of the polygon
trtl.penup()
trtl.setpos(-80,180)    #moving the turtle to make the animation more centric
trtl.pendown()
trtl.pencolor('blue')
trtl.write(' This is '+shapes[n-3], font=("Arial", 16, "bold"))    #printing the name of the polygon
n=n+1
time.sleep(1)
    #making turtle sleep for one second trtl.clear()
trtl.penup()
trtl.setpos(-n*8,n*14)
trtl.pendown()

 

 

Drawing Circles

Here is the code for adding two numbers and drawing a circle:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.circle(60)   drawing circle with radius 60 pixels

The following animation shows how the two numbers are taken in as two inputs and later answer is given out:

python-turtle:circles

 

As you can see, the starting point of the circle is the centre of the screen by default, which is not the centre of the circle. In order to get round this problem, we have to set the position by code as follows:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.penup()   #moving the pen up
trtl.setpos(0,-60)   #setting new position
trtl.pendown()   #moving the pen down
trtl.circle(60)   #drawing circle with radius 60 pixels
lmc-data-addition

 

Drawing Concentric Circles

Here is the code for drawing concentric circles with the aid of loops:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.shape('turtle')   #choosing the shape of pen nib
n=0   
while n<7:   #loop for 7 circles
n=n+1   
trtl.penup()   
trtl.setpos(0,-n*20)   
trtl.pendown()   
trtl.circle(20*n)   

The following animation shows how the animation works:

python-turtle-concentric cirles

 

 

Changing Colours

The colour of the pen can be changed in many different ways; here are two ways:

  1. turtle.pencolor('red')
  2. turtle.pencolor(red, green, blue)

If you use the second method, red, green and blue can be any integer between 1 - 255. However, before that, turtle.colormode(1) or turtle.colormode(255) must be declared in the code.

Here is the code:

import turtle    # importing the module
import random    # importing random module for generating random numbers
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pensize(4)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
n=0
while n<7:
r=random.randint(1,120)   #random numbers for red
g=random.randint(81,200)   #random numbers for green
b=random.randint(61,255)   #random numbers for blue
turtle.colormode(255)   #declaring the colour mode
trtl.pencolor(r,g,b)   #pen colour
n=n+1
trtl.penup()
trtl.setpos(0,-n*20)
trtl.pendown()
trtl.circle(20*n)

The following animation shows the output - with random colours, of course.

python-turtle-colours

 

 

Advanced Animations - turtle in its habitat!

The following code creates a turtle that moves around on a beach - leaving behind a certain regular pattern!

import turtle
import random
trtl = turtle.Turtle()
screen=turtle.Screen()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.pensize(4)
trtl.speed(1)
trtl.shape('turtle')
turtle.colormode(255)
trtl.pencolor(242,242,242)
trtl.penup()
trtl.setpos(-160,-100)
trtl.pendown()
for i in range(4):
        trtl.forward(40)
        trtl.left(90)
        trtl.forward(30)
        trtl.right(90)
        trtl.forward(40)

The following animation shows the iteration that leads to a countdown, based on the user input:

turtle-python-motion

 

 

 

Advanced Animations - diverging turtles

In this animation, turtles leave their foot print on the beach: this is achieved by turtle.stamp() method along with turtle.penup(). This is the code:

import turtle
import random
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.shape('turtle')
trtl.color('darkgoldenrod','black')
s=10
trtl.penup()
trtl.setpos(30,30)
for i in range(28):
        s=s+2
        trtl.stamp()
        trtl.forward(s)
        trtl.right(25)
        time.sleep(0.25)      #activated with a break of a 1/4th of a second

The animation is as follows:

turtle-python-motion-on beach

 

 

 

Advanced Animations - colourful star

With the aid of simple geometry, a star can be drawn by Python Turtle. This is the code:

import turtle
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgcolor('black')
clr=['red','green','blue','yellow','purple']
trtl.pensize(4)
trtl.penup()
trtl.setpos(-90,30)
trtl.pendown()
for i in range(5):
     trtl.pencolor(clr[i])
     trtl.forward(200)
     trtl.right(144)
trtl.penup()
trtl.setpos(80,-140)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

This is the animation that produces the star:

turtle-python-star

 

Advanced Animations - a clock dial

In the following animation, Python Turtle is used to construct a clock dial - with numbers, 1 to 12, surrounded by a circle. This is the code:

import turtle
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(620,620)
screen.bgcolor('black')
clr=['red','green','blue','yellow','purple']
trtl.pensize(4)
trtl.shape('turtle')
trtl.penup()
trtl.pencolor('red')
m=0
for i in range(12):
      m=m+1
      trtl.penup()
      trtl.setheading(-30*i+60)
      trtl.forward(150)
      trtl.pendown()
      trtl.forward(25)
      trtl.penup()
      trtl.forward(20)
      trtl.write(str(m),align="center",font=("Arial", 12, "normal"))
      if m==12:
        m=0
      trtl.home()
trtl.home()
trtl.setpos(0,-250)
trtl.pendown()
trtl.pensize(10)
trtl.pencolor('blue')
trtl.circle(250)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

This is the animation at work:

turtle-python-clock dial

 

 

Advanced Animations - multi-coloured flower

The following code produces a flower in different colours. The colours are provided with the aid of a list.The code is as follows:

trtl=turtle.Turtle()
screen=turtle.Screen()
screen.setup(620,620)
screen.bgcolor('black')
trtl.pensize(3)
trtl.speed(10)
n=-1
for angle in range(0,360,15):
     n=n+1
       if n==5:
       n=-1
      trtl.color(colors[n])
      trtl.seth(angle)
      trtl.circle(100)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

This is the animation:

turtle-python-flower

 

As you can see, the effect is produced by turtle.seth() function - in turning the direction of turtle.

 

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Email: 

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Advertisements

Advertisement