def run_game(): pygame.init() ai_setting = Setting() screen = pygame.display.set_mode( (ai_setting.screen_width, ai_setting.screen_height)) pygame.display.set_caption('Alien Hunter') play_button = Button(ai_setting, screen, "Play") stats = GameStats(ai_setting) sb = Scoreboard(ai_setting, screen, stats) ship = Ship(ai_setting, screen) bullets = Group() aliens = Group() agf.create_fleet(ai_setting, screen, ship, aliens) while True: agf.check_events(ai_setting, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() agf.update_bullet(ai_setting, screen, stats, sb, ship, aliens, bullets) agf.update_aliens(ai_setting, stats, sb, screen, ship, aliens, bullets) agf.update_screen(ai_setting, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) #创建记分牌 sb = Scoreboard(ai_settings, screen, stats) #创建飞船 子弹群 外星人群 ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def __init__(self): pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_heigth)) self.ship = Ship(self) self.stats = GameStats(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() pygame.display.set_caption("Alien Invasion") self.play_button = Button(self, "play")
def prep_ships(self): self.ships = Group() for ship_number in range(self.stats.ships_left): ship = Ship(self.ai_setting, self.screen) ship.rect.x = 10 + ship_number * ship.rect.width ship.rect.y = 10 self.ships.add(ship)
def run_game(): # Initialize pygame, settings, and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make the Play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics, and a scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Set the background color. bg_color = (230, 230, 230) # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
class AlienInvasion: def __init__(self): pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_heigth)) self.ship = Ship(self) self.stats = GameStats(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() pygame.display.set_caption("Alien Invasion") self.play_button = Button(self, "play") def _check_keydown_events(self, event): if event.key == pygame.K_LEFT: self.ship.moving_left = True elif event.key == pygame.K_RIGHT: self.ship.moving_right = True elif event.key == pygame.K_SPACE: self._fire_bullet() elif event.key == pygame.K_q: sys.exit() def _check_keyup_events(self, event): if event.key == pygame.K_LEFT: self.ship.moving_left = False elif event.key == pygame.K_RIGHT: self.ship.moving_right = False def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.eixt() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.KEYUP: self._check_keyup_events(event) def _create_alien(self, alien_number, row_number): alien = Alien(self) alien_width = alien.rect.width alien.x = alien_width + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number self.aliens.add(alien) def _create_fleet(self): alien = Alien(self) alien_width, alien_height = alien.rect.size avaliable_space_x = self.settings.screen_width - (2 * alien_width) number_aliens_x = avaliable_space_x // (2 * alien_width) ship_height = self.ship.rect.height available_space_y = (self.settings.screen_heigth - (3 * alien_height) - ship_height) number_row = available_space_y // (2 * alien_height) for row_number in range(number_row): for alien_number in range(number_aliens_x): self._create_alien(alien_number, row_number) def _check_fleet_edges(self): for alien in self.aliens.sprites(): if alien.check_edges(): self._change_fleet_direction() break def _change_fleet_direction(self): for alien in self.aliens.sprites(): alien.rect.y += self.settings.fleet_drop_speed self.settings.fleet_direction *= -1 def _fire_bullet(self): if len(self.bullets) < self.settings.bullet_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) def _update_bullets(self): self.bullets.update() for bullet in self.bullets.copy(): if bullet.rect.bottom <= 0: self.bullets.remove(bullet) self._check_bullet_alien_collisions() def _check_bullet_alien_collisions(self): collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True) if not self.aliens: self.bullets.empty() self._create_fleet() def _update_aliens(self): self._check_fleet_edges() self.aliens.update() if pygame.sprite.spritecollideany(self.ship, self.aliens): self._ship_hit() self._check_aliens_bottom() def _ship_hit(self): if self.stats.ships_left > 0: self.stats.ships_left -= 1 self.aliens.empty() self.bullets.empty() self._create_fleet() self.ship.center_ship() sleep(0.5) else: self.stats.game_active = False def _check_aliens_bottom(self): screen_rect = self.screen.get_rect() for alien in self.aliens.sprites(): if alien.rect.bottom >= screen_rect.bottom: self._ship_hit() break def _update_screen(self): self.screen.fill(self.settings.background_color) self.ship.blitme() for bullet in self.bullets.sprites(): bullet.draw_bullet() self.aliens.draw(self.screen) self.aliens.draw(self.screen) if not self.stats.game_active: self.play_button.draw_button() pygame.display.flip() def run_game(self): self._create_fleet() while True: self._check_events() self.ship.update() self._update_bullets() self._update_aliens() self._update_screen()