Introduction to Python Lesson 4: Turtle Graphics

Click to Download Lesson 4 Slides

In this tutorial we will learn how to draw a graph using Python Turtle library.

“Turtle” is a python feature like a drawing board, which lets you command a turtle to draw all over it! You can use functions like turtle.forward(…) and turtle.left(…) which can move the turtle around.

What can be drawn with Turtle Graphics ?

Below are two drawings created using Turtle Graphics. Original author Jack Dong

Pikachu using Python Turtle

Doraemon using Python Turtle

Simple movement with turtle, draw a line

As a starting point, you can draw a line using python turtle functions: there are four steps involved:

  1. Import the turtle module
  2. Create a turtle to control and assign a name to it
  3. Draw things.
    1. Draw more things with forward(…) or backward(…)
    2. Draw more things.
  4. Run turtle.done() after finishing.

Copy the code below, open the Python Playground, paste the code and run it.

1
2
3
4
5
6
7
8
9
10
# Step 1: Make all the "turtle" commands available to us.
import turtle

# Step 2: Create a new turtle. call it alice
alice = turtle.Turtle()

# Step 3: Move in the direction Alice's facing for 50 pixels
alice.forward(100)
# Step 4: We're done!
turtle.done()

Draw a square

We can use Lines are boring. We can rotate the turtle in order to draw more interesting figures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import turtle
alice = turtle.Turtle()

alice.forward(100)   # Move forward 50 pixels
alice.right(90)     # Rotate clockwise by 90 degrees

alice.forward(100)
alice.right(90)

alice.forward(100)
alice.right(90)

alice.forward(100)
alice.right(90)

turtle.done()

Draw a star

1
2
3
4
5
6
7
8
9
import turtle

star = turtle.Turtle()

for i in range(5):
    star.forward(50)
    star.right(144)

turtle.done()

Draw a hexagon

1
2
3
4
5
6
7
8
9
10
11
12
13
import turtle

polygon = turtle.Turtle()

num_sides = 6
side_length = 70
angle = 360.0 / num_sides

for i in range(num_sides):
    polygon.forward(side_length)
    polygon.right(angle)

turtle.done()

Changing line color

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import turtle

painter = turtle.Turtle()

painter.pencolor("blue")

for i in range(50):
    painter.forward(50)
    painter.left(123) # Let's go counterclockwise this time

painter.pencolor("red")
for i in range(50):
    painter.forward(100)
    painter.left(123)

turtle.done()

Spiraling star

1
2
3
4
5
6
7
8
9
import turtle

spiral = turtle.Turtle()

for i in range(20):
    spiral.forward(i * 10)
    spiral.right(144)

turtle.done()

Drawing circles

The code below will draw a circle of radius 50units.

1
2
3
4
import turtle
t = turtle.Turtle()

t.circle(50)

More than one circle having one point of intersection is called tangent circles. The code below draws tangent Circles in Python turtle.

1
2
3
4
5
6
#Program to draw tangent circles in Python Turtle
import turtle

t = turtle.Turtle()
for i in range(10):
  t.circle(10*i)

Circles with varying radius are called spiral. The code below draws spiral circles in Python turtle.

1
2
3
4
5
6
#Program to draw spiral circles in Python Turtle
import turtle

t = turtle.Turtle()
for i in range(30):
  t.circle(10+i, 45)

Circles with different radii having a common center are called concurrent circles.

1
2
3
4
5
6
7
8
9
#Program to draw concentric circles in Python Turtle
import turtle

t = turtle.Turtle()
for i in range(10):
  t.circle(10*i)
  t.up()
  t.sety((10*i)*(-1))
  t.down()

Turtle Olympics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import turtle
myTurtle = turtle.Turtle()

myTurtle.circle(50)
myTurtle.penup()
myTurtle.setposition(-120, 0)
myTurtle.pendown()
myTurtle.circle(50)
myTurtle.penup()
myTurtle.setposition(60,60)
myTurtle.pendown()
myTurtle.circle(50)
myTurtle.penup()
myTurtle.setposition(-60, 60)
myTurtle.pendown()
myTurtle.circle(50)
myTurtle.penup()
myTurtle.setposition(-180, 60)
myTurtle.pendown()
myTurtle.circle(50)
turtle.getscreen()._root.mainloop()

turtle.done()

