148 Chapter 7
To fill the circle with yellow, we add four lines of code
around our t.circle(50) command. First, we set the pen color to
yellow with t.pencolor("yellow"). Second, we set the fill color with
t.fillcolor("yellow"). Third, before we call the t.circle(50) com-
mand to draw the face of our smiley, we tell the computer that we
want to fill the circle we’re drawing. We do this with the t.begin_
fill() function. Finally, after we draw our circle, we call the
t.end_fill() function to tell the computer that we’re done with the
shape that we want to fill with color.
Drawing eyes
First, we need to figure out where to position the turtle to draw
the left eye in the correct place, then set the fill color to blue,
and finally draw a circle of the correct size. The eyes are about
20 pixels (two grid lines) tall, and we know that a diameter of 20
means that we need a radius of half that amount, or 10, so we’ll
use the t.circle(10) command to draw each eye. The tricky part is
deciding where to draw them.
Our (x, y) starting point will be the local origin of each smiley
face, and you can locate the left eye in Figure 7-1. It looks like it
starts about 6 grid lines above the origin (60 pixels up, in the posi-
tive y-direction), and it’s sitting about 1.5 grid lines to the left of
the y-axis (or about 15 pixels left, in the negative x-direction).
To tell our program how to get to the right place to draw the
left eye, starting from the bottom of the big yellow circle at a given
(x, y) passed to our function as a pair of arguments, we need to
start at x and move left 15 pixels, start at y and move up 60 pixels,
or move to (x-15, y+60). So, calling t.setpos(x-15, y+60) should put
the turtle where we need to start drawing our left eye. Here’s the
code for the left eye:
# Left eye
t.setpos(x-15, y+60)
t.fillcolor("blue")
t.begin_fill()
t.circle(10)
t.end_fill()
An easy mistake might be writing the setpos command with
just (–15, 60) as arguments, but remember that we want to draw
lots of smiley faces at various (x, y) positions on the screen; not all