From 6f1ec7cc1bbd9feb402e96552b39fd12e366b7fc Mon Sep 17 00:00:00 2001 From: Andrew Tan Jin Shen Date: Sat, 12 Mar 2022 00:37:04 +0800 Subject: [PATCH] Fix issues with pygame event handling (#2684) * Fix issues with pygame event handling * Fix display initialization and exit for jupyter --- gym/envs/box2d/bipedal_walker.py | 4 +++- gym/envs/box2d/car_racing.py | 3 +++ gym/envs/box2d/lunar_lander.py | 3 +++ gym/envs/classic_control/acrobot.py | 3 +++ gym/envs/classic_control/cartpole.py | 3 +++ gym/envs/classic_control/continuous_mountain_car.py | 3 +++ gym/envs/classic_control/mountain_car.py | 3 +++ gym/envs/classic_control/pendulum.py | 3 +++ gym/envs/toy_text/blackjack.py | 2 ++ gym/envs/toy_text/frozen_lake.py | 2 ++ 10 files changed, 28 insertions(+), 1 deletion(-) diff --git a/gym/envs/box2d/bipedal_walker.py b/gym/envs/box2d/bipedal_walker.py index 063dc1fd4..253614980 100644 --- a/gym/envs/box2d/bipedal_walker.py +++ b/gym/envs/box2d/bipedal_walker.py @@ -536,9 +536,9 @@ class BipedalWalker(gym.Env, EzPickle): return np.array(state, dtype=np.float32), reward, done, {} def render(self, mode="human"): - if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((VIEWPORT_W, VIEWPORT_H)) if self.clock is None: self.clock = pygame.time.Clock() @@ -655,6 +655,7 @@ class BipedalWalker(gym.Env, EzPickle): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (-self.scroll * SCALE, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -667,6 +668,7 @@ class BipedalWalker(gym.Env, EzPickle): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/box2d/car_racing.py b/gym/envs/box2d/car_racing.py index db455b3a6..846d3748c 100644 --- a/gym/envs/box2d/car_racing.py +++ b/gym/envs/box2d/car_racing.py @@ -439,6 +439,7 @@ class CarRacing(gym.Env, EzPickle): def render(self, mode="human"): assert mode in ["human", "state_pixels", "rgb_array"] if self.screen is None and mode == "human": + pygame.display.init() self.screen = pygame.display.set_mode((WINDOW_W, WINDOW_H)) if self.clock is None: self.clock = pygame.time.Clock() @@ -472,6 +473,7 @@ class CarRacing(gym.Env, EzPickle): self.surf.blit(text, text_rect) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) self.screen.fill(0) self.screen.blit(self.surf, (0, 0)) @@ -603,6 +605,7 @@ class CarRacing(gym.Env, EzPickle): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/box2d/lunar_lander.py b/gym/envs/box2d/lunar_lander.py index ac90593d1..244140aee 100644 --- a/gym/envs/box2d/lunar_lander.py +++ b/gym/envs/box2d/lunar_lander.py @@ -447,6 +447,7 @@ class LunarLander(gym.Env, EzPickle): def render(self, mode="human"): if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((VIEWPORT_W, VIEWPORT_H)) if self.clock is None: self.clock = pygame.time.Clock() @@ -533,6 +534,7 @@ class LunarLander(gym.Env, EzPickle): self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -545,6 +547,7 @@ class LunarLander(gym.Env, EzPickle): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/classic_control/acrobot.py b/gym/envs/classic_control/acrobot.py index 3a3e2353e..12563b68d 100644 --- a/gym/envs/classic_control/acrobot.py +++ b/gym/envs/classic_control/acrobot.py @@ -269,6 +269,7 @@ class AcrobotEnv(core.Env): def render(self, mode="human"): if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((self.SCREEN_DIM, self.SCREEN_DIM)) if self.clock is None: self.clock = pygame.time.Clock() @@ -326,6 +327,7 @@ class AcrobotEnv(core.Env): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -338,6 +340,7 @@ class AcrobotEnv(core.Env): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/classic_control/cartpole.py b/gym/envs/classic_control/cartpole.py index f9d1a658c..3773b7bd3 100644 --- a/gym/envs/classic_control/cartpole.py +++ b/gym/envs/classic_control/cartpole.py @@ -204,6 +204,7 @@ class CartPoleEnv(gym.Env[np.ndarray, Union[int, np.ndarray]]): if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((screen_width, screen_height)) if self.clock is None: self.clock = pygame.time.Clock() @@ -255,6 +256,7 @@ class CartPoleEnv(gym.Env[np.ndarray, Union[int, np.ndarray]]): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -267,5 +269,6 @@ class CartPoleEnv(gym.Env[np.ndarray, Union[int, np.ndarray]]): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/classic_control/continuous_mountain_car.py b/gym/envs/classic_control/continuous_mountain_car.py index 14202c7e5..7168c907b 100644 --- a/gym/envs/classic_control/continuous_mountain_car.py +++ b/gym/envs/classic_control/continuous_mountain_car.py @@ -184,6 +184,7 @@ class Continuous_MountainCarEnv(gym.Env): carheight = 20 if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((screen_width, screen_height)) if self.clock is None: self.clock = pygame.time.Clock() @@ -248,6 +249,7 @@ class Continuous_MountainCarEnv(gym.Env): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -260,5 +262,6 @@ class Continuous_MountainCarEnv(gym.Env): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/classic_control/mountain_car.py b/gym/envs/classic_control/mountain_car.py index 76846fdb4..c1e624594 100644 --- a/gym/envs/classic_control/mountain_car.py +++ b/gym/envs/classic_control/mountain_car.py @@ -160,6 +160,7 @@ class MountainCarEnv(gym.Env): carheight = 20 if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((screen_width, screen_height)) if self.clock is None: self.clock = pygame.time.Clock() @@ -224,6 +225,7 @@ class MountainCarEnv(gym.Env): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -240,5 +242,6 @@ class MountainCarEnv(gym.Env): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/classic_control/pendulum.py b/gym/envs/classic_control/pendulum.py index cfb642b5f..525186e1d 100644 --- a/gym/envs/classic_control/pendulum.py +++ b/gym/envs/classic_control/pendulum.py @@ -142,6 +142,7 @@ class PendulumEnv(gym.Env): def render(self, mode="human"): if self.screen is None: pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((self.screen_dim, self.screen_dim)) if self.clock is None: self.clock = pygame.time.Clock() @@ -203,6 +204,7 @@ class PendulumEnv(gym.Env): self.surf = pygame.transform.flip(self.surf, False, True) self.screen.blit(self.surf, (0, 0)) if mode == "human": + pygame.event.pump() self.clock.tick(self.metadata["render_fps"]) pygame.display.flip() @@ -215,6 +217,7 @@ class PendulumEnv(gym.Env): def close(self): if self.screen is not None: + pygame.display.quit() pygame.quit() self.isopen = False diff --git a/gym/envs/toy_text/blackjack.py b/gym/envs/toy_text/blackjack.py index 162a69a71..00ce6b6c2 100644 --- a/gym/envs/toy_text/blackjack.py +++ b/gym/envs/toy_text/blackjack.py @@ -178,6 +178,7 @@ class BlackjackEnv(gym.Env): if not hasattr(self, "screen"): if mode == "human": pygame.init() + pygame.display.init() self.screen = pygame.display.set_mode((screen_width, screen_height)) else: pygame.font.init() @@ -266,6 +267,7 @@ class BlackjackEnv(gym.Env): ), ) if mode == "human": + pygame.event.pump() pygame.display.update() self.clock.tick(self.metadata["render_fps"]) else: diff --git a/gym/envs/toy_text/frozen_lake.py b/gym/envs/toy_text/frozen_lake.py index 60d68400b..2b937de30 100644 --- a/gym/envs/toy_text/frozen_lake.py +++ b/gym/envs/toy_text/frozen_lake.py @@ -244,6 +244,7 @@ class FrozenLakeEnv(Env): def _render_gui(self, desc, mode): if self.window_surface is None: pygame.init() + pygame.display.init() pygame.display.set_caption("Frozen Lake") if mode == "human": self.window_surface = pygame.display.set_mode(self.window_size) @@ -336,6 +337,7 @@ class FrozenLakeEnv(Env): self.window_surface.blit(board, board.get_rect()) if mode == "human": + pygame.event.pump() pygame.display.update() self.clock.tick(self.metadata["render_fps"]) else: # rgb_array