简体   繁体   English

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

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

I am experimenting with eclipse and I came across a strange occurrence which I am unable to fix.我正在试验 eclipse,但遇到了一个我无法修复的奇怪事件。 The while loop only works if there was a print statement. while 循环仅在有打印语句时才有效。 However, once I remove it, the while loop cease to work (in this case, it stops updating the turtle).但是,一旦我删除它,while 循环就会停止工作(在这种情况下,它会停止更新乌龟)。 I have tried google for this problem to no avail.我已经尝试谷歌解决这个问题无济于事。 Hence, I would like to seek for someone's help regarding this.因此,我想就此寻求某人的帮助。 Many thanks in advance :)提前谢谢了 :)

PS.附注。 I only extracted the essential code from my script to upload.我只从我的脚本中提取了必要的代码来上传。 There are no errors currently.目前没有错误。

# 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)

I realized I made a very novice mistake after playing around abit.在玩了 abit 之后,我意识到我犯了一个非常初级的错误。 decay_wheel_spinforce() will run so fast as it is unrestricted and will quickly be set to 0. This problem was solved by including a time.sleep(0.01) to make sure that the function does not run too fast. decay_wheel_spinforce() 将运行得如此之快,因为它不受限制,并将迅速设置为 0。通过包含 time.sleep(0.01) 以确保函数不会运行得太快,解决了这个问题。

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

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