Turtle’s House

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Create a scene with a house, a tree, and a sun
# Based on the picture at http://dragonometry.net/blog/?p=566
# copied from https://trinket.io/python/8bfa7a2f2a

import turtle
import math

# Set the background color
screen = turtle.Screen()
screen.bgcolor("skyblue")

# Create our turtle
george = turtle.Turtle()
george.color("black")
george.shape("turtle")
george.speed(10)

# Define a funtion to draw and fill a rectangle with the given
# dimensions and color
def drawRectangle(t, width, height, color):
  t.fillcolor(color)
  t.begin_fill()
  t.forward(width)
  t.left(90)
  t.forward(height)
  t.left(90)
  t.forward(width)
  t.left(90)
  t.forward(height)
  t.left(90)
  t.end_fill()

# Define a function to draw and fill an equalateral right
# triangle with the given hypotenuse length and color.  This
# is used to create a roof shape.
def drawTriangle(t, length, color):
  t.fillcolor(color)
  t.begin_fill()
  t.forward(length)
  t.left(135)
  t.forward(length / math.sqrt(2))
  t.left(90)
  t.forward(length / math.sqrt(2))
  t.left(135)
  t.end_fill()

# Define a function to draw and fill a parallelogram, used to
# draw the side of the house
def drawParallelogram(t, width, height, color):
  t.fillcolor(color)
  t.begin_fill()
  t.left(30)
  t.forward(width)
  t.left(60)
  t.forward(height)
  t.left(120)
  t.forward(width)
  t.left(60)
  t.forward(height)
  t.left(90)
  t.end_fill()

# Define a function to draw four sun rays of the given length,
# for the sun of the given radius.  The turtle starts in the
# center of the circle.
def drawFourRays(t, length, radius):
  for i in range(4):
    t.penup()
    t.forward(radius)
    t.pendown()
    t.forward(length)
    t.penup()
    t.backward(length + radius)
    t.left(90)

# Draw and fill the front of the house
george.penup()
george.goto(-150, -120)
george.pendown()
drawRectangle(george, 100, 110, "blue")

# Draw and fill the front door
george.penup()
george.goto(-120, -120)
george.pendown()
drawRectangle(george, 40, 60, "lightgreen")

# Front roof
george.penup()
george.goto(-150, -10)
george.pendown()
drawTriangle(george, 100, "magenta")

# Side of the house
george.penup()
george.goto(-50, -120)
george.pendown()
drawParallelogram(george, 60, 110, "yellow")

# Window
george.penup()
george.goto(-30, -60)
george.pendown()
drawParallelogram(george, 20, 30, "brown")

# Side roof
george.penup()
george.goto(-50, -10)
george.pendown()
george.fillcolor("orange")
george.begin_fill()
george.left(30)
george.forward(60)
george.left(105)
george.forward(100 / math.sqrt(2))
george.left(75)
george.forward(60)
george.left(105)
george.forward(100 / math.sqrt(2))
george.left(45)
george.end_fill()

# Tree base
george.penup()
george.goto(100, -130)
george.pendown()
drawRectangle(george, 20, 40, "brown")

# Tree top
george.penup()
george.goto(65, -90)
george.pendown()
drawTriangle(george, 90, "lightgreen")
george.penup()
george.goto(70, -45)
george.pendown()
drawTriangle(george, 80, "lightgreen")
george.penup()
george.goto(75, -5)
george.pendown()
drawTriangle(george, 70, "lightgreen")

# Sun
george.penup()
george.goto(55, 110)
george.fillcolor("yellow")
george.pendown()
george.begin_fill()
george.circle(24)
george.end_fill()

# Sun rays
george.penup()
george.goto(55, 134)
george.pendown()
drawFourRays(george, 25, 24)
george.right(45)
drawFourRays(george, 15, 24)
george.left(45)

# Put down some labels
george.color("black")
george.penup()
george.goto(-150, 70)
george.pendown()
george.write("House", None, None, "14pt bold")
george.penup()
george.goto(150, -150)
george.pendown()
george.write("Tree", None, None, "14pt bold")
george.penup()
george.goto(130, 150)
george.pendown()
george.write("Sun", None, None, "14pt bold")

# Bring the turtle down to the front door, and we're done!
george.penup()
george.goto(-100, -150)
george.left(90)