150 Chapter 7
these pairs of points are mirror images of one another across the
y-axis; this makes our smiley face nice and even.
Here’s the code for the mouth:
# Mouth
t.setpos(x-25, y+40)
t.pencolor("black")
t.width(10)
t.goto(x-10, y+20)
t.goto(x+10, y+20)
t.goto(x+25, y+40)
u t.width(1)
After we set the turtle at the upper-left corner of the mouth, we
change the pen color to black and the width to 10. We start draw-
ing by telling the turtle to go to each of the other three points of
the smile. The turtle module’s goto() function does the same thing
as setpos(): it moves the turtle to a given point. I’m using it here
just so you can see there’s an alternative to setpos(). Finally, at u,
t.width(1) sets the pen width back down to 1 so that our shapes
aren’t too thick when the next face is drawn.
Defining and calling draw_smiley()
All that remains is to define the draw_smiley() function with all the
code to draw a smiley face, set up a loop to generate 50 random
(x, y) locations on the screen, and call the draw_smiley(x,y) function
to draw smileys at all 50 locations.
The function definition for draw_smiley() will need to take two
parameters, x and y, for the location where the smiley is to be
drawn, and it will need to lift the turtle’s pen, move the turtle
to that (x, y) position, and then put the pen back down to get
ready to draw. After that, we just need to add our code snippets
for drawing the big yellow face, the left and right eyes, and the
mouth.
def draw_smiley(x,y):
t.penup()
t.setpos(x,y)
t.pendown()
# All of your drawing code goes here...