Fix issues with pygame event handling (#2684)

* Fix issues with pygame event handling

* Fix display initialization and exit for jupyter
This commit is contained in:
Andrew Tan Jin Shen
2022-03-12 00:37:04 +08:00
committed by GitHub
parent 29cf1e5393
commit 6f1ec7cc1b
10 changed files with 28 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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