Pygame - 敌人射击,群体问题

Pygame - Enemy shoot, problem with groups

刚刚遇到 Space Invaders 游戏的问题。只需进行一些升级,并希望我的老板从中心而不是从角落向玩家集中射击。 游戏现在看起来:Game - 大多数时候,我收到一个错误:

self.rect.left = bosss.rect.right
AttributeError: 'Group' object has no attribute 'rect'

我来自 Bullet 和 Boss 的代码:

class PociskBoss(Sprite):

def __init__(self, ai_settings, screen, bosss):

    super(PociskBoss, self).__init__()
    self.ai_settings = ai_settings
    self.screen = screen
    
    self.image = pygame.image.load('C:/Users/Kamil/Pictures/bossbullet.png').convert_alpha()
    self.image = pygame.Surface([12, 4])

    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()
    #self.rect.center = self.screen_rect.midright #this work ofc (screen)
    self.rect.left = bosss.rect.right #GROUP RECT(?)
    
    self.rect.y = random.randrange(0, 50)
    

    rects = pygame.sprite.spritecollide(self, bosss, False)
    for platform in rects:
  
       self.rect.add_internal(platform) #like that?
       self.rect.right = platform.rect.left 

    self.speed_factor = ai_settings.boss_bullet_speed_factor
    self.x = float(self.rect.x)


def update(self, bosss):
    self.x = self.speed_factor
    self.rect.x += self.x
    #self.rect.y += self.y

def draw_pocisk(self):
    self.screen.blit(self.image, self.rect)

class boss(Sprite):

def __init__(self, ai_settings, screen):
    super(boss, self).__init__()
    self.screen = screen
    self.ai_settings = ai_settings



    self.image = pygame.image.load('C:/Users/Kamil/Pictures/boss.png').convert_alpha()
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()
    self.rect.right = self.screen_rect.right
    #self.center = float(self.rect.centery)
    self.y = self.rect.y
    self.x = self.rect.x
    self.y = float(self.rect.y)
    #self.center = float(self.rect.centery)

def update(self):
  
    self.y -= (self.ai_settings.boss_speed_factor *
                    self.ai_settings.fleet_direction)
    self.rect.y = self.y


def check_edges(self):
    screen_rect = self.screen.get_rect()
    if self.rect.top <= screen_rect.top:
        return True
    elif self.rect.bottom >= screen_rect.bottom:
        return True

def blitme(self):
     self.screen.blit(self.image, self.rect)

Main loop of game:


def game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.SCREEN_WIDTH, ai_settings.SCREEN_HEIGHT))
    clock = pygame.time.Clock()
    pygame.display.set_caption("Shootin")
    koloo = Character(ai_settings, screen)
    # Utworzenie grupy przeznaczonej do przechowywania pocisków.
    new_bullet = Group()
    boss_bullet = Group()
    all_sprites = Group()
    aliens = Group()
    starr = Group()
    deszcz = Group()
    bosss = Group()
    enemyPlayer = Group()
    gf.create_fleet(ai_settings, screen, koloo, aliens)
    #gf.create_fleet_starss(ai_settings, screen, koloo, starr)
    gf.create_fleet_drop(ai_settings, screen, koloo, deszcz)
    gf.create_boss(ai_settings, screen, bosss)
    gf.create_enemy(ai_settings, screen, enemyPlayer)
    clock=pygame.time.Clock()
    FPS = 60
    #GŁÓWNA PĘTLA GRY
    while True:
        gf.check_events(ai_settings, screen, koloo, new_bullet, aliens, boss_bullet, bosss,all_sprites)
        koloo.update()
        new_bullet.update()
        gf.update_bullets(ai_settings, screen, koloo, new_bullet, aliens, bosss, boss_bullet, enemyPlayer)
        boss_bullet.update(bosss)
        gf.boss_bullet_update(ai_settings, screen, bosss, boss_bullet)
        gf.update_rain(ai_settings, deszcz)
        gf.update_aliens(ai_settings, aliens)
        gf.update_boss(ai_settings, bosss)
        gf.update_screen(ai_settings, screen, koloo, bosss, deszcz, aliens, starr, new_bullet, boss_bullet,
                         enemyPlayer, all_sprites, sprites_list, bullet_list)

        clock.tick(FPS)

您可以在 pygame.sprite.Group with the method sprites() 中获得 Sprties 的列表。列表中的精灵可以通过订阅访问。
如果 bosss 恰好包含 1 个 Sprite,则可以通过以下方式获取 Spriterect 属性:

rect = bosss.sprites()[0].rect