proffreda

turtle-etch-sketch.py

Jun 22nd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import turtle
  2.  
  3. turtle.setup(400,500) # Determine the window size
  4. wn = turtle.Screen() # Get a reference to the window
  5. wn.title("Handling keypresses!") # Change the window title
  6. wn.bgcolor("lightgray") # Set the background color
  7. tess = turtle.Turtle() # Create our favorite turtle
  8.  
  9. # The next four functions are our "event handlers".
  10. def h1():
  11. tess.forward(30)
  12.  
  13. def h2():
  14. tess.left(45)
  15.  
  16. def h3():
  17. tess.right(45)
  18.  
  19. def h4():
  20. wn.bye() # Close down the turtle window
  21.  
  22. # These lines "wire up" keypresses to the handlers we've defined.
  23. wn.onkey(h1, "Up")
  24. wn.onkey(h2, "Left")
  25. wn.onkey(h3, "Right")
  26. wn.onkey(h4, "q")
  27.  
  28. # Now we need to tell the window to start listening for events,
  29. # If any of the keys that we're monitoring is pressed, its
  30. # handler will be called.
  31. wn.listen()
  32. turtle.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment