簡體   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