python - 如何在 Repl 中删除 Python(使用海龟)中的重复延迟?
问题描述
我正在制作一个游戏,您可以在其中使用箭头键来移动您的角色。每次我按住任何键时,它都不会重复,直到一秒钟后。我想这样做,所以每当我按住右箭头键时,它会继续重复,没有一秒钟的延迟。
我正在使用 Python Turtle,我试图找出这个问题一个星期,但我找不到任何东西来解决这个问题。
import turtle
p = turtle.Turtle()
p.shape("turtle")
p.speed(0)
screen = turtle.Screen()
def up():
p.forward(10)
def left():
p.right(-10)
def right():
p.right(10)
screen.onkey("Up",up)
screen.onkey("Left",left)
screen.onkey("Right",right)
screen.listen()
(一个月后仍然没有答案)
解决方案
我通过添加“向后”函数并在“左”函数上使用“左()”方法编写了一个稍微不同的海龟程序。该代码能够通过按住箭头键来实现连续移动。
import turtle
t=turtle.Turtle()
t.penup()
t.speed(0)
LoadWindow=turtle.Screen()
LoadWindow.setup(width=1000, height=1000)
def f():
t.forward(10)
def l():
t.left(10)
def r():
t.right(10)
def b():
t.forward(-10)
LoadWindow.onkey(f,"Up")
LoadWindow.onkey(l,"Left")
LoadWindow.onkey(r,"Right")
LoadWindow.onkey(b,"Down")
LoadWindow.listen()
LoadWindow.exitonclick()
此外,我将您的代码导入 Repl.it 和 Trinket,它也运行良好,这可能意味着您与网站的连接可能存在问题,或者您的计算机由于某种原因运行缓慢。
如果问题仍然存在,我建议尝试不同的 python 浏览器程序,例如:
祝你好运!
推荐阅读
- python - 考虑python中的键/列顺序计算字典和数据框之间的匹配值
- sql - 如何修复元数据库中的“缺少表的 FROM 子句条目”错误
- vba - Prompt for verifying recipients
- matrix - 如何进行 4 维旋转?
- spring - 在应用程序上下文中将 Spring Boot 2.1.9 碰撞到 2.2.0 没有 AuthenticationManager
- html - 如何修改下拉列表?
- javascript - ngClass 混合条件
- python - 将图像保存到后台任务抓取的 ImageField
- mongodb - Pentaho 数据集成多选参数
- deep-learning - 将cntk模型v1转换为cntk模型v2