简体   繁体   中英

How to use letter keys in turtle?

Is it possible to use other keys such wasd in turtle besides the arrow keys for commands? Can I make the turtle move by using the (wasd) keys? or can i use the "A" key to run turtle.penup().= turtle.penup()

You can use "a" to run command - even documentation for onkey() mentions char "a" as example

import turtle

def forward():
    turtle.setheading(90)
    turtle.forward(100)

def backward():
    turtle.setheading(270)
    turtle.forward(100)

def left():
    turtle.setheading(180)
    turtle.forward(100)

def right():
    turtle.setheading(0)
    turtle.forward(100)

turtle.onkey(forward, 'w')
turtle.onkey(backward, 's')
turtle.onkey(left, 'a')
turtle.onkey(right, 'd')

turtle.listen()

#turtle.mainloop()
turtle.exitonclick()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM