in gym/envs/toy_text/taxi.py [0:0]
def _render_gui(self, mode):
try:
import pygame # dependency to pygame only if rendering with human
except ImportError:
raise DependencyNotInstalled(
"pygame is not installed, run `pip install gym[toy_text]`"
)
if self.window is None:
pygame.init()
pygame.display.set_caption("Taxi")
if mode == "human":
self.window = pygame.display.set_mode(WINDOW_SIZE)
elif mode == "rgb_array":
self.window = pygame.Surface(WINDOW_SIZE)
assert (
self.window is not None
), "Something went wrong with pygame. This should never happen."
if self.clock is None:
self.clock = pygame.time.Clock()
if self.taxi_imgs is None:
file_names = [
path.join(path.dirname(__file__), "img/cab_front.png"),
path.join(path.dirname(__file__), "img/cab_rear.png"),
path.join(path.dirname(__file__), "img/cab_right.png"),
path.join(path.dirname(__file__), "img/cab_left.png"),
]
self.taxi_imgs = [
pygame.transform.scale(pygame.image.load(file_name), self.cell_size)
for file_name in file_names
]
if self.passenger_img is None:
file_name = path.join(path.dirname(__file__), "img/passenger.png")
self.passenger_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
if self.destination_img is None:
file_name = path.join(path.dirname(__file__), "img/hotel.png")
self.destination_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
self.destination_img.set_alpha(170)
if self.median_horiz is None:
file_names = [
path.join(path.dirname(__file__), "img/gridworld_median_left.png"),
path.join(path.dirname(__file__), "img/gridworld_median_horiz.png"),
path.join(path.dirname(__file__), "img/gridworld_median_right.png"),
]
self.median_horiz = [
pygame.transform.scale(pygame.image.load(file_name), self.cell_size)
for file_name in file_names
]
if self.median_vert is None:
file_names = [
path.join(path.dirname(__file__), "img/gridworld_median_top.png"),
path.join(path.dirname(__file__), "img/gridworld_median_vert.png"),
path.join(path.dirname(__file__), "img/gridworld_median_bottom.png"),
]
self.median_vert = [
pygame.transform.scale(pygame.image.load(file_name), self.cell_size)
for file_name in file_names
]
if self.background_img is None:
file_name = path.join(path.dirname(__file__), "img/taxi_background.png")
self.background_img = pygame.transform.scale(
pygame.image.load(file_name), self.cell_size
)
desc = self.desc
for y in range(0, desc.shape[0]):
for x in range(0, desc.shape[1]):
cell = (x * self.cell_size[0], y * self.cell_size[1])
self.window.blit(self.background_img, cell)
if desc[y][x] == b"|" and (y == 0 or desc[y - 1][x] != b"|"):
self.window.blit(self.median_vert[0], cell)
elif desc[y][x] == b"|" and (
y == desc.shape[0] - 1 or desc[y + 1][x] != b"|"
):
self.window.blit(self.median_vert[2], cell)
elif desc[y][x] == b"|":
self.window.blit(self.median_vert[1], cell)
elif desc[y][x] == b"-" and (x == 0 or desc[y][x - 1] != b"-"):
self.window.blit(self.median_horiz[0], cell)
elif desc[y][x] == b"-" and (
x == desc.shape[1] - 1 or desc[y][x + 1] != b"-"
):
self.window.blit(self.median_horiz[2], cell)
elif desc[y][x] == b"-":
self.window.blit(self.median_horiz[1], cell)
for cell, color in zip(self.locs, self.locs_colors):
color_cell = pygame.Surface(self.cell_size)
color_cell.set_alpha(128)
color_cell.fill(color)
loc = self.get_surf_loc(cell)
self.window.blit(color_cell, (loc[0], loc[1] + 10))
taxi_row, taxi_col, pass_idx, dest_idx = self.decode(self.s)
if pass_idx < 4:
self.window.blit(self.passenger_img, self.get_surf_loc(self.locs[pass_idx]))
if self.lastaction in [0, 1, 2, 3]:
self.taxi_orientation = self.lastaction
dest_loc = self.get_surf_loc(self.locs[dest_idx])
taxi_location = self.get_surf_loc((taxi_row, taxi_col))
if dest_loc[1] <= taxi_location[1]:
self.window.blit(
self.destination_img,
(dest_loc[0], dest_loc[1] - self.cell_size[1] // 2),
)
self.window.blit(self.taxi_imgs[self.taxi_orientation], taxi_location)
else: # change blit order for overlapping appearance
self.window.blit(self.taxi_imgs[self.taxi_orientation], taxi_location)
self.window.blit(
self.destination_img,
(dest_loc[0], dest_loc[1] - self.cell_size[1] // 2),
)
if mode == "human":
pygame.display.update()
self.clock.tick(self.metadata["render_fps"])
elif mode == "rgb_array":
return np.transpose(
np.array(pygame.surfarray.pixels3d(self.window)), axes=(1, 0, 2)
)