Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Turtle Graphics: Drawing with Python 19

A radius of x means that the diameter, or total width, of the circle
will be two times x. In other words, t.circle(x) draws a circle 2 pix-
els across when x is equal to 1, 4 pixels across when x is 2, all the
way up to 198 pixels across when x is 99. That’s almost 200 pix-
els across, or twice the size of our biggest side in the square, so
the circle spiral is about double the size of our square spiral—and
maybe twice as cool, too!

a dding a Touch of color


These spirals are nice shapes, but wouldn’t it be cooler if they were
a bit more colorful? Let’s go back to our square spiral code and add
one more line to our program, right after the t = turtle.Pen() line,
to set the pen color to red:

SquareSpiral3.py
import turtle
t = turtle.Pen()
t.pencolor("red")
for x in range(100):
t.forward(x)
t.left(91)

Run the program, and you’ll see a more colorful version of our
square spiral (Figure 2-4).

Figure 2-4: The square spiral gets a little
more colorful.

Try replacing "red" with another common color, like "blue"
or "green", and run the program again. You can use hundreds of
Free download pdf