def recordPlayerGames(dest, games_to_play = 10): disp = basicdisplayer.GameWindow(gameparams.windowwidth, gameparams.windowheight) blue_agent = humanagent.HumanAgent(('w', 'd', 's', 'a', 'LSHIFT'), disp) red_agent = humanagent.HumanAgent(('UP', 'RIGHT', 'DOWN', 'LEFT', 'RCTRL'), disp) pygame.init() for game_number in range(games_to_play): sim = gs.GameSim(1,1,1) game_done = False game_log = log.Game() while not game_done: disp.updateKeys() red_move = red_agent.getAction() blue_move = blue_agent.getAction() sim.giveCommands([red_move, blue_move]) sim.step() game_log.append(sim.log()) disp.drawFrame(sim.log()) goals = sim.checkGoals() if sum(goals) > 0: game_done = True game_log.red_goals = goals[0] game_log.blue_goals = goals[1] disp.getInput() if disp.rip: return game_log.save(f"{dest}/{game_number}")
def render(self, mode='human'): # If the display hasn't been created, create it if self.display == None: self.display = basicdisplayer.GameWindow(gameparams.windowwidth, gameparams.windowheight) self.display.drawFrame(self.game_sim.log()) # Support moving and closing the window self.display.getInput() if self.display.rip: self.display.shutdown()
def main(): model_1 = torch.load("models/hybridloss_step_2.model") model_2 = torch.load("models/champion3_v1.model") #if torch.cuda.is_available(): # model_1 = model_1.cuda() # model_2 = model_2.cuda() # Intialise the graphical interface of the game red_debug_surf = movedisplayer.DebugSurf() blue_debug_surf = movedisplayer.DebugSurf() #disp = basicdisplayer.GameWindow(gp.windowwidth, gp.windowheight) disp = basicdisplayer.GameWindow(gp.windowwidth + 2 * 256, gp.windowheight,\ debug_surfs = [red_debug_surf.surf, blue_debug_surf.surf]) red_player_count = 1 blue_player_count = 1 ball_count = 1 # Doesn't work with >1 yet as balls reset in the exact center # Intialise the agents in the order of all reds sequentially, then blues agents = [] # Red agents redA = ACagent.ACAgent(model_2, "red", "random", red_debug_surf, False) #agents.append(humanagent.HumanAgent(('w', 'd', 's', 'a', 'LSHIFT'), disp)) agents.append(redA) # agents.append(randomagent.RandomAgent()) # Blue agents blueA = ACagent.ACAgent(model_1, "blue", "random", blue_debug_surf, False) agents.append(humanACagent.HumanACAgent(('UP', 'RIGHT', 'DOWN', 'LEFT', 'u'), disp, blueA)) #agents.append(blueA) # agents.append(randomagent.RandomAgent()) if False: t1 = time.time() model_testers.duel_trials.playGames(redA,blueA, 100, randStart = True) t2 = time.time() print(f"Took {t2-t1} seconds.") # Initialise the game simulator game = gamesim.GameSim(red_player_count, blue_player_count, ball_count, printDebug = True, auto_score = True, rand_reset = True) game.run(disp, agents)
def main(): if args.suppress_display and (args.red_human or args.blue_human): raise ValueError("Human players need display to function") red_debug_surf = movedisplayer.DebugSurf() blue_debug_surf = movedisplayer.DebugSurf() if not args.suppress_display: display = basicdisplayer.GameWindow(gp.windowwidth + 2 * 256, gp.windowheight,\ debug_surfs = [red_debug_surf.surf, blue_debug_surf.surf]) else: display = None agents = getAgents(display, red_debug_surf, blue_debug_surf) game = gamesim.GameSim(1, 1, 1, printDebug=args.print_debug, print_score_update=not args.suppress_scorekeeping, auto_score=args.auto_score, rand_reset=args.rand_reset, max_steps=args.max_steps) # Run the game for game_number in range(args.max_games): exit_loop = False # Initialise the game logger if needed if args.save_dir != "None": game_logger = log.Game() if args.save_step_length == -1: args.save_step_length = args.step_length save_counter = 0 if not os.path.exists(f"{args.save_master_dir}"): os.makedirs(f"{args.save_master_dir}") while True: game_ended = False # Query each agent on what commands should be sent to the game simulator if display != None: display.getInput() game.giveCommands([a.getAction(game.log()) for a in agents]) for i in range(args.step_length): game_ended = game_ended or game.step() # Append a frame to the game_logger if enabled if args.save_dir != "None": save_counter += 1 if save_counter == args.save_step_length: save_counter = 0 game_logger.append(game.log()) if game_ended: # Save the game logger data if enabled if args.save_dir != "None": if not os.path.exists( f"{args.save_master_dir}/{args.save_dir}"): os.makedirs( f"{args.save_master_dir}/{args.save_dir}") game_logger.save( f"{args.save_master_dir}/{args.save_dir}/{game_number}" ) break if game_ended: break if display != None: # Update the graphical interface canvas display.drawFrame(game.log()) if display.rip: display.shutdown() exit_loop = True break if exit_loop: break