【发布时间】:2021-07-24 09:45:39
【问题描述】:
我对编码还很陌生,我正在使用 pygame 开发一个小游戏。我有一个用于爆炸动画的精灵,第一次出现时它对整个游戏都很好,但是一旦你玩第二或第三场游戏,即使每次游戏重新开始时我都清空精灵组,每次爆炸都会持续更多,我认为问题可能是爆炸更新功能对一个以上的游戏效果不佳,但我真的不知道如何修复它。提前感谢您的帮助!
class Explosion(pygame.sprite.Sprite):
expl_img = []
def __init__(self, center):
pygame.sprite.Sprite.__init__(self)
self.image = self.cargaImagenes()
self.rect = self.image.get_rect()
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
self.frame_rate = 50
def update(self):
now = pygame.time.get_ticks()
if now -self.last_update > self.frame_rate:
self.last_update = now
self.frame +=1
if self.frame == len(self.expl_img):
self.kill()
else:
center = self.rect.center
self.image = self.expl_img[self.frame]
self.rect = self.image.get_rect()
self.rect.center = center
def cargaImagenes(self):
for i in range(9):
filename = 'regularExplosion0{}.png'.format(i)
img = pygame.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
img_def = pygame.transform.scale(img, (75, 75))
self.expl_img.append(img_def)
return self.expl_img[0]
这里是爆炸出现的地方:
if self.player.estado == self.player.Estado.volando:
anticolision = False
colisiones = pygame.sprite.spritecollide(self.player, self.asteroides, True, pygame.sprite.collide_circle)
for colision in colisiones:
start_ticks = pygame.time.get_ticks()
if colision and not anticolision:
anticolision = True
print("anticolision true")
expl = Explosion(colision.rect.center)
self.explosionSound.play()
self.all_sprites.add(expl)
self.vidas -= 1
segundos = (pygame.time.get_ticks()-start_ticks)/1000
if segundos > 3:
anticolision = False
print("anticolision false")
elif colision and anticolision:
pass
if self.vidas == 0:
self.gameOver()
running= False
【问题讨论】:
-
如何开始爆炸?您是否创建了
Explosion的新实例?如果不是,你必须在爆炸开始时设置self.last_update = pygame.time.get_ticks()。 -
每次发生碰撞时我都会创建一个新的爆炸实例,我会编辑问题给你看