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

(vip2019) #1

164 Chapter 7


Figure 7-6: A smiley face drawn using the ClickSpiral.py app

RandomSpiralsFunction.py (page 145) contained a function
called random_spiral() that drew colorful spirals in random places
on the screen. Now, however, instead of spirals at random loca-
tions, we want a spiral to appear where the user clicks the mouse.
To do this, we can rewrite the random_spiral() function to take two
parameters, x and y, from the turtle.onscreenclick() listener. We’ll
rename the function spiral(x,y):

def spiral(x,y):
t.pencolor(random.choice(colors))
size = random.randint(10,40)
t.penup()
t.setpos(x,y)
t.pendown()
for m in range(size):
t.forward(m*2)
t.left(91)
Free download pdf