UIElement(UIButton, 300, 50, text='exit', ui_id='reconnect-exit-button') ], margin=Margin.only(bottom=20), padding=Padding.all(50), hor_layout=UI.Center, ver_layout=UI.Top)) game.add_interface( name='game', interface=UI(elements=[UIElement(UILabel, 300, 100, text='', visible=0)]), ) game.add_handler(interface_name='game', handler=lambda event: quit_from_game(event, game)) game.add_handler( 'reconnect', lambda event: change_interface(event, 'play-again-button', game, 'ip-entry')) game.add_handler( 'reconnect', lambda event: exit_button_handler(event, 'reconnect-exit-button')) # ip entry -> game game.add_handler(interface_name='ip-entry', handler=lambda event: load_game(event, game)) # exit button (Native) game.add_global_handler(exit_event_handler) # run game game.start()
def controller(strategies, grid_size, candy_ratio=1., max_iter=None, verbose=0, gui_active=False, game_speed=None): # Pygame Init pygame.init() clock = pygame.time.Clock() if gui_active: gui_options = gui.Options() win = gui.Window(grid_size, 'Multiplayer Snake', gui_options) quit_game = False # Start Game game = Game(grid_size, len(strategies), candy_ratio=candy_ratio, max_iter=max_iter) # state = game.startState() state = game.start(strategies) prev_human_action = None game_over = False agent_names = [a.name for a in strategies] if "human" in agent_names: waiting = True while waiting: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: waiting = False break i_human = None if "human" in agent_names: i_human = agent_names.index("human") while not ((gui_active and quit_game) or ((not gui_active) and game_over)): # Print state if verbose > 0: state.printGrid(game.grid_size) # Get events if gui_active: events = pygame.event.get() if pygame.QUIT in [ev.type for ev in events]: quit_game = True continue # Compute the actions for each player following its strategy (except human) actions = game.agentActions() # Compute human strategy if necessary human_action = None if i_human is not None: speed = 2. if pygame.K_SPACE in [ ev.key for ev in events if ev.type == pygame.KEYDOWN ] else 1. arrow_key = False for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: human_action = move.Move((-1, 0), speed) arrow_key = True if event.key == pygame.K_RIGHT: human_action = move.Move((1, 0), speed) arrow_key = True if event.key == pygame.K_UP: human_action = move.Move((0, -1), speed) arrow_key = True if event.key == pygame.K_DOWN: human_action = move.Move((0, 1), speed) arrow_key = True if not arrow_key and prev_human_action is None: human_action = move.Move((0, -1), speed) elif not arrow_key: human_action = prev_human_action # Assign human action if i_human is not None and i_human in list(actions.keys()): actions[i_human] = human_action prev_human_action = human_action if verbose > 1: print(state) print(actions) # Update the state if not game_over: state = game.succ(state, actions, copy=False) # Pause if game_speed: clock.tick(game_speed) # Check if game over game_over = game.isEnd(state) # if game_over: # win.print_message('GAME OVER') # Update gui if gui_active: win.updateSprites(state) win.refresh() if verbose > 0: state.printGrid(game.grid_size) return state