turtle key listener code example
Example 1: screen.onkey python
from turtle import Turtle, Screen
screen = Screen()
screen.setup(1200, 500)
ground = Turtle()
ground.speed('fastest')
ground.penup()
ground.goto(-1000, -200)
ground.pendown()
ground.forward(2000)
player = Turtle()
player.speed('fastest')
PlayerX = -600
def moveX():
global PlayerX
screen.onkeypress(None, "w")
player.clear()
player.penup()
player.goto(PlayerX, -99)
player.pendown()
player.color("Slate Gray")
player.begin_fill()
player.circle(-50)
player.end_fill()
PlayerX -= 1
screen.onkeypress(moveX, "w")
screen.listen()
moveX()
screen.mainloop()
Example 2: how to add keyboard to python turtle
turtle.listen()
turtle.onkey(def, 'key')