def __init__(self): """初始化游戏并创建游戏资源""" pygame.init() # 导入游戏设置 self.settings = Settings() # 设置游戏名称 pygame.display.set_caption(self.settings.name) # 创建用于存储游戏信息实例 self.stats = GameStates(self) # 设置窗口屏幕大小 self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) # 背景颜色调整 self.bg_color = self.settings.screen_color # 导入飞船 self.ship = Ship(self) # 子弹存储 self.bullets = pygame.sprite.Group() # 外星人存储 self.aliens = pygame.sprite.Group() self._create_fleet() # 设置计分 self.sb = Scoreboard(self) # 创建play按钮 self.play_button = Button(self, "play")
def load_game(cls): if not os.path.isfile("save_game.json"): raise FileNotFoundError with open("save_game.json") as save_file: json_data = json.load(save_file) entities = [ Entity.from_json(json_data=entity_json_data) for entity_json_data in json_data["entities"] ] player = entities[json_data["player_index"]] game_map = GameMap.from_json(json_data=json_data["game_map"]) message_log = MessageLog.from_json(json_data=json_data["message_log"]) game_state = GameStates(json_data["game_state"]) game = cls() game.map_generator: Optional[MapGenerator] = MapGenerator( map_width=CONSTANTS.map_width, map_height=CONSTANTS.map_height) game.entities = entities game.player = player game.map_generator.game_map = game_map game.message_log = message_log game.current_state = game_state game.camera: Camera = Camera.from_json(json_data=json_data, player=player) return game
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") states = GameStates(ai_settings) button = Button(ai_settings, screen, "Play") ship = Ship(ai_settings, screen) alien = Alien(ai_settings, screen) #创建子弹存储数组 bullets = Group() #创建外星人编组 aliens = Group() gf.create_fleet(ai_settings, screen, aliens, ship) #开始游戏的主循环 while True: gf.check_events(ai_settings, screen, states, button, ship, bullets) if states.game_active: ship.update() gf.update_aliens(ai_settings, states, screen, ship, aliens, bullets) gf.update_bullets(ai_settings, screen, ship, bullets, aliens) gf.update_screen(ai_settings, screen, ship, aliens, bullets, button, states)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) pygame.display.set_caption("Alien Invasion") # 创建一个用于存储游戏统计信息实例 stats = GameStates(ai_settings) # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") while True: gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets) if stats.game_active: gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_alien(ai_settings, stats, screen, ship, aliens, bullets) gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, 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("史大炮大战外星人") play_button = Button(ai_settings, screen, "Play") states = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, states) ship = Ship(ai_settings, screen) bullets = Group() alien = Alien(ai_settings, screen) aliens = Group() gf.creat_fleet(ai_settings, screen, ship, aliens) while 1: gf.check_events(ai_settings, screen, states, sb, play_button, ship, aliens, bullets) if states.game_active: ship.update() gf.update_bullets(ai_settings, screen, states, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, states, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, states, sb, ship, aliens, bullets, play_button)
def run_game(): pygame.init() FPS = 30 # frames per second setting fpsClock = pygame.time.Clock() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_heigth) ) pygame.display.set_caption("Alien Invasion") play_button = Button(ai_settings, screen,"PLAY") ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) stats = GameStates(ai_settings) 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) pygame.display.update() fpsClock.tick(FPS)
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') #创建button按钮 play_button = Button(ai_settings, screen, 'Play') #创建一个用于统计游戏信息的实例,并创建计分板 stats = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, stats) #创建一艘飞船,一个子弹编组和一个外星人编组 ship = Ship(ai_settings, screen) alien = Alien(ai_settings, screen) #创建一个用于存储子弹的编组,如果在循环内部创建这样的编组,游戏运行时将创建数千个子弹编组,导致游戏慢得像 # 蜗牛。如果游戏停滞不前,请仔细查看主while循环中发生的情况。 bullets = Group() aliens = Group() #创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) #开始游戏的主循环 while True: #响应按键和鼠标事件 # gf.check_events(ship) gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: #更新移动飞船位置 ship.update() #这若没有这行代码,子弹不会显示.对编组Group调用update()方法, # 编程将会自动对组内的每一个sprite调用update()方法。 #详细请看https://www.jianshu.com/p/126ef3e4e36e,bullet的方法名称和sprite的方法update必须一致,不然不起作用 #被放到了update_bullets(bullets)中,可以让主程序alien_invasion更加简洁 # bullets.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,ship) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): """运行游戏方法""" #initial the game and create a screen object pygame.init() #创建 Setting 实例 ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") #创建开始按钮 play_button = Button(ai_settings, screen, "PLAY!") #创建一个用于存储游戏统计信息的实例 stats = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, stats) #创建一个飞船 ship = Ship(ai_settings, screen) #创建一个存储子弹的编组 Group bullets = Group() #创建一个外星人 aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) #读取历史最高分 gf.read_high_score(stats) sb.prep_high_score() #The main loop while True: #检查玩家输入 gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) #游戏主体需要启动方可运行 if stats.game_active: #更新飞船 ship.update() #更新子弹 gf.update_bullet(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets) #重绘屏幕 gf.update_screen(ai_settings, 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_button = Button(ai_settings, screen, "Play") # 统计游戏信息的实例 stats = GameStates(ai_settings) # 建立一艘飞船 ship = Ship(ai_settings, screen) # 创建子弹 bullets = Group() # 建立外星人 aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) # 创建记分牌 sb = Scoreboard(ai_settings, screen, stats) # 开始游戏循环 while 1: # 监听键盘和鼠标 见引用库 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 load_game(): if not os.path.isfile('save_game.json'): raise FileNotFoundError with open('save_game.json') as save_file: json_data = json.load(save_file) entities = [Entity.from_json(json_data=entity_json_data) for entity_json_data in json_data['entities']] player = entities[json_data['player_index']] game_map = GameMap.from_json(json_data['game_map']) message_log = MessageLog.from_json(json_data['message_log']) game_state = GameStates(json_data['game_state']) return player, entities, game_map, message_log, game_state
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() # 设置背景色 ai_settings = Settings() # 返回一个surface对象,surface对象是屏幕的一部分,属于显示元素 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption(ai_settings.caption) # 实例化存储游戏统计信息的实例 stats = GameStates(ai_settings) # 创建按钮 play_button = Button(ai_settings, screen, "Play") # 创建记分牌 sb = Scoreboard(ai_settings, screen, stats) # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个外星人 # alien = Alien(ai_settings, screen) # 创建一个用于存储外星人的编组 aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 开始游戏循环 while True: # 监视键盘和鼠标事件 # for event in pygame.event.get(): # 访问Pygame检测到的事件 # if event.type == pygame.QUIT: # sys.exit() gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) # 检测Pygame的事件 # screen.fill(ai_settings.bg_color) # 背景填充 # ship.blitme() # 在屏幕中显示飞船 # # 让最近的屏幕可见 # pygame.display.flip() # 让最近绘制的屏幕可见 if stats.game_active: ship.update() # 更新飞船的位置 gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def load_game(): with open('save_game.json') as save_file: data = json.load(save_file) player_index = data['player_index'] entities_json = data['entities'] game_map_json = data['game_map'] message_log_json = data['message_log'] game_state_json = data['game_state'] entities = [Entity.from_json(entity_json) for entity_json in entities_json] player = entities[player_index] game_map = GameMap.from_json(game_map_json) message_log = MessageLog.from_json(message_log_json) game_state = GameStates(game_state_json) return player, entities, game_map, message_log, game_state
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 = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 开始游戏主循环 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 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(ai_settings, screen, "Play") # 创建一个用于存储游戏统计信息的实例 states = GameStates(ai_settings) # 创建记分牌 scoreboard = Scoreboard(ai_settings, screen, states) # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一用于存储外星人的编组 # alien = Alien(ai_settings, screen) # 创建外星人群 aliens = Group() # 创建一个用于存储子弹的编组 bullets = Group() gf.create_fleet(ai_settings, screen, ship, aliens) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 gf.check_event(ai_settings, screen, states, play, scoreboard, ship, aliens, bullets) if states.game_active: # 更新界面 ship.update() # 更新子弹 gf.update_bullets(ai_settings, screen, states, scoreboard, bullets, ship, aliens) # 更新外星人 gf.update_aliens(ai_settings, states, screen, scoreboard, ship, aliens, bullets) gf.update_screen(ai_settings, screen, states, scoreboard, ship, aliens, bullets, play)
def load_game(): with ZipFile(save_filename, 'r') as savezip: json_bytes = savezip.read('savegame.json') json_data = json_bytes.decode('utf-8') data = json.loads(json_data) player_index = data['player_index'] entities_json = data['entities'] game_map_json = data['game_map'] message_log_json = data['message_log'] game_state_json = data['game_state'] entities = [Entity.from_json(entity_json) for entity_json in entities_json] player = entities[player_index] game_map = GameMap.from_json(game_map_json) message_log = MessageLog.from_json(message_log_json) game_state = GameStates(game_state_json) return player, entities, game_map, message_log, game_state
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") #创建一艘飞船 ship = Ship(ai_settings, screen) #创建一个用于存储所有子弹的编组 bullets = Group() # 创建一个用于存储所有外星人的编组 aliens = Group() # 创建一个游戏时统计信息对象 states = GameStates(ai_settings) # 创建一堆外星人 gf.create_fleet(ai_settings, screen, aliens, ship) #创建一个分数板对象 score = ScoreBoard(ai_settings, screen, states) while True: #事件监听 gf.check_events(ai_settings, screen, ship, states, aliens, bullets, play_button, score) if states.game_active: ship.update() gf.update_bullets(ai_settings, screen, bullets, aliens, ship, states, score) gf.update_aliens(ai_settings, states, screen, aliens, ship, bullets, score) #屏幕更新 gf.update_screen(ai_settings, screen, ship, bullets, aliens, states, play_button, score)
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 = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, stats) #创建一艘飞船 ship = Ship(ai_settings, screen) #创建一个用于存储子弹的编组 bullets = Group() #创建一个外星人编组 aliens = Group() #创建外星人群 gf.create_fleet(ai_settings, screen, aliens, ship) # 开始游戏的主循环 while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, bullets, aliens) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, bullets, aliens) gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, bullets, aliens, play_button)
def run_game(): pygame.init() ai_setting = Setting() screen = pygame.display.set_mode((ai_setting.width, ai_setting.height)) pygame.display.set_caption('Alien Invasion') # Creat a ship Bullets group and aliens fleet ship = Ship(screen, ai_setting) bullets = Group() aliens = Group() # Creat game state states = GameStates(ai_setting) # Creat a button instance play_button = Button(ai_setting, screen, 'Play') # Creat score board sb = ScoreBoard(ai_setting, screen, states) # Creat aliens fleet gf.creat_fleet(aliens, ship, screen, ai_setting) while True: gf.check_event(ship, bullets, aliens, screen, ai_setting, states, play_button, sb) if states.game_active: ship.update(ai_setting.width, ai_setting.height) gf.update_bullet(ai_setting, screen, states, sb, ship, bullets, aliens) gf.update_aliens(screen, ai_setting, states, sb, aliens, ship, bullets) gf.update_screen(screen, ai_setting, states, sb, ship, bullets, aliens, play_button) pygame.display.flip()
class AlienInvasion: """管理游戏资源和行为的类""" def __init__(self): """初始化游戏并创建游戏资源""" pygame.init() # 导入游戏设置 self.settings = Settings() # 设置游戏名称 pygame.display.set_caption(self.settings.name) # 创建用于存储游戏信息实例 self.stats = GameStates(self) # 设置窗口屏幕大小 self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) # 背景颜色调整 self.bg_color = self.settings.screen_color # 导入飞船 self.ship = Ship(self) # 子弹存储 self.bullets = pygame.sprite.Group() # 外星人存储 self.aliens = pygame.sprite.Group() self._create_fleet() # 设置计分 self.sb = Scoreboard(self) # 创建play按钮 self.play_button = Button(self, "play") def run_game(self): """开始游戏的主循环""" while True: self._check_events() if self.stats.game_active: self.ship.update_position() self._update_bullets() self._update_aliens() self._update_screen() def _check_events(self): """监视键盘和鼠标事件""" 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_events(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): """玩家单机play时运行游戏""" button_clicked = self.play_button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: # 重置游戏设置 self.settings.initialize_dynamic_settings() # 重置游戏信息 self.stats.reset_stats() self.stats.game_active = True # 清空子弹丶外星人 self.aliens.empty() self.bullets.empty() # 创建一群新的外星人并让飞船居中 self._create_fleet() self.ship.center_ship() pygame.mouse.set_visible(False) def _check_keyup_events(self, event): """松开按键""" 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 _check_keydown_events(self, event): """按下按键""" 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_SPACE: self._create_bullet() def _create_bullet(self): """创建一颗子弹,并将其加入编组bullets中""" if len(self.bullets) < self.settings.bullet_maxNum: 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() self.settings.increase_speed() def _draw_bullets(self): # 在屏幕上绘制打出的子弹 for bullet in self.bullets.sprites(): bullet.draw_bullet() def _create_fleet(self): """创建外星人舰队""" # 创建一个外星人,并且计算一行可以容纳多少外星人 # 外星人间距为外星人宽度 alien = Alien(self) alien_width, alien_height = alien.rect.size available_space_x = self.settings.screen_width - (2 * alien_width) number_aliens_x = available_space_x // (2 * alien_width) # 计算屏幕能容纳多少行外星人 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) # 创建外星人群 for row_number in range(number_rows): # 创建一行外星人 for alien_number in range(number_aliens_x): self._create_alien(alien_number, row_number) def _create_alien(self, alien_number, row_number): """创建一个外星人把它加入当前行的指定位置""" alien = Alien(self) alien_width, alien_height = alien.rect.size alien.set_alien_rect_X(alien_width + 2 * alien_width * alien_number) alien.set_alien_rect_Y(alien.rect.height + 2 * alien.rect.height * row_number) self.aliens.add(alien) 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 _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 _update_screen(self): """更新屏幕图像,并切换到新屏幕""" self.screen.fill(self.bg_color) self.ship.blitMe() self._draw_bullets() self.aliens.draw(self.screen) # 显示得分 self.sb.show_score() # 若游戏处于非活动状态,绘制play按钮 if not self.stats.game_active: self.play_button.draw_button() # 让最近绘制的屏幕可见 pygame.display.flip() def _ship_hit(self): """响应飞船与外星人碰撞""" # 飞船数量-1 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 pygame.mouse.set_visible(True) 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 read_from(sock, rooms, doors, read_ship_state=False): addresses = [ (0x078D, 0x10), # 0x78D to 0x79C (0x0998, 0x02), # 0x998 to 0x999 (0x09A4, 0x06), # 0x9A4 to 0x9A9 (0x09DA, 0x07), # 0x9DA to 0x9E0 (0xFB00, 0x20), # 0xFB00 to 0xFB19 ] if read_ship_state: addresses.extend([ (0xD821, 0x01), (0x0FB2, 0x02), ]) mem = SparseMemory.read_from(sock, *addresses) if mem is None: return None door_id = mem.short(0x78D) room_id = mem.short(0x79B) door = doors.from_id(door_id) room = rooms.from_id(room_id) game_state_id = mem.short(0x998) game_state = GameStates.get(game_state_id, hex(game_state_id)) collected_items_bitmask = mem.short(0x9A4) collected_beams_bitmask = mem.short(0x9A8) igt_frames = mem.short(0x9DA) igt_seconds = mem[0x9DC] igt_minutes = mem[0x9DE] igt_hours = mem[0x9E0] fps = 60.0 # TODO igt = FrameCount(216000 * igt_hours + 3600 * igt_minutes + 60 * igt_seconds + igt_frames) if read_ship_state: event_flags = mem[0xD821] ship_ai = mem.short(0xFB2) reached_ship = (event_flags & 0x40) > 0 and ship_ai == 0xaa4f else: reached_ship = False # Practice hack ram_load_preset = mem.short(0x0FB00) gametime_room = FrameCount(mem.short(0x0FB02)) last_gametime_room = FrameCount(mem.short(0x0FB04)) realtime_room = FrameCount(mem.short(0x0FB06)) last_realtime_room = FrameCount(mem.short(0x0FB08)) last_room_lag = FrameCount(mem.short(0x0FB0A)) last_door_lag_frames = FrameCount(mem.short(0x0FB0C)) transition_counter = FrameCount(mem.short(0x0FB0E)) last_realtime_door = FrameCount(mem.short(0x0FB12)) seg_rt_frames = mem.short(0x0FB14) seg_rt_seconds = mem.short(0x0FB16) seg_rt_minutes = mem.short(0x0FB18) seg_rt = FrameCount(3600 * seg_rt_minutes + 60 * seg_rt_seconds + seg_rt_frames) return State( door=door, room=room, game_state_id=game_state_id, game_state=game_state, igt=igt, seg_rt=seg_rt, gametime_room=gametime_room, last_gametime_room=last_gametime_room, realtime_room=realtime_room, last_realtime_room=last_realtime_room, last_realtime_door=last_realtime_door, last_room_lag=last_room_lag, last_door_lag_frames=last_door_lag_frames, transition_counter=transition_counter, ram_load_preset=ram_load_preset, items_bitmask='%x' % collected_items_bitmask, beams_bitmask='%x' % collected_beams_bitmask, items=items_string(imask=collected_items_bitmask), beams=beams_string(imask=collected_items_bitmask, bmask=collected_beams_bitmask), reached_ship=reached_ship, )