长时间停止功能,而不让它运行到最后

2024-09-23 22:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想弄清楚这个问题和所有其他问题之间的区别:

我想打破一个很长的函数,它是这样开始的:

while running:
    framerate += 1.0
    if Counter > 0.0:

但是我不想仅仅使用running=False来停止它,因为这样已经触发的最后一个while循环仍然会完成并运行到最后。 我需要这个来微调帧速率,所以循环必须停止在while循环中的位置,然后从头开始重新启动

既然while循环很长,我真的不想添加一个

if running==False:
       break

到while循环中的每个表达式

有没有什么方法可以在不向while循环中添加几十个break表达式的情况下脱离while循环

如果您想要while代码的示例,请参见:

running=True

while running:
    framerate += 1.0
    if Counter > 0.0:
        SetCounter()
    elif Counter == 0.0:
        newframerate += 1
        Counter = -1.0
        #Check for collisions:
        for enemy in enemies:
            for bullet in bullets:
                if enemy.Collides(bullet):
                    if enemy.state=="damaged":
                        enemy.lt(random.randint(-10,10))
                    else:
                        enemy.state="damaged"
                        enemy.speed=0.15
                        enemy.shapesize(0.5, 0.5, 10)
                        enemy.color("black")
                    bullet.status="ready"
                    bullet.ht()
                    Sparks.explode(enemy)
            if(enemy.Collides(spaceship)):
                if enemy.state=="whole":
                    if not triggerexplosiononce:
                        restart=True
                        Sparks.explode(spaceship)
                        triggerexplosiononce=True
                elif enemy.state=="damaged":
                    metal+=1
                    enemy.ht()
                    enemy.state="done"
                    enemy.setposition(10000,10000)
                    size+=0.05
                    spaceship.shapesize(size,size*1.4, 1)
                    if exhaustoffset<15:
                        exhaustoffset+=0.05
                        exhaustoffset*=1.4
                    else:
                        exhaustoffset+=0.1
                    #gun.shapesize(size*1.4,size*0.3, 1)
                    shipwindow.shapesize(size*0.3,size*0.3, 1)
        #Speed up the game:
        window.update()
        #automatically MOVE THE ENEMY
        for enemy in enemies:
            if enemy.isvisible():# and enemy.state=="whole":
                enemy.move()
        #Move the bullet
        for bullet in bullets:
            bullet.move()

        #Gun Rotation (gun used as fire exhaust for now
        #if keyboard.is_pressed("c"):# and keyboard.is_pressed("ctrl"):
            #gun.Turn("Left")
        #if keyboard.is_pressed("v"):# and keyboard.is_pressed("Ctrl"):
            #gun.Turn("Right")

        #Reset Once functionality
        if  spaceship.xcor()>-349 and spaceship.xcor()<349 \
        and spaceship.ycor()>-349 and spaceship.ycor()<349:
            Once=False
        #Breaks
        if keyboard.is_pressed("b") and spaceship.speed>0.1:# and keyboard.is_pressed("ctrl"):
            spaceship.deccelerate(4)

        #SPACESHIP MOVEMENT
        if spaceship.xcor()>-350 and spaceship.xcor()<350 \
        and spaceship.ycor()>-350 and spaceship.ycor()<350:
            #Rotate the Spaceship
            if keyboard.is_pressed("Left"):
                spaceship.Turn("Left")
                #Drone Bullet Experiment
                #bullet.Turn("Left")

            if keyboard.is_pressed("Right"):
                spaceship.Turn("Right")
                # Drone Bullet Experiment
                #bullet.Turn("Right")

            #Motor
            if keyboard.is_pressed("Up"):
                gun.showturtle()
                if spaceship.speed<-0.1*spaceship.speedlimit:
                    spaceship.accelerate(4)
                elif spaceship.speed >= -0.1*spaceship.speedlimit and spaceship.speed <0.0:
                    spaceship.speed=0.1*spaceship.speedlimit
                elif spaceship.speed<spaceship.speedlimit:
                    spaceship.accelerate()
                if  spaceship.xcor()>-349 and spaceship.xcor()<349 \
                and spaceship.ycor()>-349 and spaceship.ycor()<349:
                    spaceship.fd(spaceship.speed)
                else:
                    if Once==False:
                        Once=True
                    spaceship.fd(spaceship.speed/spaceship.speedlimit)

            elif keyboard.is_pressed("Down"):
                gun.hideturtle()
                if spaceship.speed>0.1*spaceship.speedlimit:
                    spaceship.deccelerate(4)
                elif spaceship.speed <= 0.1*spaceship.speedlimit and spaceship.speed >0.0:
                    spaceship.speed=-0.1*spaceship.speedlimit
                elif spaceship.speed>-spaceship.speedlimit:
                    spaceship.deccelerate()
                if  spaceship.xcor()>-349 and spaceship.xcor()<349 \
                and spaceship.ycor()>-349 and spaceship.ycor()<349:
                    spaceship.fd(spaceship.speed)
                else:
                    if Once==False:
                        Once=True
                    spaceship.fd(spaceship.speed/spaceship.speedlimit)

            elif spaceship.speed>0.1*spaceship.speedlimit or spaceship.speed<-0.1*spaceship.speedlimit:
                gun.hideturtle()
                if spaceship.speed>0.1*spaceship.speedlimit:
                    spaceship.deccelerate()
                elif spaceship.speed<-0.1*spaceship.speedlimit:
                    spaceship.accelerate()
                if spaceship.xcor() > -349 and spaceship.xcor() < 349 \
                and spaceship.ycor() > -349 and spaceship.ycor() < 349:
                    spaceship.fd(spaceship.speed)
                else:
                    if Once == False:
                        Once = True
                        # spaceship.speed = 0.1
                    spaceship.fd(spaceship.speed / spaceship.speedlimit)

        #Autorotation against borders
        elif spaceship.xcor()<-349:
            spaceship.setx(-349)
            if keyboard.is_pressed("Right")==False and spaceship.heading()>180 and spaceship.heading()<270:
                spaceship.lt(1*spaceship.speedlimit)
            elif keyboard.is_pressed("Left")==False and spaceship.heading()<180 and spaceship.heading()>90:
                spaceship.rt(1*spaceship.speedlimit)
        elif spaceship.xcor()>349:
            spaceship.setx(349)
            if keyboard.is_pressed("Right")==False and spaceship.heading()>0 and spaceship.heading()<90:
                spaceship.lt(1*spaceship.speedlimit)
            elif keyboard.is_pressed("Left")==False and spaceship.heading()<360 and spaceship.heading()>270:
                spaceship.rt(1*spaceship.speedlimit)
        elif spaceship.ycor()<-349:
            spaceship.sety(-349)
            if keyboard.is_pressed("Right")==False and spaceship.heading()>270 or spaceship.heading()<90:
                spaceship.lt(1*spaceship.speedlimit)
            elif keyboard.is_pressed("Left")==False:
                spaceship.rt(1*spaceship.speedlimit)
        elif spaceship.ycor()>349:
            spaceship.sety(349)
            if keyboard.is_pressed("Right")==False and spaceship.heading()>90 and spaceship.heading()<270:
                spaceship.lt(1*spaceship.speedlimit)
            elif keyboard.is_pressed("Left")==False:
                spaceship.rt(1*spaceship.speedlimit)

        #keyboard.on_press_key("q", Do)
        #keyboard.on_release_key("q", Do)

        #Teleportation Grow effect of the Beamer window:
        if dummygrow==True and growcounter<10:
            dummylengths+=0.05
            dummy.turtlesize(dummylengths,dummylengths,1)
            growcounter+=1
        elif dummygrow==True and growcounter>9:
            dummylengths=0.3
            growcounter=0
        elif dummygrow==False:
            pass

        #Explosion: Move the sparks:

        if Sparks.sparksmoving==True and movecounter < 25:
            movesparks()
            movecounter+=1
        elif Sparks.sparksmoving==True and movecounter>24:
            Sparks.sparksmoving=False
            movecounter=0
            for spark in sparksgroup:
                spark.ht()
                if restart==True:
                    spaceship.setposition(0, 0)
                    spaceship.setheading(90)
                    spaceship.speed=0
                    for enm in enemies:
                        if enm.isvisible():
                            enm.showturtle()
                            enm.setposition(random.randint(-349, 349), random.randint(-349, 349))
                            if enm.xcor()<50 and enm.xcor()>-50\
                            and enm.ycor()<50 and enm.ycor()>-50:
                                enm.fd(100)
                    triggerexplosiononce=False
                    restart=False

        #Gun and Window Follow Position and Rotation:
        gun.setheading(spaceship.heading())
        gun.setposition(spaceship.xcor(),spaceship.ycor())
        gun.fd(-exhaustoffset)
        shipwindow.setposition(spaceship.xcor(),spaceship.ycor())

        Once=False
        Counter=Delay```



Tags: andfalsetrueifisspeedelifpressed
3条回答

我想你不能到处走动 a) 确定断裂条件 b) 如果它被满足,它就会崩溃

您的方法是通过设置标志,然后在循环的while(或repeat)端检查标志来中断

我看不到在您提供的循环片段中设置了“running”标志,因此很难为您的案例提供特定于代码的示例,但也许这样可以:

您可以在不设置中断标志的情况下中断循环:如果满足条件,只需声明“中断”:

https://docs.python.org/2.0/ref/break.html

您还可以将循环放入函数中,并在检测到中断条件时执行早期“返回”。这通过删除所有临时变量很好地清理了您的范围

您可以将循环放在迭代器中,并引发StopIteration异常以发出迭代结束的信号

https://docs.python.org/3.9/library/exceptions.html

但在所有情况下,你都不能在每一个重要的地方检查一个条件,然后执行一个中断、停止或返回

有时有效的方法是,如果循环很长,可以重新计算代码的系数,以减少执行检查的点

如果不需要太多代码,可以删除while running,使用while True,而不使用if running == False: break,只使用break(如果没有帮助,很抱歉)

也许你可以用一根线来做这个。启动一个线程来执行循环操作,并在时间结束时停止。 参考本条https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/

相关问题 更多 >