Turtle Projects
Put It All Together
In this chapter, you will use what you have learned to draw a star, two spirals and a pattern.
Draw a Star
A star with five points is drawn with five lines. The turtle turns 144 degrees at each point:
Example
Draw a star:
import turtle
t = turtle.Turtle()
for i in range(5):
t.forward(150)
t.right(144)
turtle.done()
Result:
Why 144 degrees? When the turtle draws a star, it turns all the way around twice, which is 2 × 360 = 720 degrees. With 5 points, each turn is 720 / 5 = 144 degrees.
Draw a Spiral
A square spiral is like a square, but each line is a bit longer than the one before.
The loop variable i counts 0, 1, 2, 3, and so on.
When the turtle moves i * 5 pixels, each line is 5 pixels longer than the line before:
Example
Draw a square spiral:
import turtle
t = turtle.Turtle()
for i in range(50):
t.forward(i * 5)
t.left(90)
turtle.done()
Result:
A Colorful Spiral
This is the spiral from the first page of this tutorial.
It uses a list of colors, and a turn of 91 degrees instead of 90. The extra degree twists the spiral a little more for each line:
Example
Draw a colorful spiral:
import turtle
t = turtle.Turtle()
t.pensize(2)
colors = ["red", "orange",
"blue", "green"]
for i in range(100):
t.pencolor(colors[i % 4])
t.forward(i * 2)
t.left(91)
turtle.done()
Result:
colors[i % 4] picks a color from the list.
The modulus operator % gives the remainder after division,
so i % 4 counts 0, 1, 2, 3, 0, 1, 2, 3, and so on. That way, the four colors repeat.
A Pattern of Squares
This pattern is made of 36 squares, with a small turn between each one:
Example
Draw a pattern of squares:
import turtle
t = turtle.Turtle()
t.speed(0)
def square(size):
for i in range(4):
t.forward(size)
t.left(90)
for i in range(36):
square(90)
t.left(10)
turtle.done()
Result:
The turtle turns 10 degrees after each square. 36 turns of 10 degrees is 360 degrees, one full turn.
The pattern has many lines, so t.speed(0) makes the turtle draw it fast.
Keep Experimenting
Change the numbers in the examples, and see what happens. Here are some ideas:
- Change 91 to 89 in the colorful spiral
- Add a fifth color to the list, and change
i % 4toi % 5 - Draw the pattern with triangles instead of squares
Tip: You can find a list of turtle methods in our Python turtle Module Reference.