PyGame-Learning-Environment icon indicating copy to clipboard operation
PyGame-Learning-Environment copied to clipboard

pygame.error: posted event keycode must be int

Open SamWqc opened this issue 4 years ago • 0 comments

MacBook Air --MAC OS 10.14.6 Python3.73 64bit Wrappered the environment: `#Code based on https://github.com/KibaAmor/rl-code/blob/master/demos/dqn_flappybird/flappybird_wrapper.py import numpy as np from gym import Env, spaces from ple import PLE from ple.games import FlappyBird

class FlappyBirdWrapper(Env): def init(self, **kwargs): self.game = FlappyBird() self.p = PLE(self.game, **kwargs) self.action_set = self.p.getActionSet()

    # 
    self.observation_space = spaces.Discrete(3)
    # 
    self.action_space = spaces.Discrete(2)

def _get_obs(self):
    # 
    state = self.game.getGameState()
    # 
    dist_to_pipe_horz = state["next_pipe_dist_to_player"]
    dist_to_pipe_bottom = state["player_y"] - state["next_pipe_top_y"]

    velocity = state['player_vel']
    return np.array([dist_to_pipe_horz, dist_to_pipe_bottom, velocity])

def reset(self):
    self.p.reset_game()
    return self._get_obs()

def step(self, action):
    reward = self.p.act(self.action_set[action])
    obs = self._get_obs()
    done = self.p.game_over()
    return obs, reward, done, dict()

def seed(self, *args, **kwargs):
    pass

def render(self, *args, **kwargs):
    pass`

Error happened when running: File "/Users/Sam/Desktop/MSACoures/WorkShop/RL_FlappyBird/Test.py", line 31, in <module> watch() File "/Users/Sam/Desktop/MSACoures/WorkShop/RL_FlappyBird/Test.py", line 19, in watch next_state, reward, done, _ = env.step(action) File "/Users/Sam/Desktop/MSACoures/WorkShop/RL_FlappyBird/Env.py", line 34, in step reward = self.p.act(self.action_set[action]) File "/Users/Sam/PyGame-Learning-Environment/ple/ple.py", line 376, in act return sum(self._oneStepAct(action) for i in range(self.frame_skip)) File "/Users/Sam/PyGame-Learning-Environment/ple/ple.py", line 376, in <genexpr> return sum(self._oneStepAct(action) for i in range(self.frame_skip)) File "/Users/Sam/PyGame-Learning-Environment/ple/ple.py", line 395, in _oneStepAct self._setAction(action) File "/Users/Sam/PyGame-Learning-Environment/ple/ple.py", line 411, in _setAction self.game._setAction(action, self.last_action) File "/Users/Sam/PyGame-Learning-Environment/ple/games/base/pygamewrapper.py", line 79, in _setAction pygame.event.post(ku) pygame.error: posted event keycode must be int

And I found the last action may be '[]', error happens here if last_action is None: last_action = self.NOOP

if last_action is None or last_action==[]:` fixed the issue

SamWqc avatar Dec 15 '20 16:12 SamWqc