简体   繁体   中英

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. The while loop only works if there was a print statement. However, once I remove it, the while loop cease to work (in this case, it stops updating the turtle). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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