试图做动画,但程序将关闭之前,我

2024-06-23 18:50:55 发布

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

我是一个初学者,想知道是否有任何方法来完成当前的动画程序关闭前。下面是代码示例:

    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

基本上running=False代码将在动画(expl)开始之前运行。有没有更好的方式来充分展示这个动画?你知道吗


Tags: the方法代码程序false示例if动画
2条回答

这听起来像是使用回调函数的例子。Pygame的animation类具有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

可能您需要更多的修改,但其中之一是:

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

相关问题 更多 >

    热门问题