Python-Turtle - 按住一个键移动乌龟:释放键将乌龟向后移动而不是停止

Python-Turtle - Moving the turtle by holding down a key: releasing the key moves the turtle back instead of stopping it

我正在编写一个程序,通过按下箭头键使海龟向不同的方向移动。我希望能够通过按住相应的箭头键而不是反复按下来将其移动到特定方向。但是,当我按住箭头键几秒钟后松开它时,乌龟向后移动了一点,而不是立即停止。它向后移动的量取决于我按住键移动它的时间。

你能帮我解决这个问题或者建议用 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()

这花了一分钟时间才弄明白。不知道为什么 turtle.forward() 释放后会恢复到以前的位置。 Python 的 Turtle 模块有问题。它应该留在你放它的地方,但不管出于什么原因,它又冒出来了。

这会如您所愿。

#! /usr/bin/python3

from turtle import Turtle, Screen

turtle = Turtle()
screen = Screen()

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def right():
    turtle.setx( turtle.pos()[0] +25 )

def up():
    turtle.sety( turtle.pos()[1] +25 )

def left():
    turtle.setx( turtle.pos()[0] -25 )

def down():
    turtle.sety( turtle.pos()[1] -25 )

screen.onkeypress( right, "Right")
screen.onkeypress( up, "Up")
screen.onkeypress( left, "Left")
screen.onkeypress( down, "Down")

screen.listen()
screen.exitonclick()

##  eof  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

编辑:哦,是 setheading() 函数被重复导致挂断。
首先测试海龟的航向,然后只在需要时设置它。

#! /usr/bin/python3

from turtle import Turtle, Screen

turtle = Turtle()
screen = Screen()

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def right():
    if turtle.heading() != 0:  turtle.setheading(0)
    turtle.forward(25)

def up():
    if turtle.heading() != 90:  turtle.setheading(90)
    turtle.forward(25)

def left():
    if turtle.heading() != 180:  turtle.setheading(180)
    turtle.forward(25)

def down():
    if turtle.heading() != 270:  turtle.setheading(270)
    turtle.forward(25)

screen.onkeypress( right, "Right")
screen.onkeypress( up, "Up")
screen.onkeypress( left, "Left")
screen.onkeypress( down, "Down")

screen.listen()
screen.exitonclick()

##  eof  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

您可以通过将 screen.tracer() 方法设置为 0 来使用它。这样,您还需要在每次海龟移动时更新屏幕:

from turtle import Turtle, Screen

def move_right():
    turtle.setheading(0)
    turtle.forward(25)
    screen.update()

def move_up():
    turtle.setheading(90)
    turtle.forward(25)
    screen.update()

def move_left():
    turtle.setheading(180)
    turtle.forward(25)
    screen.update()

def move_down():
    turtle.setheading(270)
    turtle.forward(25)
    screen.update()

turtle = Turtle()
screen = Screen()
screen.tracer(0)
screen.onkeypress(move_right, "Right")
screen.onkeypress(move_up, "Up")
screen.onkeypress(move_left, "Left")
screen.onkeypress(move_down, "Down")

screen.listen()
screen.exitonclick()

您还可以使用 lambda 函数来缩短您的代码:

from turtle import Turtle, Screen

def f(num):
    turtle.setheading(num)
    turtle.forward(25)
    screen.Screen.update()

turtle = Turtle()
screen = Screen()
screen.tracer(0)
screen.onkeypress(lambda: f(0), "Right")
screen.onkeypress(lambda: f(90), "Up")
screen.onkeypress(lambda: f(180), "Left")
screen.onkeypress(lambda: f(270), "Down")

screen.listen()
screen.exitonclick()

请注意,将您的 Turtle 对象命名为 turtle 并不是最佳选择,因为它可能会与 turtle 模块混淆。