pygame中动画的并行显示更新

时间:2016-11-03 10:22:47

标签: python pygame

我正在使用pygame创建一个游戏,它到目前为止一直有效。

然而,我通过循环浏览我从spritesheet创建的图像,包含了一个爆炸动画。 当这个动画正在运行时,游戏中的其他所有内容都会停止,并且只有在动画完成后才会继续运行:

class Explosion(Thread):
 def __init__(self, coordinates):
    Thread.__init__(self)
    self.x, self.y = coordinates
    self.explosionimgs = []
    for i in range(0, 47):
        self.explosionimgs.append(pygame.image.load("resources/explosion2/%d.png" % (i)))
    self.explosionimg = self.explosionimgs[0]

 def run(self):
    for j in range (0,47):
        screen.blit(self.explosionimgs[j], (self.x-80, self.y-80))
        pygame.display.update()

有没有办法让游戏中的其他所有内容继续运行,而动画正在进行中?

1 个答案:

答案 0 :(得分:3)

  

有没有办法让游戏中的其他所有内容继续运行,而动画正在进行中?

是。除了游戏的主循环之外,不要创建另一个循环。

一种简单的方法是使用pygame的Sprite类来表示游戏对象,因为它很好地结合了RectSurface并且可以轻松地与{{1 }}

这是一个愚蠢简单的可运行的例子。注意一切都在继续移动,而Groups类一直在追逐它的形象。

只需从spritesheet设置图像,而不是使用Explosion

pygame.draw.circle
相关问题