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