示例#1
0
class AlienInvasion:
    """Overall class to manage game assets and behavior."""
    def __init__(self):
        """Initialize the game, and create game ressources."""
        pygame.init()
        self.settings = Settings()
        self.clock = pygame.time.Clock()

        # # Switch the comments on these blocks for fullscreen or window screen
        # self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        # self.settings.screen_width = self.screen.get_rect().width
        # self.settings.screen_height = self.screen.get_rect().height

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption('COVID Invasion')

        # Create an instance to store game statistics
        #   and create a scoreboard
        self.stats = GameStats(self)
        self.scoreboard = Scoreboard(self)

        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        self._create_fleet()

        self.play_button = Button(self, 'Play')

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()

            if self.stats.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_aliens()

            self._update_screen()
            self.clock.tick(300)

    def _check_events(self):
        """Response to keypresses ans mouse events"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_play_button(self, mouse_pos):
        """Start a new game when the player hit start"""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self._start_game()

    def _start_game(self):
        """Start the game"""
        # Reset the dynamic settings
        self.settings.init_dynamic()

        # Hide the mouse cursor
        pygame.mouse.set_visible(False)

        # Reset the game stat
        self.stats.reset_stat()
        self.stats.game_active = True
        self.scoreboard.prep_score()
        self.scoreboard.prep_level()
        self.scoreboard.prep_ships()

        # Get rid of all remaining alien and bullets
        self.aliens.empty()
        self.bullets.empty()

        # Create a new fleet and center the ship
        self._create_fleet()
        self.ship.center_ship()

    def _check_keydown_events(self, event):
        """response to key presses."""
        if event.key in (pygame.K_RIGHT, pygame.K_d):
            self.ship.moving_right = True
        elif event.key in (pygame.K_LEFT, pygame.K_a):
            self.ship.moving_left = True
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()
        elif event.key == pygame.K_p:
            self._start_game()
        elif event.key == pygame.K_ESCAPE:
            sys.exit()

    def _check_keyup_events(self, event):
        """response to key releases."""
        if event.key in (pygame.K_RIGHT, pygame.K_d):
            self.ship.moving_right = False
        elif event.key in (pygame.K_LEFT, pygame.K_a):
            self.ship.moving_left = False

    def _fire_bullet(self):
        """Create a new bullet and add it to the bullet group"""
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        """Update position of the bullets and get rid of old ones."""
        # Update bullets position
        self.bullets.update()

        # Get rid of bullets that have disappeared
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

        self._check_bullet_aliens_collision()

    def _check_bullet_aliens_collision(self):
        """Respond to bullet-aliens collisions"""
        # Remove any bullets and aliens that collided
        collisions = pygame.sprite.groupcollide(
            self.bullets,
            self.aliens,
            True,  # dokill1: if True, the bullet is gone if coll
            True  # dokill2: if True, the alien is gone if coll
        )

        if collisions:
            for aliens in collisions.values():
                self.stats.score += self.settings.alien_points * len(aliens)

            self.scoreboard.prep_score()
            self.scoreboard.check_high_score()

        # Create a new fleet if every aliens is down
        if not self.aliens:
            # Destroy existing bullets and create new fleet
            self.bullets.empty()
            self._create_fleet()
            self.settings.increase_stats()

            # Increase the level
            self.stats.level += 1
            self.scoreboard.prep_level()

    def _create_fleet(self):
        """Create the fleet of aliens"""
        # Create an alien and find the numbre of aliens in a row
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        available_space = self.settings.screen_width - (2 * alien_width)
        number_alien_x = available_space // (2 * alien_width)

        # Determine the number of rows of aliens that fit on the screen
        ship_height = self.ship.rect.height
        available_space_y = (self.settings.screen_height - (3 * alien_height) -
                             ship_height)
        number_rows = available_space_y // (2 * alien_height)

        # Creating the full fleet of aliens
        for row_number in range(number_rows):
            for alien_number in range(number_alien_x):
                self._create_alien(alien_number, row_number)

    def _create_alien(self, alien_number, row_number):
        """Create an alien and place it in the row"""
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien.x_pos = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x_pos
        alien.rect.y = alien_height + 2 * alien_height * row_number
        self.aliens.add(alien)

    def _update_aliens(self):
        """
        Check if the fleet is at an edge,
            then update the position of all aliens in the fleet"""
        self._check_fleet_edge()
        self.aliens.update()

        # Look for alien-ship collision
        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            self._ship_hit()

        # Looks for aliens hitting the bottom of the screen
        self._check_aliens_bottom()

    def _check_fleet_edge(self):
        for alien in self.aliens.sprites():
            if alien.check_edge():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        """Drop the entire fleet and change direction"""
        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.alien_drop_speed
        self.settings.fleet_direction *= -1

    def _ship_hit(self):
        """Respond to the ship being hit by an alien"""
        if self.stats.ships_left > 0:
            # Decrement ship_left
            self.stats.ships_left -= 1
            self.scoreboard.prep_ships()

            # Get rid of any remaining bullets and aliens
            self.bullets.empty()
            self.aliens.empty()

            # Create a new fleet and recenter the ship
            self._create_fleet()
            self.ship.center_ship()

            # Little pause for the suspens
            sleep(0.5)
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)

    def _check_aliens_bottom(self):
        """Check if the aliens have reach the bottom of the screen"""
        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >= screen_rect.bottom:
                # Same treatment as the ship hit
                self._ship_hit()
                break

    def _update_screen(self):
        """Update images on the screen, and flip to the new screen."""
        self.screen.fill(self.settings.background_color)
        self.ship.blitme()

        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        self.aliens.draw(self.screen)

        # Draw the score informations
        self.scoreboard.show_score()

        if not self.stats.game_active:
            self.play_button.draw_button()

        pygame.display.flip()
class AlienAttack:
    """Overall class to manage game assests and characterstics of game"""
    def __init__(self):
        """Initialize the game , and create game resources"""
        pygame.init()
        self.setting = Setting()
        self.bullets = pygame.sprite.Group(
        )  # use for collecting similar type of object
        self.screen = pygame.display.set_mode(
            (0, 0), pygame.FULLSCREEN
        )  # 0 width and 0 length expresses the full screen
        self.setting.screen_width = self.screen.get_rect().width
        self.setting.screen_height = self.screen.get_rect().height
        # pygame.display.set_caption("Alien Invasion")
        self.ship = Ship(self)
        self.enemy = pygame.sprite.Group()
        self.stats = GameStats(self)
        self.score_board = ScoreBoard(self)  #create a score board
        self._create_fleet()
        # as we need only one button then we need to call it once
        self.play_button = Button(self, "Play")

    def _create_fleet(self):
        """Create a fleet of alien"""

        new_enemy = Enemy(self)
        enemy_width, enemy_height = new_enemy.rect.size
        available_space_x = self.setting.screen_width - (2 * enemy_width)
        number_enemy_x = available_space_x // (2 * enemy_width)

        # determine the number of rows that will fit
        ship_height = self.ship.ship_rect.height
        available_space_y = self.setting.screen_height - (
            3 * enemy_height) - ship_height
        number_rows = available_space_y // (2 * enemy_height)

        # create a full fleet of aliens
        for row_number in range(number_rows + 1):
            for enemy_number in range(number_enemy_x + 1):
                self._create_alien(enemy_number, row_number)

    def _create_alien(self, enemy_number, row_number):
        new_enemy = Enemy(self)
        enemy_width, enemy_height = new_enemy.rect.size
        new_enemy.x = enemy_width + 2 * enemy_width * enemy_number
        new_enemy.rect.x = new_enemy.x
        new_enemy.rect.y = new_enemy.rect.height + 2 * new_enemy.rect.height * row_number
        self.enemy.add(new_enemy)

    def run_game(self):
        """Start the main loop for game"""
        while True:
            self._check_event()
            if self.stats.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_enemy()
            self._update_screen()

    def _ship_hit(self):
        """Respond to the ship being hit by alien"""
        # decrement ship left
        if self.stats.ships_left > 0:
            self.stats.ships_left -= 1
            self.score_board.prep_ships()

            # get rid of any remaining aliens and bulllets
            self.enemy.empty()
            self.bullets.empty()

            # Create a new fleet
            self._create_fleet()
            self.ship.center_ship()

            # pause
            sleep(0.5)
        else:
            self.stats.game_active = False
            self.stats.reset_stat()
            self.ship.center_ship()
            pygame.mouse.set_visible(True)
            self.stats.level = 0

    def _update_enemy(self):
        """Update the position of enemy ships"""
        self._check_fleet_edges()
        self.enemy.update()
        # Look for alien-ship collision
        if pygame.sprite.spritecollideany(
                self.ship,
                self.enemy):  # use for collision bw images or rectangles
            self._ship_hit()
        self._check_enemy_bottom()

    def _check_fleet_edges(self):
        """Respond appropriatly if any aliens have reached an edge"""
        for enemy in self.enemy.sprites():
            if enemy.check_edge():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        """Drop the entire fleet and change the fleet's direction"""
        for enemy in self.enemy.sprites():
            enemy.rect.y += self.setting.fleet_drop_speed
        self.setting.fleet_direction *= -1

    def _update_bullets(self):
        self.bullets.update()

        # get rid of the bullets that have disapperead
        for bullet in self.bullets.copy():
            if bullet.rect.y <= 0:
                self.bullets.remove(bullet)
        self._check_bullet_enemy_collision()

    def _check_bullet_enemy_collision(self):
        """Respond to bullet - alien collision"""
        # check for any bullet that have hit enemy
        # if so , get rid of the bullet and the enemy
        collisions = pygame.sprite.groupcollide(self.bullets, self.enemy, True,
                                                True)
        if not self.enemy:
            self.bullets.empty()
            self._create_fleet()
            self.setting.increase_speed()

            #increase level
            self.stats.level += 1
            self.score_board.prep_level()
        if collisions:
            for enemy in collisions.values():
                self.stats.score += self.setting.enemy_point * len(enemy)
            self.score_board.prep_score()
            self.score_board.check_high_score()

    def _check_event(self):
        """Response to keypress and mouse event"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_event(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_play_button(self, mouse_pos):
        """Start a new game when the player clicked clicks play"""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self.stats.reset_stat()
            self.score_board.prep_score()
            self.setting.initialize_dynamic_setting()
            self.stats.game_active = True
            self.score_board.prep_level()
            pygame.mouse.set_visible(False)
            self.score_board.prep_ships()
            # Get rid of any remaining aliens and bullets
            self.enemy.empty()
            self.bullets.empty()

            # create a new fleet and center the ship
            self._create_fleet()
            self.ship.center_ship()

    def _check_keydown_events(self, event):
        """Response to keyup events"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_UP:
            self.ship.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()

    def _fire_bullet(self):
        if len(self.bullets) < self.setting.bullet_alloewed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _check_keyup_event(self, event):
        """Response to keyup events"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
        elif event.key == pygame.K_UP:
            self.ship.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = False

    def _update_screen(self):
        """update images on the screen, and flip to the new screen"""
        self.screen.fill(self.setting.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.enemy.draw(self.screen)
        self.score_board.show_score()
        # draw the play button if the game is inactive
        if not self.stats.game_active:
            self.play_button.draw_button()
        """Make the latest change visible on screen"""
        pygame.display.flip()

    def _check_enemy_bottom(self):
        """check if any aliens have reached the bottom of screen"""
        screen_rect = self.screen.get_rect()
        for enemy in self.enemy.sprites():
            if enemy.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break