def run_game(): ai_settings = Settings() # 初始化设置参数 screen = gf.init(ai_settings) # 初始化屏幕 stats = GameStats(ai_settings) # 初始化游戏状态信息 sb = ScoreBoard(ai_settings, screen, stats) # 初始化记分牌信息 play_button = Button(ai_settings, screen, "Play") # 初始化按钮 ship = Ship(ai_settings, screen) # 初始化飞船 aliens = Group() # 初始化外星人组 bullets = Group() # 初始化子弹组 gf.create_fleet(ai_settings, screen, aliens, ship) # 创建外星人 while True: ''' 循环检测事件 ''' gf.check_events(ship, ai_settings, screen, aliens, bullets, play_button, stats, sb) ''' 如果游戏开始,则不断更新子弹,飞船,外星人的位置 ''' if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, aliens, bullets, ship, stats, sb) gf.updete_aliens(ai_settings, stats, screen, bullets, aliens, ship, sb) ''' 刷新所有屏幕信息 ''' gf.update_screen(ai_settings, stats, screen, ship, bullets, aliens, play_button, sb)
def run_game(): # Инициализирует игру и создает объект экрана. pygame.init() ai_settings = Settings() stats = GameStats(ai_settings) screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") sb = Scoreboard(ai_settings, screen, stats) play_button = Button(ai_settings, screen, "Play") ship = Ship(ai_settings, screen) # Group for bullet bullets = Group() # Group for alien aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) # alien = Alien(ai_settings, screen) while True: # Отслеживание событий клавиатуры и мыши. gf.check_events(ai_settings, screen, stats, play_button, ship, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)
def run_game(): # 初始化pygame,设置和屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个外星人的编组 aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 创建play按钮 play_button = Button(ai_settings, screen, "Play") # 创建记分牌 sb = Scoreboard(ai_settings, screen, stats) # 开始游戏的主循环 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, stats, screen, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): # Center window that will be created os.environ['SDL_VIDEO_CENTERED'] = '1' # Initialize pygame, settings and screen object. pygame.init() app_settings = Settings() screen = pygame.display.set_mode((app_settings.screen_width, app_settings.screen_height)) pygame.display.set_caption('Alien Invasion') # Make a ship, a group of bullets, and a group of aliens ship = Ship(app_settings, screen) bullets = Group() aliens = Group() # Create the fleet of aliens. gf.create_fleet(app_settings, screen, ship, aliens) # Create an instance to store game statistics and create a scoreboard stats = GameStats(app_settings) sb = Scoreboard(app_settings, screen, stats) # Make buttons play_button = Button(app_settings, screen, "Play") scores_button = Button(app_settings, screen, "Scores") exit_button = Button(app_settings, screen, "Exit") sound_on_button = Button(app_settings, screen, img_path=app_settings.sound_on_button) sound_off_button = Button(app_settings, screen, img_path=app_settings.sound_off_button) # Make an object for managing scores scores = Score(app_settings, stats) # Start the main loop for the game. while True: gf.check_events(app_settings, screen, stats, scores, sb, play_button, scores_button, exit_button, sound_on_button, ship, aliens, bullets) if not stats.pause: if stats.game_active: ship.update() gf.update_bullets(app_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(app_settings, stats, sb, screen, ship, aliens, bullets) gf.update_screen(app_settings, screen, stats, scores, sb, ship, aliens, bullets, play_button, scores_button, exit_button, sound_on_button, sound_off_button)