def _create_fleet_of_enemy_bullet(self):
        """Create alien fleet (2nd line). Same method as befor"""

        enemy_bullet = EnemyBullet(self)
        enemy_bullet_width, enemy_bullet_height = enemy_bullet.rect.size

        space_x = enemy_bullet.rect.x / self.settings.alien_adjustment
        available_space_x = self.settings.screen_width - \
            (2 * space_x + enemy_bullet_width)
        number_enemy_bullet_x = int(available_space_x //
                                    (2 * space_x + enemy_bullet_width))

        ship_height = self.ship.rect.height
        space_y = enemy_bullet.rect.y / self.settings.alien_adjustment
        available_space_y = (self.settings.screen_height -
                             (3 * enemy_bullet_height) - ship_height)
        number_rows = int(available_space_y // (space_y + enemy_bullet_height))

        num1 = random.randint(0, number_rows -
                              1)  # Generate random number for power up.
        num2 = random.randint(0, number_enemy_bullet_x -
                              1)  # Generate random number for power up.

        for row_number in range(number_rows):
            for number_enemy_bullet in range(number_enemy_bullet_x):
                self._create_enemy_bullet(number_enemy_bullet, row_number,
                                          num1, num2)
Пример #2
0
def shot_bullet_enemy(enemy_ships, number_bullets):
    global enemy_bullets, enemy_x_position, enemy_y_position
    for i in range(number_bullets):
        try:
            index_ship_to_shoot = randint(0, 9)
            enemy_x_position = enemy_ships[index_ship_to_shoot].xcor()
            enemy_y_position = enemy_ships[index_ship_to_shoot].ycor()
            enemy_bullets.append(EnemyBullet(enemy_x_position, enemy_y_position))
        except IndexError:
            pass
    def _create_enemy_bullet(self, number_enemy_bullet, row_number, num1,
                             num2):
        """Create an enemy bullet and place it in the row."""

        enemy_bullet = EnemyBullet(self)
        enemy_bullet_width, enemy_bullet_height = enemy_bullet.rect.size
        if row_number == num1 and number_enemy_bullet == num2:  # If randomly generated numbers match, create power-up.
            enemy_bullet.bonus = True
            # print(f"Selected power up position (row:column): {num1}:{num2}")  # Check position of power-up.

        space_x = enemy_bullet.rect.x / self.settings.alien_adjustment
        enemy_bullet.x = (
            space_x +
            (1.5 * space_x + enemy_bullet_width) * number_enemy_bullet)
        enemy_bullet.rect.x = enemy_bullet.x

        space_y = enemy_bullet.rect.y / self.settings.alien_adjustment
        enemy_bullet.y = 4 * space_y + \
            (space_y + enemy_bullet_height) * row_number
        enemy_bullet.rect.y = enemy_bullet.y
        self.enemy_bullets.add(enemy_bullet)
Пример #4
0
def handle_enemies(settings, screen, enemies, enemy_bullets):
    if settings.counter % settings.enemy_interval == 0:
        enemies.add(Enemy(settings, screen))
    enemies.update()
    for enemy in enemies.copy():
        if settings.counter % settings.enemy_bullet_interval == 0:
            enemy_bullets.add(EnemyBullet(settings, screen, enemy))
        if enemy.rect.top > enemy.screen_rect.bottom:
            enemies.remove(enemy)
    enemy_bullets.update()
    for enemy_bullet in enemy_bullets.copy():
        if enemy_bullet.rect.top > enemy_bullet.screen_rect.bottom:
            enemy_bullets.remove(enemy_bullet)
Пример #5
0
    def rotate_for_given_angle(self, angle, tank, part, tw_settings, enemy_tank, enemy_bullets):
        angle_for_enemy_tank_movement = 0
        start_movement = 0
        if part == 1:
            if angle >= 0 and angle <= 90:
                angle_for_enemy_tank_movement = angle
                start_movement = 1
            elif angle > 180 and angle <= 270:
                angle_for_enemy_tank_movement = angle - 180
                start_movement = 1
        elif part == 2:
            if angle > 90 and angle <= 180:
                angle_for_enemy_tank_movement = angle
                start_movement = 1
            elif angle > 270 and angle <= 360:
                angle_for_enemy_tank_movement = angle - 180
                start_movement = 1
        elif part == 3:
            if angle > 180 and angle <= 270:
                angle_for_enemy_tank_movement = angle
                start_movement = 1
            elif angle > 0 and angle <= 90:
                angle_for_enemy_tank_movement = angle + 180
                start_movement = 1
        elif part == 4:
            if angle > 270 and angle <= 360:
                angle_for_enemy_tank_movement = angle
                start_movement = 1
            elif angle > 90 and angle <= 180:
                angle_for_enemy_tank_movement = angle + 180
                start_movement = 1

        if start_movement == 1:
            rotate_right = 1
            enemy_tank.moving_forward = False
            ############
            if len(enemy_bullets) < enemy_tank.allowed_enemy_bullets:
                new_bullet = EnemyBullet(tw_settings, self.screen, enemy_tank)
                enemy_bullets.add(new_bullet)
            ############
            while self.angle <= angle_for_enemy_tank_movement:
                rotate_right = 0
                self.angle += 2
                image = pygame.transform.rotozoom(self.image, self.angle, 1)
                self.check_free_angle_for_moving_forward()
                self.check_free_angle_for_moving_backward()
                self.on = 1
                self.recent_image = image
                self.recent_rect =  self.rect
                self.blitme(image, self.rect)

            if rotate_right == 1:
                while self.angle >= angle_for_enemy_tank_movement:
                    rotate_right = 0
                    self.angle -= 2
                    image = pygame.transform.rotozoom(self.image, self.angle, 1)
                    self.check_free_angle_for_moving_forward()
                    self.check_free_angle_for_moving_backward()
                    self.on = 1
                    self.recent_image = image
                    self.recent_rect =  self.rect
                    self.blitme(image, self.rect)

            enemy_tank.moving_forward = True
def fire_enemy_bullets(settings, screen, enemy_ships, enemy_bullets):
    # Firing enemy bullet
    en_bullet = EnemyBullet(settings, screen, enemy_ships)
    enemy_bullets.add(en_bullet)
Пример #7
0
def play():
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((settings.scr_width, settings.scr_height))
    pygame.display.set_caption('Alien Invasion')
    main_clock = pygame.time.Clock()

    stats = GameStats(settings, screen)
    sb = Scoreboard(settings, screen, stats)
    sscreen = StartScreen(settings, screen)

    # Make a ship
    ship = Ship(settings, screen)

    # Make bunkers
    bunkers = [Bunker(screen, 400, 480)]

    # Make bullets and aliens group
    bullets = Group()
    aliens = Group()
    enemy_bullets = Group()
    # gf.create_fleet(settings, screen, ship, aliens)

    # Make buttons
    play_button = Button(screen, 'PLAY', screen.get_rect().centerx, 400)
    score_button = Button(screen, 'HIGH SCORE', screen.get_rect().centerx, 480)
    buttons = [play_button, score_button]

    # Set up music
    bg_music1 = pygame.mixer.Sound('audio/background_1.wav')
    bg_music2 = pygame.mixer.Sound('audio/background_2.wav')
    is_playing_music = False
    is_playing_music2 = False
    game_over_sound = pygame.mixer.Sound('audio/gameover.wav')

    # Boss timer
    boss_respawn_time = randint(10, 18) * 1000  # 10-18s at level 1
    boss_timer = boss_respawn_time
    delta_time = 0

    # Enemy fire timer
    enemy_fire_time = randint(2, 6) * 1000  # 2-6s at level 1
    fire_timer = enemy_fire_time

    # Main game loop
    game_over = False
    while not game_over:
        gf.check_events(settings, screen, stats, sb, buttons, ship, aliens, bullets, enemy_bullets, bunkers)
        if stats.game_status == 2:
            # update bg music
            if not is_playing_music:
                bg_music1.play(-1)
                is_playing_music = True
                is_playing_music2 = False
            if len(aliens) - settings.boss_number <= 10 and not is_playing_music2:
                bg_music1.stop()
                bg_music2.play(-1)
                is_playing_music2 = True
            if is_playing_music2 and len(aliens) - settings.boss_number > 10:
                bg_music2.stop()
                bg_music1.play(-1)
                is_playing_music = True
                is_playing_music2 = False
            if ship.dead and ship.die_anim.finished and stats.ships_left > 0:
                pygame.mixer.stop()
                is_playing_music, is_playing_music2 = False, False
                # reset boss and fire timer when ship explodes
                boss_respawn_time = randint(10, 18) * 1000
                boss_timer = int(boss_respawn_time / settings.enemy_timer_scale)
                enemy_fire_time = randint(2, 6) * 1000
                fire_timer = int(enemy_fire_time / settings.enemy_timer_scale)

            for b in bunkers:
                b.update(bullets, enemy_bullets)
            ship.update()
            gf.update_ship(settings, screen, stats, sb, ship, aliens, bullets, enemy_bullets)
            gf.update_bullets(settings, screen, stats, sb, aliens, bullets, enemy_bullets)

            # Spawn boss
            if settings.boss_number < settings.boss_number_limit:
                if boss_timer <= 0:
                    gf.create_boss(settings, screen, aliens)
                    boss_respawn_time = randint(10, 18) * 1000
                    boss_timer = int(boss_respawn_time / settings.enemy_timer_scale)
                else:
                    boss_timer -= delta_time

            gf.update_aliens(settings, screen, ship, aliens)

            # Enemy fire
            if fire_timer <= 0 and len(enemy_bullets) < settings.bullets_allowed:
                new_bullet = EnemyBullet(settings, screen, aliens)
                enemy_bullets.add(new_bullet)
                enemy_fire_time = randint(2, 6) * 1000
                fire_timer = int(enemy_fire_time / settings.enemy_timer_scale)
            elif fire_timer > 0:
                fire_timer -= delta_time
            gf.update_enemy_bullets(settings, ship, bullets, enemy_bullets)

        else:
            # update music
            if is_playing_music or is_playing_music2:
                pygame.mixer.stop()
                is_playing_music, is_playing_music2 = False, False
                if stats.ships_left == 0:
                    game_over_sound.play()
                    # reset boss and fire timer to level 1
                    boss_respawn_time = randint(10, 18) * 1000
                    boss_timer = boss_respawn_time
                    enemy_fire_time = randint(2, 6) * 1000
                    fire_timer = enemy_fire_time

        gf.update_screen(settings, screen, stats, sb, ship, aliens, bullets, enemy_bullets, bunkers, buttons, sscreen)
        delta_time = main_clock.tick(FPS)