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.

  1. # SquareSpiral1.py - Draws a square spiral
  2. import turtle
  3. t = turtle.Pen()
  4. for x in range(0, 1000, 3):
  5. t.forward(x)
  6. t.left(90)

When we run this code, we get a pretty neat picture
image.png

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.

  1. # SquareSpiral2.py - Draws a square spiral
  2. import turtle
  3. t = turtle.Pen()
  4. for x in range(1, 1000, 3):
  5. t.forward(x)
  6. t.left(91)

image.png

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:

  1. # SquareSpiral2.py - Draws a square spiral
  2. import turtle
  3. t = turtle.Pen()
  4. for x in range(1, 1000, 3):
  5. t.circle(x)
  6. t.left(91)

image.png

Adding a Touch of Color

Let’s go back to our square spiral code and add one more line to our program:

  1. # SquareSpiral2.py - Draws a square spiral
  2. import turtle
  3. colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
  4. t = turtle.Pen()
  5. for x in range(0, 1000, 3):
  6. t.circle(x)
  7. t.pencolor(colors[x//3%6])
  8. t.left(91)

image.png

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

  1. Changing the number of sides
  2. How Many sides?
  3. Rubber-Band Ball