def game_loop(self): while not quit_or_esc(): command_1P = self._keyboard_action_1P.get_command() command_2P = self._keyboard_action_2P.get_command() scene_info = self._scene.get_scene_info() scene_info.command_1P = command_1P.value scene_info.command_2P = command_2P.value self._record_handler(scene_info) game_status = self._scene.update(command_1P, command_2P) if game_status == GameStatus.GAME_1P_WIN or \ game_status == GameStatus.GAME_2P_WIN: print(game_status.value) self._record_handler(self._scene.get_scene_info()) if self._game_over(game_status): break self._scene.reset() self._draw_scene() self._clock.tick(self._fps) self._print_result()
def game_loop(self): """ The game execution loop """ while not quit_or_esc(): # Get the command from the keyboard command = self._keyboard_action.get_command() # Record the scene information self._record_scene(command) # 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: # Record the scene info with the game over status self._record_scene(None) print("Score: {}".format(self._scene.score)) if self._one_shot_mode: return self._scene.reset() # Draw the scene to the display self._draw_scene() # Wait for the next frame self._clock.tick(self._fps)
def game_loop(self): while not quit_or_esc(): command_1P = self._keyboard_action_1P.get_command() command_2P = self._keyboard_action_2P.get_command() scene_info = self._scene.get_scene_info() scene_info["command_1P"] = command_1P.value scene_info["command_2P"] = command_2P.value self._record_handler(scene_info) game_status = self._scene.update(command_1P, command_2P) if game_status != GameStatus.GAME_ALIVE: print(game_status.value) scene_info = self._scene.get_scene_info() scene_info["command_1P"] = scene_info["command_2P"] = None self._record_handler(scene_info) if self._game_over(game_status): break self._scene.reset() self._screen.update(self._score, self._scene._ball.speed) self._clock.tick(self._fps) self._print_result()
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 main loop of the game execution """ keep_going = lambda: not quit_or_esc() comm.wait_all_ml_ready() while keep_going(): scene_info = self._scene.fill_scene_info_obj(SceneInfo()) # Send the scene info to the ml processes and wait for instructions instruction_1P, instruction_2P = self._make_ml_execute(scene_info) scene_info.command_1P = instruction_1P.command.value scene_info.command_2P = instruction_2P.command.value self._record_handler(scene_info) # Update the scene game_status = self._scene.update( \ instruction_1P.command, instruction_2P.command) self._draw_scene() # If either of two sides wins, reset the scene and wait for ml processes # getting ready for the next round if game_status == GameStatus.GAME_1P_WIN or \ game_status == GameStatus.GAME_2P_WIN: scene_info = self._scene.fill_scene_info_obj(SceneInfo()) self._record_handler(scene_info) comm.send_to_all_ml(scene_info) print("Frame: {}, Status: {}\n-----" \ .format(scene_info.frame, game_status.value)) if self._game_over(game_status): break self._scene.reset() self._frame_delayed = [0, 0] # Wait for ml processes doing their resetting jobs comm.wait_all_ml_ready() self._print_result() pygame.quit()
def game_loop(self): while not quit_or_esc(): command = self._keyboard.get_command() self._record_scene_info(command) game_status = self._scene.update(command) if game_status == GameStatus.GAME_OVER or \ game_status == GameStatus.GAME_PASS: print(game_status.value) self._record_scene_info(None) if self._one_shot_mode: return self._scene.reset() self._screen.update(self._scene.catch_ball_times) self._clock.tick(self._fps)
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()
def game_loop(self): """ The main loop of the game execution """ comm.wait_all_ml_ready() while not quit_or_esc(): scene_info = self._scene.get_scene_info() # Send the scene info to the ml processes and wait for commands command_1P, command_2P = self._make_ml_execute(scene_info) scene_info["command_1P"] = command_1P.value scene_info["command_2P"] = command_2P.value self._record_handler(scene_info) # Update the scene game_status = self._scene.update(command_1P, command_2P) self._screen.update(self._score, self._scene._ball.speed) # If either of two sides wins, reset the scene and wait for ml processes # getting ready for the next round if game_status != GameStatus.GAME_ALIVE: scene_info = self._scene.get_scene_info() comm.send_to_all_ml(scene_info) scene_info["command_1P"] = scene_info["command_2P"] = None self._record_handler(scene_info) print("Frame: {}, Status: {}".format(scene_info["frame"], game_status.value)) if self._game_over(game_status): break self._scene.reset() self._frame_delayed = [0, 0] # Wait for ml processes doing their resetting jobs comm.wait_all_ml_ready() self._print_result()
def game_loop(self): while not quit_or_esc(): command = self._keyboard.get_command() self._record_scene_info(command) game_status = self._scene.update(command) if game_status == GameStatus.GAME_OVER or \ game_status == GameStatus.GAME_PASS: print(game_status.value) self._record_scene_info(None) if self._one_shot_mode: return self._scene.reset() self._screen.fill((0, 0, 0)) self._scene.draw_gameobjects(self._screen) pygame.display.flip() self._clock.tick(self._fps)
def game_loop(self): while not quit_or_esc(): self._record_handler(self._scene.fill_scene_info_obj(SceneInfo())) control_action = self._keyboard.get_command() game_status = self._scene.update(control_action) if game_status == GameStatus.GAME_OVER or \ game_status == GameStatus.GAME_PASS: print(game_status.value) self._record_handler( self._scene.fill_scene_info_obj(SceneInfo())) if self._one_shot_mode: return self._scene.reset() self._screen.fill((0, 0, 0)) self._scene.draw_gameobjects(self._screen) pygame.display.flip() self._clock.tick(self._fps) pygame.quit()
def game_loop(self): while not quit_or_esc(): command_1P = self._keyboard_action_1P.get_command() command_2P = self._keyboard_action_2P.get_command() scene_info = self._scene.fill_scene_info_obj(SceneInfo()) scene_info.command_1P = command_1P.value scene_info.command_2P = command_2P.value self._record_handler(scene_info) game_status = self._scene.update(command_1P, command_2P) if game_status == GameStatus.GAME_1P_WIN or \ game_status == GameStatus.GAME_2P_WIN: print(game_status.value) self._record_handler( self._scene.fill_scene_info_obj(SceneInfo())) if self._game_over(game_status): break self._scene.reset() self._screen.fill((0, 0, 0)) self._scene.draw_gameobjects(self._screen) self._draw_game_status() pygame.display.flip() self._clock.tick(self._fps) if self._score[0] > self._score[1]: print("1P wins!") else: print("2P wins!") print("Final score: {}-{}".format(*self._score)) pygame.quit()
import pygame from src import MazeCar from src.env import FPS from mlgame.view.view import PygameView from mlgame.gamedev.generic import quit_or_esc SOUND_OFF = "off" MOVE_MAZE = "MOVE_MAZE" if __name__ == '__main__': pygame.init() # game = MazeCar.MazeCar(6, "MAZE", 6, 10000, 3, "off") # game = MazeCar.MazeCar(user_num=2, game_type=MOVE_MAZE, map=5, time=2000, sensor=3, sound=SOUND_OFF) game = MazeCar.MazeCar(2, "PRACTICE", 12, 10000, 5, "off") scene_init_info_dict = game.get_scene_init_data() game_view = PygameView(scene_init_info_dict) frame_count = 0 while game.is_running and not quit_or_esc(): pygame.time.Clock().tick_busy_loop(FPS) game.update(game.get_keyboard_command()) game_progress_data = game.get_scene_progress_data() game_view.draw(game_progress_data) frame_count += 1 pygame.quit()
from src import RacingCar from src.env import FPS from mlgame.view.view import PygameView from mlgame.gamedev.generic import quit_or_esc if __name__ == '__main__': pygame.init() # game = RacingCar.RacingCar(user_num=2, game_mode="NORMAL", car_num=50, racetrack_length=10000, game_times=1, sound="off") game = RacingCar.RacingCar(user_num=4, game_mode="RELIVE", car_num=20, racetrack_length=1000, game_times=1, sound="off") # game = RacingCar.RacingCar(2, "COIN", 30, 1, "off") scene_init_info_dict = game.get_scene_init_data() game_view = PygameView(scene_init_info_dict) interval = 1 / 30 frame_count = 0 while game.isRunning() and not quit_or_esc(): pygame.time.Clock().tick_busy_loop(FPS) game.update(game.get_keyboard_command()) game_progress_data = game.get_scene_progress_data() game_view.draw_screen() game_view.draw(game_progress_data) game_view.flip() frame_count += 1 pygame.quit()