[英]Python-Turtle - Moving the turtle by holding down a key: releasing the key moves the turtle back instead of stopping it
I'm writing a program that moves the turtle in different directions by pressing down the arrow keys.我正在编写一个程序,通过按下箭头键将海龟向不同方向移动。 I want the ability to move it in a particular direction by holding down the respective arrow key instead of pressing it repeatedly.
我希望能够通过按住相应的箭头键而不是反复按下它来将其移动到特定方向。 However, when I release the arrow key after holding it down for few seconds, the turtle is moving back a bit instead of stopping immediately.
但是,当我在按住箭头键几秒钟后松开箭头键时,乌龟会向后移动一点,而不是立即停止。 The amount by which it moves back depends on how long I held down the key to move it.
它向后移动的量取决于我按住键移动它的时间。
Can you help me to solve this issue or suggest another way to implement this with the turtle module?你能帮我解决这个问题或建议用turtle模块实现这个的另一种方法吗?
Note: I observed that when I hold down the key, the line is not drawn until I release it.注意:我观察到当我按住键时,直到我松开它才会画线。 I'm not sure if it's expected or related to this issue.
我不确定这是预期的还是与此问题有关。
Note 2: I'm using the onkeypress method for handling the "holding down the key" event.注 2:我正在使用 onkeypress 方法来处理“按住键”事件。 I tried using the onkeyrelease(None, arrow_key) method to solve this, but it doesn't work either.
我尝试使用 onkeyrelease(None, arrow_key) 方法来解决这个问题,但它也不起作用。
Here is my code:这是我的代码:
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()
That took a minute to figure out.花了一分钟才弄清楚。 Don't know why
turtle.forward()
would revert to a previous position after releasing.不知道为什么
turtle.forward()
释放后会恢复到以前的position。 Python's Turtle module is buggy. Python 的 Turtle 模块有问题。 It should stay where ya put it, but for whatever reason, it's bubbling back.
它应该留在你放置的地方,但无论出于何种原因,它都在冒泡。
This'll do what you expect.这会如你所愿。
#! /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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Edit: oh, it's the setheading()
function being repeated that causes the hangup.编辑:哦,是
setheading()
function 重复导致挂断。
Test for the turtle's heading first, then only set it when required.首先测试海龟的航向,然后仅在需要时进行设置。
#! /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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use the screen.tracer()
method by setting it to 0
.您可以通过将
screen.tracer()
方法设置为0
来使用它。 With that you'll also need to update the screen every time the turtle makes a move:这样,您还需要在每次海龟移动时更新屏幕:
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()
You can also use lambda
functions to shorten your code:您还可以使用
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()
Do note that it's not optimal to name your Turtle
object turtle
, as it can be confused as the turtle
module.请注意,将
Turtle
命名为 object turtle
并不是最佳选择,因为它可能会与turtle
模块混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.