We’ve already learned how to make the turtle draw simple shapes. Before using the turtle, we need to import the turtle module and
create the Pen object:
>>> import turtle
>>> t = turtle.Pen()
Here’s the code we used to create a square in Chapter 4:
>>> t.forward(50) >>> t.left(90) >>> t.forward(50) >>> t.left(90) >>> t.forward(50) >>> t.left(90) >>> t.forward(50)
In Chapter 6, you learned about for loops. With our newfound
knowledge, we can make this somewhat tedious code for a square simpler using a for loop:
>>> t.reset()
>>> for x in range(1, 5): t.forward(50) t.left(90)
On the first line, we tell the Pen
object to reset itself. Next, we start a for loop that will count from 1 to
4 with the code range(1, 5). Then,
with the following lines, in each run of the loop, we move forward 50 pixels and turn left 90 degrees. Because we’ve used a for loop, this
code is a little shorter than the pre- vious version—ignoring the reset
line, we’ve gone from six lines down to three.
More Turtle Graphics 147
Drawing stars
Now, with a few simple changes to our for loop, we can create
something even more interesting. Type in the following:
>>> t.reset()
>>> for x in range(1, 9): t.forward(100) t.left(225)
This code produces an eight-point star:
The code itself is very similar to the code we used to draw a square, with a few exceptions:
•
Rather than looping four times with range(1, 5), we loop eighttimes with range(1, 9).
•
Rather than moving forward 50 pixels, we move forward 100 pixels.•
Rather than turning 90 degrees, we turn 225 degrees to the left. Now let’s develop our star just a bit more. By using a 175-degree angle and looping 37 times, we can make a star with even more points, using this code:>>> t.reset()
>>> for x in range(1, 38): t.forward(100) t.left(175)
Here’s the result of running this code:
While we’re playing with stars, here’s the code to produce a spiraling star:
>>> t.reset()
>>> for x in range(1, 20): t.forward(100) t.left(95)
By changing the degree of the turn and reducing the number of loops, the turtle ends up drawing quite a different style of star:
More Turtle Graphics 149
Using similar code, we can create a variety of shapes, from a basic square to a spiral star. As you can see, by using for loops,
we’ve made it much simpler to draw these shapes. Without
for loops, our code would have
required a lot of tedious typing. Now let’s use an if state-
ment to control how the turtle will turn and draw another star variation. In this example, we want the turtle to turn one angle the first time, and then another angle the next time.
>>> t.reset() >>> for x in range(1, 19): t.forward(100) if x % 2 == 0: t.left(175) else: t.left(225)
Here, we create a loop that will run 18 times (range(1, 19)) and
tell the turtle to move forward 100 pixels (t.forward(100)). New here
is the if statement (if x % 2 == 0:). This statement checks to see if
the variable x contains an even number by using a modulo opera-
tor, the % in the expression x % 2 == 0, which is a way of saying, “x
mod 2” is equal to 0.
The expression x % 2 essentially says, “What is the amount
left over when you divide the number in variable x into two equal
parts?” For example, if we were to divide 5 balls into two parts, we would get two groups of 2 balls (making a total of 4 balls), and the remainder (the amount left over) would be 1 ball, as shown here.
2 2 5
This is the remainder.
If we divided 13 balls into two parts, we would get two groups of 6 balls with 1 ball remaining:
6 6 13
This is the remainder.
When we check to see if the remainder equals zero after divid- ing x by 2, we are actually asking whether it can be broken into
two parts with no remainder. This method is a nice way to see if a number in a variable is even, because even numbers can always be divided into two equal parts.
On the fifth line of our code, we tell the turtle to turn left 175 degrees (t.left(175)) if the number in x is even (if x % 2 == 0:);
otherwise (else), on the final line, we tell it to turn 225 degrees
(t.left(225)).
More Turtle Graphics 151
Drawing a Car
The turtle can do more than just draw stars and simple geometric shapes. For our next example, we’ll draw a rather primitive-looking car. First, we draw the body of the car. In IDLE, select File4New Window, and then enter the following code in the window.
t.reset() t.color(1,0,0) t.begin_fill() t.forward(100) t.left(90) t.forward(20) t.left(90) t.forward(20) t.right(90) t.forward(20) t.left(90) t.forward(60) t.left(90) t.forward(20) t.right(90) t.forward(20) t.left(90) t.forward(20) t.end_fill()
Next, we draw the first wheel.
t.color(0,0,0) t.up() t.forward(10) t.down() t.begin_fill() t.circle(10) t.end_fill()
Finally, we draw the second wheel.
t.setheading(0) t.up() t.forward(90) t.right(90) t.forward(10) t.setheading(0)
t.begin_fill() t.down() t.circle(10) t.end_fill()
Select File4Save As. Give the file a name, such as car.py.
Select Run4Run Module to try out the code. And here’s our car:
You may have noticed that a few new turtle functions have snuck into this code:
•
color is used to change the color of the pen.•
begin_fill and end_fill are used to fill in an area of the canvaswith a color.
•
circle draws a circle of a particular size.•
setheading turns the turtle to face a particular direction.Let’s take a look at how we can use these functions to add color to our drawings.