首页 > 解决方案 > pygame - 试图将爆炸矩形分配给老板

问题描述

我的爆炸课看起来像:

class Explosion(Sprite):


def __init__(self, bosss):

    super(Explosion, self).__init__()
    self.images = []


    for num in range(0,6):
        img = pygame.image.load(f"C:/Users/x/x/animations/ani{num}.png").convert_alpha()
        img = pygame.transform.scale(img, (200, 200))
        self.images.append(img)

    self.index = 0
    self.image = self.images[self.index]
    self.rect = self.image.get_rect()
    self.x = float(self.rect.centerx)
    for boss in bosss.sprites():
        self.rect.center = boss.rect.center

    self.counter = 0

def update(self,explosion, bosss):
    explosion_speed = 4
    self.counter += 1

    if self.counter >= explosion_speed and self.index < len(self.images) - 1:
        self.counter = 0
        self.index += 1
        self.image = self.images[self.index]

    if self.index >= len(self.images) - 1 and self.counter >= explosion_speed:
        self.kill()

和我在屏幕上的功能:

def update_screen(ai_settings, screen, player, bosss, deszcz, aliens, starr, new_bullet, boss_bullet,
                         all_sprites, hpbar, lost, extra_bullet, explosion):

    for bullets in new_bullet.sprites():
        bullets.draw_pocisk()
    player.blitme(hpbar, screen)
    # deszcz.draw(screen)
    aliens.draw(screen)

    if len(aliens) == 0:
        bosss.draw(screen)
        hpbar.blitme(bosss, boss_bullet)
        boss_bullet.draw(screen)
        extra_bullet.draw(screen)
        hpbar.hit(bosss, new_bullet, boss_bullet, ai_settings, extra_bullet)
        player.hit(bosss, boss_bullet, extra_bullet)


        if hpbar.Health <=0:
            hit = pygame.sprite.groupcollide(new_bullet, bosss, True, True)
            explosion.draw(screen)
            if hit:
                AniExplosion(explosion, bosss)


            pygame.mixer.pause()
            boss_bullet.empty()
            extra_bullet.empty()

            if player.health <= 0:
                lost.blitme()
                pygame.mixer.pause()

                boss_bullet.empty()
                extra_bullet.empty()

    pygame.display.flip()

一切正常,但爆炸显示在左上角,而不是老板的结束位置。

我尝试了所有明显的东西,但我认为我误解了一些东西:/

组也没有矩形,但即使我遍历那个精灵它仍然不起作用。

我认为它甚至没有读过它。有什么建议我可以修复它吗?

标签: pythonanimationpygamerect

解决方案


推荐阅读