首页 > 解决方案 > 如何在 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()

(一个月后仍然没有答案)

标签: pythonpython-3.xturtle-graphics

解决方案


我通过添加“向后”函数并在“左”函数上使用“左()”方法编写了一个稍微不同的海龟程序。该代码能够通过按住箭头键来实现连续移动。

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 浏览器程序,例如:

祝你好运!


推荐阅读