in gym/envs/toy_text/cliffwalking.py [0:0]
def _render_gui(self, mode):
try:
import pygame
except ImportError:
raise DependencyNotInstalled(
"pygame is not installed, run `pip install gym[toy_text]`"
)
if self.window_surface is None:
pygame.init()
if mode == "human":
pygame.display.init()
pygame.display.set_caption("CliffWalking")
self.window_surface = pygame.display.set_mode(self.window_size)
else: # rgb_array
self.window_surface = pygame.Surface(self.window_size)
if self.clock is None:
self.clock = pygame.time.Clock()
if self.elf_images is None:
hikers = [
path.join(path.dirname(__file__), "img/elf_up.png"),
path.join(path.dirname(__file__), "img/elf_right.png"),
path.join(path.dirname(__file__), "img/elf_down.png"),
path.join(path.dirname(__file__), "img/elf_left.png"),
]
self.elf_images = [
pygame.transform.scale(pygame.image.load(f_name), self.cell_size)
for f_name in hikers
]
if self.start_img is None:
file_name = path.join(path.dirname(__file__), "img/stool.png")
self.start_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
if self.goal_img is None:
file_name = path.join(path.dirname(__file__), "img/cookie.png")
self.goal_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
if self.mountain_bg_img is None:
bg_imgs = [
path.join(path.dirname(__file__), "img/mountain_bg1.png"),
path.join(path.dirname(__file__), "img/mountain_bg2.png"),
]
self.mountain_bg_img = [
pygame.transform.scale(pygame.image.load(f_name), self.cell_size)
for f_name in bg_imgs
]
if self.near_cliff_img is None:
near_cliff_imgs = [
path.join(path.dirname(__file__), "img/mountain_near-cliff1.png"),
path.join(path.dirname(__file__), "img/mountain_near-cliff2.png"),
]
self.near_cliff_img = [
pygame.transform.scale(pygame.image.load(f_name), self.cell_size)
for f_name in near_cliff_imgs
]
if self.cliff_img is None:
file_name = path.join(path.dirname(__file__), "img/mountain_cliff.png")
self.cliff_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
for s in range(self.nS):
row, col = np.unravel_index(s, self.shape)
pos = (col * self.cell_size[0], row * self.cell_size[1])
check_board_mask = row % 2 ^ col % 2
self.window_surface.blit(self.mountain_bg_img[check_board_mask], pos)
if self._cliff[row, col]:
self.window_surface.blit(self.cliff_img, pos)
if row < self.shape[0] - 1 and self._cliff[row + 1, col]:
self.window_surface.blit(self.near_cliff_img[check_board_mask], pos)
if s == self.start_state_index:
self.window_surface.blit(self.start_img, pos)
if s == self.nS - 1:
self.window_surface.blit(self.goal_img, pos)
if s == self.s:
elf_pos = (pos[0], pos[1] - 0.1 * self.cell_size[1])
last_action = self.lastaction if self.lastaction is not None else 2
self.window_surface.blit(self.elf_images[last_action], elf_pos)
if mode == "human":
pygame.event.pump()
pygame.display.update()
self.clock.tick(self.metadata["render_fps"])
else: # rgb_array
return np.transpose(
np.array(pygame.surfarray.pixels3d(self.window_surface)), axes=(1, 0, 2)
)