Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试制作动画,但程序将在此之前关闭_Python_Pygame - Fatal编程技术网

Python 正在尝试制作动画,但程序将在此之前关闭

Python 正在尝试制作动画,但程序将在此之前关闭,python,pygame,Python,Pygame,我是一个初学者,我想知道是否有办法在节目结束前完成当前的动画。以下是代码示例: if playerHealth <= 0: # If the player dies expl = Explosion(hit.rect.center, 'lg') all_sprites.add(expl) # Show animation of him blowing up running = False # End the game 如果playe

我是一个初学者,我想知道是否有办法在节目结束前完成当前的动画。以下是代码示例:

    if playerHealth <= 0: # If the player dies
        expl = Explosion(hit.rect.center, 'lg')
        all_sprites.add(expl) # Show animation of him blowing up
        running = False # End the game

如果playerHealth可能需要更多修改,但其中之一是:

if playerHealth <= 0 and not blowing_up_animation_started: # If the player dies
    expl = Explosion(hit.rect.center, 'lg')
    all_sprites.add(expl) # Show animation of him blowing up

    blowing_up_animation_started = True
    blowing_up_animation_finished = False

if blowing_up_animation_finished:
    running = False # End the game

如果playerHealth这听起来像是使用回调函数的例子。Pygame的类具有
on_finished
属性,该属性旨在分配给回调。一旦动画完成播放,就会调用回调函数,从而停止游戏。下面是一个爆炸类的示例

class Explosion:

    def __init__(self, rect, size, cb_func):

         self.animate_explosion(cb_func)

    ...

    def animate_explosion(self, cb_func):
         # start animation here

         ...

         # when animation finishes
         self.on_finished = cb_func()
在你的游戏逻辑中,你有如下的东西:

def callback():
     running = False

if playerHealth <= 0: # If the player dies
     expl = Explosion(hit.rect.center, 'lg', callback())
     all_sprites.add(expl) # Show animation of him blowing up
def callback():
运行=错误

如果playerHealth从未对游戏进行编程或查看pygame,那么延迟又如何
import time.sleep(5)#5秒
事实上,如果您查看
pygame
文档,其中有一个模块带有
pygame
。它的功能可能会对您有所帮助。例如:
pygame.time.wait()
pygame.time.delay()