Hamish Brown
Hamish Brown

Reputation: 21

Python Turtle Graphics Keyboard Commands

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()

Upvotes: 2

Views: 55153

Answers (4)

cdlane
cdlane

Reputation: 41905

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()

Upvotes: 0

dougc905
dougc905

Reputation: 93

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)

Upvotes: 0

Izak the coder
Izak the coder

Reputation: 75

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!

Upvotes: 2

That_User
That_User

Reputation: 929

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()

Upvotes: 6

Related Questions