In this chapter, we’ll write short, simple programs to create beautifully complex visuals. To do this, we’ll use turtle graphics. In turtle graphics, you write instruc- tions that tell a virtual, or imaginary, turtle to move around the screen. The turtle carries a pen, and you can instruct the turtle to use its pen to draw lines wherever it goes. By writing code to move the turtle around in cool patterns, you can make it draw amazing pictures.
First Turtle Program
Using turtle graphics, not only can you create impressive visuals with a few lines of code, but you can also follow along with the turtle and see how each line of code affects its movement.
This will help you understand the logic of your code.
# SquareSpiral1.py - Draws a square spiral
import turtle
t = turtle.Pen()
for x in range(0, 1000, 3):
t.forward(x)
t.left(90)
When we run this code, we get a pretty neat picture
How It Works
A comment begins with a hash mark (#). Comments allow us to write notes in our programs to ourselves or to other humans who might read the program later.
import
imports the ability to draw turtle graphics. Importing code that’s already been written is one of the coolest things about programming.
t = turtle.Pen()
tells the computer that we’ll use the letter t to stand for the turtle’s pen. This will allow us to draw with the turtle’s pen as the turtle moves around the screen just by typing t.forward()
instead of writing out turtle.Pen().forward()
. The letter t
is our shortcut for telling the turtle what to do.
What Happens
The command t.forward(x)
tells the turtle pen to move forward x dots on the screen. Because x is 0, the pen doesn’t move at all. The last line, t.left(90)
tells the turtle to turn left by 90 degrees, or a quarter turn. Because of that for loop, the program continues to run, and it goes back to the starting position of our loop, this continues again and again.
Turtle on a Roll
Let’s see what happens when we change one of the numbers in the program. One way to learn new things about a program is to see what happens when you change one part of it. You won’t always get a pretty result, but you can learn even when something goes wrong.
# SquareSpiral2.py - Draws a square spiral
import turtle
t = turtle.Pen()
for x in range(1, 1000, 3):
t.forward(x)
t.left(91)
Turtle Roundup
Speaking of geometry, turtle graphics can draw lots more interesting shapes than just straight lines. Changing one command from t.forward to t.circle gave us a much more complex shape:
# SquareSpiral2.py - Draws a square spiral
import turtle
t = turtle.Pen()
for x in range(1, 1000, 3):
t.circle(x)
t.left(91)
Adding a Touch of Color
Let’s go back to our square spiral code and add one more line to our program:
# SquareSpiral2.py - Draws a square spiral
import turtle
colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
t = turtle.Pen()
for x in range(0, 1000, 3):
t.circle(x)
t.pencolor(colors[x//3%6])
t.left(91)
Summary
In this chapter, we drew impressive, colorful shapes in Python using the Turtle library of tools. At this point, you should be able to do the following:
- Draw simple graphics with the Turtle library.
- Use variables to store simple number values and strings.
- Change, save, and run programs in IDLE.
Challenges
- Changing the number of sides
- How Many sides?
- Rubber-Band Ball