【问题标题】:Problem adding explosions to space invaders向太空入侵者添加爆炸的问题
【发布时间】:2022-01-07 09:37:54
【问题描述】:

您好,我是 python 新手,我的第一个项目决定做一个太空入侵者游戏, 所以我的问题是,对于每一列外星人,我击落的爆炸都出现在同一个特定位置。

例如,最左列的爆炸出现在屏幕的左上角,当您移动到右侧的列时,爆炸会下降,以至于最右列的爆炸动画出现在屏幕右下角。

我将包含我制作的 Explosions 类的代码,以及我认为我在其中编写了导致此问题的代码的函数文件部分。

爆炸类:

import pygame


pygame.init()

clock = pygame.time.Clock()
fps = 60


class Explosion(pygame.sprite.Sprite):
    def __init__(self, x, y, size):
        pygame.sprite.Sprite.__init__(self)
        self.images = []
        for num in range(1, 6):
            img = pygame.image.load(f"images/exp{num}.png")
            if size == 1:
                img = pygame.transform.scale(img, (20, 20))
            if size == 2:
                img = pygame.transform.scale(img, (40, 40))
            if size == 3:
                img = pygame.transform.scale(img, (160, 160))
            # add the image to the list
            self.images.append(img)
        self.index = 0
        self.image = self.images[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        self.counter = 0

    def update(self):
        explosion_speed = 4
        # Update explosion animation.
        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 the animation is complete, delete explosion.
        if self.index >= len(self.images) - 1 and self.counter >= explosion_speed:
            self.kill()

可能有问题的部分:

def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens,
                                  bullets, ai_sounds, explosions):
    """Respond to bullet_alien collisions."""
    # Remove the bullets that have collided.
    collisions = pygame.sprite.groupcollide(aliens, bullets, True, True)
     for alienn in collisions:
        explosion = Explosion(alienn.rect.centerx, alienn.rect.centerx, 2)
        explosions.add(explosion)

    if collisions:
        for alienss in collisions.values():
            stats.score += ai_settings.alien_points * len(alienss)
            ai_sounds.play_alien_explosion_sound()
        sb.prep_score()
        check_high_score(stats, sb)
    if len(aliens) == 0:
        start_new_level(ai_settings, screen, stats, sb, ship, aliens, bullets)

【问题讨论】:

  • 这是一个错字。您将alienn.rect.centerx 传递给Explosion 两次。它需要是Explosion(alienn.rect.centerx, alienn.rect.centery, 2)Explosion(*alienn.rect.center, 2) 而不是Explosion(alienn.rect.centerx, alienn.rect.centerx, 2)
  • @Rabbid76 - 谢谢你真的让我开心,真的很感激。

标签: python animation pygame


【解决方案1】:

您的申请中有错字。您将alienn.rect.centerx 传递给Explosion 两次。它需要是Explosion(alienn.rect.centerx, alienn.rect.centery, 2)Explosion(*alienn.rect.center, 2) 而不是Explosion(alienn.rect.centerx, alienn.rect.centerx, 2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2018-12-10
    • 2020-07-23
    相关资源
    最近更新 更多