【发布时间】:2021-05-18 05:38:04
【问题描述】:
我正在编写一个程序,通过按下箭头键将海龟向不同方向移动。我希望能够通过按住相应的箭头键而不是反复按下它来将其移动到特定方向。但是,当我在按住箭头键几秒钟后松开箭头键时,乌龟会向后移动一点,而不是立即停止。它向后移动的量取决于我按住键移动它的时间。
你能帮我解决这个问题或建议用turtle模块实现这个问题的另一种方法吗?
注意:我观察到当我按住键时,直到我松开它才会画线。我不确定它是否与此问题有关。
注意 2:我正在使用 onkeypress 方法来处理“按住键”事件。我尝试使用 onkeyrelease(None, arrow_key) 方法来解决这个问题,但它也不起作用。
这是我的代码:
from turtle import Turtle, Screen
def move_right():
turtle.setheading(0)
turtle.forward(25)
def move_up():
turtle.setheading(90)
turtle.forward(25)
def move_left():
turtle.setheading(180)
turtle.forward(25)
def move_down():
turtle.setheading(270)
turtle.forward(25)
turtle = Turtle()
screen = Screen()
screen.onkeypress(move_right, "Right")
screen.onkeypress(move_up, "Up")
screen.onkeypress(move_left, "Left")
screen.onkeypress(move_down, "Down")
screen.listen()
screen.exitonclick()
【问题讨论】:
标签: python python-3.x turtle-graphics python-turtle