Ejemplo n.º 1
0
    def update(self, d_time, players: pygame.sprite.Group, missile_wrapper: MissileWrapper):
        for e in self.sprites():
            if e.life > 0:
                missile_wrapper.enemy_hit(e)
        for e in self.sprites():
            if e.life > 0:
                missile_wrapper.enemy_aoe_hit(e)
                missile_wrapper.add_from_enemy(e.shoot())
            if e.state is Enemy.State.DEAD:
                self.remove(e)

        if len(players.sprites()) > 1:
            for e in self.sprites():
                dps = [Vector(p.rect.center[0] - e.rect.center[0], p.rect.center[1] - e.rect.center[1]).magnitude()
                       for p in players.sprites()]
                min_dp = min(dps)
                for i in range(len(dps)):
                    if min_dp == dps[i]:
                        e.update(d_time, players.sprites()[i].rect.center)
        elif len(players.sprites()) == 1:
            super().update(d_time, players.sprites()[0].rect.center)

        if len(self.sprites()) < self.max_enemies:
            self.timer += d_time
            if self.timer > self.new_enemy_spawn_interval:
                self.add([self.enemy_prefab((random.randrange(0, self.screen.get_width(), 1), -30))
                          for i in range(self.new_enemies_number)])
                self.timer = 0
Ejemplo n.º 2
0
def collision_pairs(grpSprites1: pygame.sprite.Group,
                    grpSprites2: pygame.sprite.Group,
                    fncCollided=pygame.sprite.collide_rect) -> List[Tuple[Sprite, Sprite]]:
    lstPairs = []
    lstSpr1 = grpSprites1.sprites()
    lstSpr2 = grpSprites2.sprites()
    for i in range(len(lstSpr1)):
        for j in range(len(lstSpr2)):
            if fncCollided(lstSpr1[i], lstSpr2[j]):
                lstPairs.append((lstSpr1[i], lstSpr2[j]))
    return lstPairs
Ejemplo n.º 3
0
def check_fleet_edges(ai_settings: Settings,
                      aliens: pygame.sprite.Group) -> NoReturn:
    """有外星人到达边缘是采取相应的措施"""
    for alien in aliens.sprites():
        if alien.check_edges():
            change_fleet_direction(ai_settings, aliens)
            break
Ejemplo n.º 4
0
def collision_pairs_self(grpSprites: pygame.sprite.Group,
                         fncCollided=pygame.sprite.collide_rect) -> List[Tuple[Sprite, Sprite]]:
    lstPairs = []
    lstSpr = grpSprites.sprites()
    for i in range(len(lstSpr)):
        for j in range(i + 1, len(lstSpr)):
            if fncCollided(lstSpr[i], lstSpr[j]):
                lstPairs.append((lstSpr[i], lstSpr[j]))
    return lstPairs
Ejemplo n.º 5
0
def check_aliens_bottom(ai_settings: Settings, stats: GameStats,
                        screen: pygame.Surface, ship: Ship,
                        aliens: pygame.sprite.Group,
                        bullets: pygame.sprite.Group,
                        sb: ScoreBoard) -> NoReturn:
    """检查是否有外星人到达了屏幕底端"""
    screen_rect = screen.get_rect()
    for alien in aliens.sprites():
        if alien.rect.bottom >= screen_rect.bottom:
            # 像飞船被撞到一样进行处理
            ship_hit(ai_settings, stats, screen, sb, ship, aliens, bullets)
Ejemplo n.º 6
0
def move_player_with_ai(pawnsSprintTable: pygame.sprite.Group,
                        chessTilesSprintTable: pygame.sprite.Group, action):
    pawnX = chessTilesSprintTable.sprites()[
        action[0]].get_tile_center_x_for_drawing_pawn(
            pawnsSprintTable.sprites()[0])
    pawnY = chessTilesSprintTable.sprites()[
        action[0]].get_tile_center_y_for_drawing_pawn(
            pawnsSprintTable.sprites()[0])

    pawn_X_to_move = chessTilesSprintTable.sprites()[
        action[1]].get_tile_center_x_for_drawing_pawn(
            pawnsSprintTable.sprites()[0])
    pawn_Y_to_move = chessTilesSprintTable.sprites()[
        action[1]].get_tile_center_y_for_drawing_pawn(
            pawnsSprintTable.sprites()[0])

    pawn_to_move = [
        pawn for pawn in pawnsSprintTable
        if (pawn.rect.x == pawnX) and (pawn.rect.y == pawnY)
    ]

    if len(pawn_to_move) != 0:
        pawn_to_move[0].set_pawn_position(pawn_X_to_move, pawn_Y_to_move)
Ejemplo n.º 7
0
def update_screen(ai_settings: Settings, screen: pygame.Surface,
                  stats: GameStats, sb: ScoreBoard, ship: Ship,
                  aliens: pygame.sprite.Group, bullets: pygame.sprite.Group,
                  play_button: Button) -> NoReturn:
    """更新屏幕上的图像,并切换到新屏幕"""

    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)

    # 在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    aliens.draw(screen)

    # 显示得分
    sb.show_score()

    # 如果游戏处于非活动状态,就绘制Play按钮
    if not stats.game_active:
        play_button.draw_button()

    # 让最近绘制的屏幕可见
    pygame.display.flip()
Ejemplo n.º 8
0
def change_fleet_direction(ai_settings: Settings,
                           aliens: pygame.sprite.Group) -> NoReturn:
    """将整群外星人下移, 并改变他们的方向"""
    for alien in aliens.sprites():
        alien.rect.y += ai_settings.fleet_drop_speed
    ai_settings.fleet_direction *= -1
Ejemplo n.º 9
0
def collisions_of_enemies(enemies: pygame.sprite.Group):
    for i in range(len(enemies.sprites())):
        for j in range(i + 1, len(enemies.sprites())):
            if is_collision(enemies.sprites()[i], enemies.sprites()[j]):
                yield enemies.sprites()[i], enemies.sprites()[j]