def game_loop(self): """ The main loop of the game in machine learning mode The execution order in the loop: 1. Send the scene information to the machine learning process. 2. Wait for the key frame (During this period, machine learning process can process the information and generate the game instruction.) 3. Check if there has a command to receive. If it has, receive the command. Otherwise, generate a dummy one. 4. Pass the command received to the game and update the scene. 5. If the game is over or passed, send the scene information containing the game status to the machine learning process, and reset the game. 6. Back to 1. """ comm.wait_ml_ready(self._ml_name) while not quit_or_esc(): scene_info = self._scene.get_scene_info() command = self._make_ml_execute(scene_info) scene_info["command"] = command.value self._record_handler(scene_info) game_status = self._scene.update(command) self._screen.update(self._scene.catch_ball_times) if (game_status == GameStatus.GAME_OVER or game_status == GameStatus.GAME_PASS): scene_info = self._scene.get_scene_info() comm.send_to_ml(scene_info, self._ml_name) scene_info["command"] = None self._record_handler(scene_info) print(game_status.value) if self._one_shot_mode: return self._scene.reset() self._frame_delayed = 0 # Wait for ml process doing resetting jobs comm.wait_ml_ready(self._ml_name)
def game_loop(self): """ The game execution loop """ # Wait for the ml process comm.wait_ml_ready(self._ml_name) while not quit_or_esc(): # Generate the scene information scene_info = self._scene.get_scene_info() # Send the scene information to the ml process # and wait the command sent from it command = self._make_ml_execute(scene_info) # Record the scene information scene_info["command"] = command.value self._record_handler(scene_info) # Update the scene game_status = self._scene.update(command) # If the game is over, reset the scene or # quit the game loop if one shot mode is set. if game_status == GameStatus.GAME_OVER: # Send the scene info with the game over status # and record that scene info scene_info = self._scene.get_scene_info() comm.send_to_ml(scene_info, self._ml_name) scene_info["command"] = None self._record_handler(scene_info) if self._one_shot_mode: return self._scene.reset() self._frame_delayed = 0 # Wait for the ml process for the next round comm.wait_ml_ready(self._ml_name) # Draw the scene to the display self._draw_scene()
def game_loop(self): """The main loop of the game in machine learning mode The execution order in the loop: 1. Send the SceneInfo to the machine learning process. 2. Wait for the key frame (During this period, machine learning process can process the SceneInfo and generate the GameInstruction.) 3. Check if there has a GameInstruction to receive. If it has, receive the instruction. Otherwise, generate a dummy one. 4. Pass the GameInstruction to the game and update the scene (and frame no.) 5. If the game is over or passed, send the SceneInfo containing the game status to the machine learning process, and reset the game. 6. Back to 1. """ keep_going = lambda: not quit_or_esc() comm.wait_ml_ready(self._ml_name) while keep_going(): scene_info = self._scene.fill_scene_info_obj(SceneInfo()) self._record_handler(scene_info) instruction = self._make_ml_execute(scene_info) game_status = self._scene.update(instruction.command) self._draw_scene() if game_status == GameStatus.GAME_OVER or \ game_status == GameStatus.GAME_PASS: scene_info = self._scene.fill_scene_info_obj(SceneInfo()) self._record_handler(scene_info) comm.send_to_ml(scene_info, self._ml_name) if self._one_shot_mode: return self._scene.reset() self._frame_delayed = 0 # Wait for ml process doing resetting jobs comm.wait_ml_ready(self._ml_name) pygame.quit()