繁体   English   中英

Eclipse:除非有打印语句,否则while循环不起作用

[英]Eclipse: while-loop does not work unless there is a print statement

我正在试验 eclipse,但遇到了一个我无法修复的奇怪事件。 while 循环仅在有打印语句时才有效。 但是,一旦我删除它,while 循环就会停止工作(在这种情况下,它会停止更新乌龟)。 我已经尝试谷歌解决这个问题无济于事。 因此,我想就此寻求某人的帮助。 提前谢谢了 :)

附注。 我只从我的脚本中提取了必要的代码来上传。 目前没有错误。

# Initialise frame-tracking for pointer drawing
secondPerFrame = 0.04
nextFrame =0

# Initialise screen
screen = turtle.Screen()
screen.setup(600,600)
screen.tracer(0)
screen.colormode(255)

# Initialise turtle for drawing of pointer
pointerTurt = turtle.Turtle()
pointerTurt.speed(0)
pointerTurt.width(10)
pointerTurt.hideturtle()

# Initialise variables
wheelRadius = 250
startForce = 30

def update_pointer_direction():
    pointerTurt.clear()   
    pointerTurt.goto(0,0)
    pointerTurt.pendown()
    pointerTurt.forward(wheelRadius *0.75)
    pointerTurt.penup()
    screen.update()

def decay_wheel_spinforce():
    global startForce
    if (startForce > 0):
        partialStartForce = startForce * 0.005
        startForce -= random.random() * partialStartForce

    if (startForce < 0.005):
        startForce =0

while True :

    if (nextFrame < time.perf_counter()):        
        update_pointer_direction()       
        nextFrame = time.perf_counter() + secondPerFrame

    print()
    decay_wheel_spinforce()         
    pointerTurt.left(startForce)

在玩了 abit 之后,我意识到我犯了一个非常初级的错误。 decay_wheel_spinforce() 将运行得如此之快,因为它不受限制,并将迅速设置为 0。通过包含 time.sleep(0.01) 以确保函数不会运行得太快,解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM