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

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

Here is a step-by-step visual of the loop as x grows from 0
toward 100:


for x in range(100):
t.forward(x)
t.left(90)


Loops 0 to 4: The first four lines are drawn (after x = 4).

Loops 5 to 8: Another four lines are drawn; our square emerges.

Loops 9 to 12: Our square spiral grows to 12 lines (three squares).

The dots, or pixels, on your computer screen are probably too
tiny for you to see them very well. But, as x gets closer to 100, the
turtle draws lines consisting of more and more pixels. In other
words, as x gets bigger, t.forward(x) draws longer and longer lines.
The turtle arrow on the screen draws for a while, then turns
left, draws some more, turns left, and draws again and again, with
longer lines each time.
By the end, we have a hypnotizing square shape. Turning left
90 degrees four times gives us a square, just like turning left four
times around a building will take you around the building and
back where you started.
The reason we have a spiral in this example is that every time
we turn left, we go a little farther. The first line that’s drawn is
just 1 step long (when x = 1), then 2 (the next time through the
loop), then 3, then 4, and so on, all the way through 100 steps,
when the line is 99 pixels long. Again, the pixels are probably so
tiny on your screen that you can’t easily see the individual dots,
but they’re there, and you can see the lines get longer as they con-
tain more pixels.
By making all the turns 90-degree angles, we get the perfect
square shape.

Free download pdf