Keyboard Commands with Python Turtle
- Step 1 - Create a new python (with turtle program) called "yourname_dragging", set the speed to "fast", the shape to "turtle", and the color to "blue"
import turtle
t=turtle.Turtle() t.shape("turtle") t.speed("fast") t.color("blue") |
- Step 2 - Create a function that will repeatedly move the turtle to the new coordinates as you drag the turtle /cursor around the screen
#function to move turtle up
def goto(x,y): t.goto(x,y) |
- Step 3 - Call the "goto" function in the main program
# main program
t.ondrag(goto) |
- Step 4 - Some options to try:
- Modify the program to change the color of the turtle by pressing different keys on the keyboard (Ex. r = "red", b = "blue", g = "green")
- Change the background color of the screen
- Modify the size or thickness of the pen
- Use the predefined clear( ) function to clear the screen when a key such as the space bar (" ") is pressed
- Step 6 - Try using the onclick and setposition commands to create a snapping tool that relocates the turtle / cursor to wherever the user clicks on the screen. The turtle’s pen is down by default, so when the user clicks somewhere on the drawing window, the turtle moves there and draws a line from its old location to the location where the user clicked.
- Step 7 - Make sure to embed your program in your weebly page