Turtle graphics

Update for 2011: Be aware of these turtle commands to reset things:  clear(), reset() and bye()



Today I started using the turtle module in class to entice the students to play around with more than just arithmetic and prints. It makes a couple of behind-the-scenes simplifications to allow the user (programmer) to get up and running right away and it also keeps the internal turtle objects more persistant than you might expect. (For those in the know, it instantiates a singleton of the turtle object called turtle.)

turtle.goto(0,0) # put turtle at 0,0 (center of canvas)
turtle.forward(100) # move 100 steps forward
turtle.left(45) #rotate 45 degrees to left
turtle.forward(100)# move another 100 steps forward 


It seems that on Windows (XP?), if you re-run the turtle script several times (hit F5 a couple of times) to simulate looping over these lines can you create a star of sorts:  



You can add clear(), reset() or (bye) at the ned to prevent this or you could restart the python shell first (Control-F6 in python shell) before running the program (F5 in editor). This is also good to know in case things get mysteriously "stuck" :)






As the turtle's graphics refresh behavior can be pretty dumb, I recommend moving all windows (including Idle) out of the center of you desktop so that when the turtle graphics window pops up, there's nothing else that obscures it:









There's a turtle IDE here : http://pythonturtle.org/  (Thanks, Joseph)










No comments:

Search This Blog