class BattleWindow(): def __init__(self, screen): self.screen = screen self.ctrlList = [] # 创建导演 self.director = Director(self.screen) # 创建定时器 self.alarm = Alarm() self.alarm.setTimeout(1000) self.alarm.start() # 创建调试信息 self.debugInfo = DebugInfo(screen) # fps相关 self.fps_count = 0 self.fps = 0 def processEvent(self, event): pass def doModel(self): while True: for event in pygame.event.get(): self.director.processEvent(event) # 导演类描画主窗体 self.director.draw() # debug类描画调试信息 self.debugInfo.clean() self.debugInfo.drawLine("fps: %d" % self.fps) if self.alarm.isTimeout(): self.fps = self.fps_count self.fps_count = 0 else: self.fps_count += 1 pygame.display.update()
def main(): global current_game_state # load high-score file high_scores = load_scores("scores.json") # set the random seed - produces more random trajectories random.seed() # start music if SOUND == True: background_track.play(-1) # list of all active explosions explosion_list = [] # list of all active missiles missile_list = [] # TBC - generate the cities # need to be replaced with working cities city_list = [] for i in range(1, 8): # 8 == Max num cities plus defence plus one if i == 8 // 2: # find centre point for gun pass else: city_list.append(City(i, 7)) # 7 == max num cities plus guns # Intercepter gun defence = Defence() # set the game running current_game_state = GAME_STATE_RUNNING show_high_scores(screen, high_scores) # setup the AI director director = Director(1, high_scores["1"]["score"]) while True: # write event handlers here for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN: if event.button == 1: # primary mouse button defence.shoot(missile_list) if event.button == 2: # middle mouse button pass if event.button == 3: # right mouse button pass if event.type == KEYDOWN: if event.key == K_ESCAPE: exit_game(screen) if event.key == K_SPACE: defence.shoot(missile_list) if event.key == K_p: pause_game(screen) if event.type == KEYUP: pass # clear the screen before drawing screen.fill(BACKGROUND) # Game logic and draws # --- cities for city in city_list: city.draw(screen) # --- interceptor turret defence.update() defence.draw(screen) # --- missiles for missile in missile_list[:]: missile.update(explosion_list) missile.draw(screen) if missile.detonated: missile_list.remove(missile) # --- explosions for explosion in explosion_list[:]: explosion.update() explosion.draw(screen) if explosion.complete: explosion_list.remove(explosion) # --- Draw the interface director.draw(screen, defence) # --- update game director if current_game_state == GAME_STATE_RUNNING: current_game_state = director.update(missile_list, explosion_list, city_list) # load message for Game Over and proceed to high-score / menu if current_game_state == GAME_STATE_OVER: director.game_over(screen) # load a message and set new game values for start new level if current_game_state == GAME_STATE_NEW_LEVEL: director.new_level(screen, defence) # Update the display pygame.display.update() # hold for few seconds before starting new level if current_game_state == GAME_STATE_NEW_LEVEL: time.sleep(3) current_game_state = GAME_STATE_RUNNING # hold for few seconds before proceeding to high-score or back to menu or game over splash if current_game_state == GAME_STATE_OVER: input_box = InputBox(100, 100, 140, 32) while input_box.check_finished() == False: for event in pygame.event.get(): input_box.handle_event(event) input_box.update() input_box.draw(screen) ''' if check_high_score != 0: high_scores = update_high_scores(director.get_player_score(), "XXX", high_scores) save_high_scores("scores.json", high_scores) time.sleep(3) # TBC - below line should move game on to new game state for menu/game-over/high-score/etc. ''' current_game_state = GAME_STATE_MENU # display the high scores if current_game_state == GAME_STATE_MENU: show_high_scores(screen, high_scores) current_game_state = 0 # run at pre-set fps clock.tick(FPS)