# The turtle library

#Basic#turtle

turtle (opens new window) is a standard Python library that allows you to create shapes and drawings in a virtual canvas. Imagine you start at (0, 0) in a x-y plane. With the help of turtle library, you can move a virtual pen to draw lines and fill with colors to make creative drawings.

Let's see how it goes in action.

# Getting started

Simply import turtle to get started. Take a look at the example from the Python docs and get the feeling of what turtle can offer.

import turtle

turtle.color('red', 'yellow')
turtle.begin_fill()

while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break

turtle.end_fill()
1
2
3
4
5
6
7
8
9
10
11
12

Now let's learn the basics of turtle.

# Screen

import turtle

screen = turtle.getscreen()
screen.title("Hello World")
screen.setup(width=.5,height=400)
1
2
3
4
5

turtle.getscreen() (opens new window) will show the interactive canvas. You can see the triangular figure ("turtle") in the middle of the screen. That's where our virtual penpoint locates.

We can set the title using title() (opens new window) and change the size of screen using setup() (opens new window).

screen.bgcolor("green")
1

We can also change the background color using bgcolor() (opens new window)

# Turtle

t = turtle.getturtle()
t.shape("turtle")
1
2

You can get the default turtle using turtle.getturtle() (opens new window). shape() (opens new window) can change the default triangular shape to your liking. Available options include "arrow", "turtle", "circle", "square", "triangle", and "classic".

t.pencolor("red")
t.fillcolor("white")
1
2

You can further customize the appearance of the turtle using pencolor() (opens new window) and fillcolor() (opens new window).

In fact, pencolor() also determines the color of the trace and fillcolor() would change the color of a filled shape. See the examples below.

# Moving

The turtle can move forward() (opens new window) or backward() (opens new window) in the direction that it's facing. It can also turn to the left() (opens new window) or right() (opens new window) by a specific degree. By default, the turtle will draw when it moves. Check the commands and the associated results.


First, we move the turtle forward by 100.

t.forward(100)
1


Then, we turn the turtle to the right by 45 degrees. Notice the turtle head turned.

t.right(45)
1


Next, we move backward by 50.

t.backward(50)
1


This time, we turn the turtle to the left by 90 degrees. See the turtle head now is pointing to upper right.

t.left(90)
1


Lastly the turtle continues to move forward by 100

t.forward(100)
1


We can also move the turtle to a certain coordinate using goto() (opens new window).

t.goto(-100, 50)
1


And home() will send the turtle back to the initial location.

t.home()
1


circle() (opens new window) would make the turtle move in circle.

t.circle(100)
1


By the way, we can also control the speed() (opens new window) in which the turtle moves.

t.speed(1)
t.circle(20)
t.speed(10)
t.circle(40)
1
2
3
4

# Filling

When wrapped by begin_fill() and end_fill(), the area determined by the path of the turtle's movement will be filled with color.

Let's use some of the previous movements for example.


import turtle

screen = turtle.getscreen()
screen.title("Hello World")
screen.setup(width=.5,height=400)
screen.bgcolor("green")

t = turtle.getturtle()
t.shape("turtle")
t.pencolor("blue")
t.pencolor("red")
t.fillcolor("white")

turtle.begin_fill()
t.forward(100)
t.right(45)
t.backward(50)
t.left(90)
t.forward(100)
t.goto(-100, 50)
turtle.end_fill()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Notice that a straight imaginary line will draw automatically if the ending location is different from the initial one.


We can as well control the pen size using pensize() (opens new window).

t.pensize(10)

turtle.begin_fill()
t.circle(60)
turtle.end_fill()
1
2
3
4
5


# Multiple Turtles

We can create additional turtles if needed.

import turtle

screen = turtle.getscreen()
screen.title("Hello World")
screen.setup(width=.5,height=400)
screen.bgcolor("green")
t1 = turtle.getturtle()
t2 = turtle.Turtle()

t1.shape("turtle")
t2.shape("arrow")

t1.goto(100, 100)
t2.goto(100, -100)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Moving without drawing

The turtle will not draw if we lift the pen up. To resume drawing, simply pen down.

import turtle

screen = turtle.getscreen()
screen.title("Hello World")
screen.setup(width=.5,height=400)
screen.bgcolor("green")

t = turtle.getturtle()
t.penup()
t.goto(100, 100)
t.pendown()
t.backward(100)
t.home()
1
2
3
4
5
6
7
8
9
10
11
12
13

# Assignment 15

Create a Python script named turtle_square.py which draws a red square with length of 100.

Canvas closing too soon?

Add turtle.done() at the end of script so that the canvas won't close automatically.

The drawing would look like as below.

Sample Solution
import turtle

screen = turtle.getscreen()
screen.title("Red Square")

t = turtle.getturtle()
t.color("red", "red")

turtle.begin_fill()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.home()
turtle.end_fill()

turtle.done()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Assignment 16

Create a Python script named turtle_triangle.py which draws a green triangle with vertices at (0,0), (100, 40), and (-50, 80).

The drawing would look like as below.

Sample Solution
import turtle

screen = turtle.getscreen()
screen.title("Green Triangle")

t = turtle.getturtle()
t.color("green", "green")

turtle.begin_fill()
t.goto(100, 40)
t.goto(-50, 80)
t.home()
turtle.end_fill()

turtle.done()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Assignment 17

Create a Python script named turtle_race.py which simulates a race between two turtles, a blue turtle and a green one. The first to move 200 forward would win the race. The race is turn by turn. In each turn both turtles roll a dice to determine how far they move forward. For example, if the blue turtle rolls a six, it moves 6 forward.

Also show the winner at the title bar when the race ends.

The drawing would look like as below on completion.

Sample Solution
import turtle as t
from random import randint

screen = t.getscreen()
screen.title("Turtle Race")

t1 = t.getturtle()
t2 = t.Turtle()

t1.penup()
t1.goto(0, 50)
t1.pendown()

t1.shape("turtle")
t2.shape("turtle")

t1.color("blue", "blue")
t2.color("green", "green")

distance_t1, distance_t2, length = 0, 0, 200

while distance_t1 < length and distance_t2 < length:
    move_t1 = randint(1, 6)
    move_t2 = randint(1, 6)

    t1.forward(move_t1)
    t2.forward(move_t2)

    distance_t1 += move_t1
    distance_t2 += move_t2

if distance_t1 >= length and distance_t2 >= length:
    title = "It's a tie."
elif distance_t1 >= length:
    title = "Blue Turtle won!"
else:
    title = "Green Turtle won!"

t.title(title)
t.done()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40