2

Anybody have any insight into controlling turtle graphics in python 2.7 with keyboard commands? I have done extensive research on this website and others and feel like I am doing the right thing but it just doesn't want to work for me. Below is what I have so far, can anyone tell me where I am going wrong????

from turtle import *
turtle.setup(500, 500)
wn = turtle.Screen()
wn.title("Turtle Keys")
move = turtle.Turtle()
showturtle()

def k1():
move.forward(45)

def k2():
move.left(45)

def k3():
move.right(45)

def k4():
move.back(45)

wn.onkey(k1, "Up")
wn.onkey(k2, "Left")
wn.onkey(k3, "Right")
wn.onkey(k4, "Down")

wn.listen()

4 Answers 4

6

When you specified import * you don't have to use turtle., also you have to use mainloop() read (infinite loop) to watch over user interactions, in your example wn is also unnecessary.

Here is the working code...

from turtle import *
setup(500, 500)
Screen()
title("Turtle Keys")
move = Turtle()
showturtle()

def k1():
    move.forward(45)

def k2():
    move.left(45)

def k3():
    move.right(45)

def k4():
    move.back(45)

onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")

listen()
mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Followed exactly and the code functions, as expected. But I tried getting to the turtle the way I'm familiar with. >>>import turtle >>>turtle.fd(0). Then I create a direction function and then call the onkey function passing the direction function and key name as parameters and I get a traceback error. Why does this happen?
See my answer about move = Turtle(); showturtle() as it explains why this causes two turtles to show on the screen instead of one. And your Screen() call adds nothing. As reasonable as I believe it is to import turtle * , since it's designed for beginners, I recommend an alternate approach in my answer to avoid these kinds of errors.
2
import turtle

image = "C:/Python27/PythonProgramming/picture.gif"
screenr = turtle.Screen()

Lewi = turtle.Turtle()

screenr.addshape(image)
Lewi.shape(image)

Lewi.penup()



def up():
    Lewi.sety(Lewi.ycor()+10)

def down():
    Lewi.sety(Lewi.ycor()-10)

def left():
    Lewi.forward(-10)

def right():
    Lewi.forward(10)





screenr.onkey(up, "Up")
screenr.onkey(down, "Down")
screenr.onkey(right, "Right")
screenr.onkey(left, "Left")
screenr.listen()

turtle.mainloop()

I just recently came up with this. Hope it helps!

1 Comment

The image.gif isn't necessary btw. If you want a turtle only, then just don't write it.
0

I found that with the code above and my example code, the keypresses were not registered until I clicked on the window. In my example, the turtle would move but the left/right action would not occur until I clicked on the window.

import turtle

def rightTurn():
   bob.rt(90)

def leftTurn():
   bob.lt(90)


wn=turtle.Screen()
wn.bgcolor('lightblue')

bob=turtle.Turtle()

wn.onkeypress(rightTurn, "Right")
wn.onkeypress(leftTurn, "Left")
wn.listen()


while True:
   bob.fd(1)

Comments

0

When you issue commands like this:

move = turtle.Turtle()
showturtle()

you're actually talking to two different turtles, your turtle object in 'move' and the default turtle. Most of the screen and default turtle methods can be called without an explicit object as they are also top level functions. To avoid confusion, I recommend you always import turtle this way:

from turtle import Turtle, Screen

and explicitly create your own turtle(s) and screen object. This way you won't be able to call the alternative functiions and won't get confused. Your example rewritten with the above in mind:

from turtle import Turtle, Screen

screen = Screen()

screen.setup(500, 500)
screen.title("Turtle Keys")

move = Turtle(shape="turtle")

def k1():
    move.forward(10)

def k2():
    move.left(45)

def k3():
    move.right(45)

def k4():
    move.backward(10)

screen.onkey(k1, "Up")
screen.onkey(k2, "Left")
screen.onkey(k3, "Right")
screen.onkey(k4, "Down")

screen.listen()

screen.exitonclick()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.