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

(vip2019) #1
Functions: There’s a Name for That 159

Figure 7-3: A TurtleDraw.py sketch (there’s a reason
I’m an author and not an artist)


The reason this works is that we’ve told the computer to do
something when the user clicks the mouse on the screen: set the
position of the turtle to that location. The turtle’s pen is down by
default, so when the user clicks on the drawing window, the turtle
moves there and draws a line from its old location to the location
where the user clicked.
You can customize TurtleDraw.py by changing the background
color of the screen, the turtle’s pen color, the width of the pen, and
more. Check out the version my four-year-old son created (with
some help from his dad):


TurtleDrawMax.py


import turtle
t = turtle.Pen()
t.speed(0)
turtle.onscreenclick(t.setpos)
turtle.bgcolor("blue")
t.pencolor("green")
t.width(99)


Max liked the drawing program (a lot), but he wanted the
screen to be blue and the pen to be green and really thick, so
we set the bgcolor(), pencolor(), and width() to blue, green, and 99 ,
respectively. We made an arbitrary choice to set these after we told
the computer what to do with mouse clicks on the screen (t.setpos).

Free download pdf