def render(self, mode='human'): # DO NOT TRANSLATE TO OTHER LANGUAGES UNLESS YOU ARE BRAVE screen_width = 600 screen_height = 400 world_width = self.max_position - self.min_position scale = screen_width / world_width carwidth = 40 carheight = 20 if self.viewer is None: import rendering self.viewer = rendering.Viewer(screen_width, screen_height) xs = np.linspace(self.min_position, self.max_position, 100) ys = self.height(xs) xys = list(zip((xs - self.min_position) * scale, ys * scale)) self.track = rendering.make_polyline(xys) self.track.set_linewidth(4) self.viewer.add_geom(self.track) clearance = 10 l, r, t, b = -carwidth / 2, carwidth / 2, carheight, 0 car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) car.add_attr(rendering.Transform(translation=(0, clearance))) self.cartrans = rendering.Transform() car.add_attr(self.cartrans) self.viewer.add_geom(car) frontwheel = rendering.make_circle(carheight / 2.5) frontwheel.set_color(.5, .5, .5) frontwheel.add_attr( rendering.Transform(translation=(carwidth / 4, clearance))) frontwheel.add_attr(self.cartrans) self.viewer.add_geom(frontwheel) backwheel = rendering.make_circle(carheight / 2.5) backwheel.add_attr( rendering.Transform(translation=(-carwidth / 4, clearance))) backwheel.add_attr(self.cartrans) backwheel.set_color(.5, .5, .5) self.viewer.add_geom(backwheel) flagx = (self.goal_position - self.min_position) * scale flagy1 = self.height(self.goal_position) * scale flagy2 = flagy1 + 50 flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2)) self.viewer.add_geom(flagpole) flag = rendering.FilledPolygon([(flagx, flagy2), (flagx, flagy2 - 10), (flagx + 25, flagy2 - 5)]) flag.set_color(.8, .8, 0) self.viewer.add_geom(flag) pos = self.state[0] self.cartrans.set_translation((pos - self.min_position) * scale, self.height(pos) * scale) self.cartrans.set_rotation(math.cos(3 * pos)) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): screen_width = 600 screen_height = 400 world_width = self.x_threshold * 2 scale = screen_width / world_width carty = 100 # TOP OF CART polewidth = 10.0 polelen = scale * 1.0 cartwidth = 50.0 cartheight = 30.0 if self.viewer is None: import rendering self.viewer = rendering.Viewer(screen_width, screen_height) l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2 axleoffset = cartheight / 4.0 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.carttrans = rendering.Transform() cart.add_attr(self.carttrans) self.viewer.add_geom(cart) l, r, t, b = -polewidth / 2, polewidth / 2, polelen - polewidth / 2, -polewidth / 2 pole = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) pole.set_color(.8, .6, .4) self.poletrans = rendering.Transform(translation=(0, axleoffset)) pole.add_attr(self.poletrans) pole.add_attr(self.carttrans) self.viewer.add_geom(pole) self.axle = rendering.make_circle(polewidth / 2) self.axle.add_attr(self.poletrans) self.axle.add_attr(self.carttrans) self.axle.set_color(.5, .5, .8) self.viewer.add_geom(self.axle) self.track = rendering.Line((0, carty), (screen_width, carty)) self.track.set_color(0, 0, 0) self.viewer.add_geom(self.track) if self.state is None: return None x = self.state cartx = x[0] * scale + screen_width / 2.0 # MIDDLE OF CART self.carttrans.set_translation(cartx, carty) self.poletrans.set_rotation(-x[2]) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def __init__(self, pos, glx, _id, nears): self.glx = glx self.pos = pos self._id = _id self.nears = nears self.err = 0 x, y = pos x, y = x * SCALE, y * SCALE lx, rx = x - Desk.Ws / 2 + OFFX, x + Desk.Ws / 2 + OFFX by, ty = y - Desk.Hs / 2 + OFFY, y + Desk.Hs / 2 + OFFY shape = rendering.FilledPolygon([(lx, by), (lx, ty), (rx, ty), (rx, by)]) shape.set_color(0.8, 0.1, 0.7) viewer.add_geom(shape) viewer.draw_polyline([(lx, by), (lx, ty), (rx, ty), (rx, by)])
def __init__(self, pos, tlx, _id, nears): self.tlx = tlx # Target lx of the desk self.pos = pos self._id = _id self.nears = nears self.err = 0 x, y = pos x, y = x * SCALE, y * SCALE lx, rx = x - Desk.Ws / 2 + OFFX, x + Desk.Ws / 2 + OFFX by, ty = y - Desk.Hs / 2 + OFFY, y + Desk.Hs / 2 + OFFY self.shape = rendering.FilledPolygon([(lx, by), (lx, ty), (rx, ty), (rx, by)]) self.center = rendering.make_circle(x + OFFX, y + OFFY, radius=Desk.Hs / 4) self.set_shape_color() self.set_center_color(self.tlx) viewer.add_geom(self.shape) viewer.add_geom(self.center) viewer.draw_polyline([(lx, by), (lx, ty), (rx, ty), (rx, by)])
def render(self, color=None): rec = rendering.FilledPolygon([p.to_list() for p in self.vertices]) if not color: color = self.color rec.set_color(color[0], color[1], color[2]) return [rec]
def render(self, mode='human', close=False): scale = 6 screen_width = scale * self.width screen_height = scale * self.height if self.viewer is None: #TODO: remove when it works import importlib import rendering importlib.reload(rendering) self.viewer = rendering.Viewer(screen_width, screen_height) self.cases = [] for i in range(self.height): self.cases.append([]) for j in range(self.width): x0 = i * scale y0 = j * scale x1 = (i + 1) * scale y1 = (j + 1) * scale case = rendering.FilledPolygon([(y0, x0), (y0, x1), (y1, x1), (y1, x0)]) self.cases[i].append(case) self.viewer.add_geom(case) circle = rendering.make_circle(radius=self.roomba.radius * scale) circle.set_color(1, 0, 0) self.circle = circle direction_circle = rendering.make_circle( radius=self.roomba.radius * scale / 2) #color? direction_circle.add_attr( rendering.Transform(translation=(self.roomba.radius * scale / 2, 0))) tracker = rendering.Compound([circle, direction_circle]) self.tracker_trans = rendering.Transform() tracker.add_attr(self.tracker_trans) self.viewer.add_geom(tracker) for i in range(self.height): for j in range(self.width): if self.room[i, j] == 0: self.cases[i][j].set_color(1, 1, 1) elif self.room[i, j] == 1: self.cases[i][j].set_color( 1 - 0.5 * min(1, self.dirty[i, j]), 1 - 0.5 * min(1, self.dirty[i, j]), 1 - 0.5 * min(1, self.dirty[i, j])) elif self.room[i, j] == 2: self.cases[i][j].set_color(0, 0, 0) elif self.room[i, j] == 3: self.cases[i][j].set_color(0.5, 0.5, 0) self.tracker_trans.set_translation(self.roomba.pos[1] * scale, self.roomba.pos[0] * scale) self.tracker_trans.set_rotation(self.roomba.direction) self.circle.set_color(self.roomba.get_life(), 0, 0) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def make_zone(zone, color): l, r, t, b = zone zone = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) zone.set_color(color[0], color[1], color[2]) return zone
def make_obstacle(obstacle): l, r, t, b = obstacle.l, obstacle.r, obstacle.t, obstacle.b obstacle = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) return obstacle
def render(self, mode='human'): screen_width = 800 screen_height = 500 world_width = self.width scale = screen_width / world_width # Function to make box obstacles def make_obstacle(obstacle): l, r, t, b = obstacle.l, obstacle.r, obstacle.t, obstacle.b obstacle = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) return obstacle def make_zone(zone, color): l, r, t, b = zone zone = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) zone.set_color(color[0], color[1], color[2]) return zone # Creates viewer object, robots, and obstacles if self.viewer is None: self.viewer = rendering.Viewer(screen_width, screen_height) # Add starting zones self.viewer.add_geom(make_zone(self.team1_start, [0, 0, 0.5])) self.viewer.add_geom(make_zone(self.team2_start, [0.5, 0, 0])) # Add bonus zone self.viewer.add_geom(make_zone(self.bonus_bounds, [0.8, 0.8, 0])) # Add robot geometry l, r, t, b = -self.robot.width / 2, self.robot.width / 2, -self.robot.length / 2, self.robot.length / 2 robot = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) robot.set_color(0, 0, 1) self.robot_trans = rendering.Transform() robot.add_attr(self.robot_trans) l, r, t, b = -self.robot.gun_width / 2, self.robot.gun_width / 2, -self.robot.gun_width / 2, self.robot.gun_length - self.robot.gun_width / 2 robot_gun = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.robot_guntrans = rendering.Transform() robot_gun.add_attr(self.robot_guntrans) self.viewer.add_geom(robot) self.viewer.add_geom(robot_gun) # Add enemy geometry l, r, t, b = -self.enemy.width / 2, self.enemy.width / 2, -self.enemy.length / 2, self.enemy.length / 2 enemy = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) enemy.set_color(1, 0, 0) self.enemy_trans = rendering.Transform() enemy.add_attr(self.enemy_trans) l, r, t, b = -self.enemy.gun_width / 2, self.enemy.gun_width / 2, -self.enemy.gun_width / 2, self.enemy.gun_length - self.enemy.gun_width / 2 enemy_gun = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.enemy_guntrans = rendering.Transform() enemy_gun.add_attr(self.enemy_guntrans) self.viewer.add_geom(enemy) self.viewer.add_geom(enemy_gun) # Add obstacles for obstacle in self.obstacles: self.viewer.add_geom(make_obstacle(obstacle)) if self.state is None: return None x = self.state # Render location for robot robotx, roboty = x[0] * scale, x[1] * scale gun_angle = x[2] * np.pi / 180 self.robot_trans.set_translation(robotx, roboty) self.robot_trans.set_rotation((self.robot.angle - 90) * np.pi / 180) self.robot_guntrans.set_translation(robotx, roboty) self.robot_guntrans.set_rotation(gun_angle) # Render location for enemy enemyx, enemyy = x[3] * scale, x[4] * scale enemy_gun_angle = x[5] * np.pi / 180 self.enemy_trans.set_translation(enemyx, enemyy) self.enemy_guntrans.set_translation(enemyx, enemyy) self.enemy_guntrans.set_rotation(enemy_gun_angle) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self): screen_width = MAZE_W * UNIT screen_height = MAZE_H * UNIT offset = np.array([UNIT / 2, UNIT / 2]) if self.viewer is None: import rendering self.viewer = rendering.Viewer(screen_width, screen_height) for c in range(0, MAZE_W * UNIT, UNIT): x0, y0, x1, y1 = c, 0, c, MAZE_H * UNIT self.viewer.add_geom(rendering.Line((x0, y0), (x1, y1))) for r in range(0, MAZE_H * UNIT, UNIT): x0, y0, x1, y1, = 0, r, MAZE_W * UNIT, r self.viewer.add_geom(rendering.Line((x0, y0), (x1, y1))) hell1_center = offset + self.hell1_coord * UNIT hell1 = rendering.FilledPolygon([ (-BLOCK_SIZE / 2, -BLOCK_SIZE / 2), (-BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, -BLOCK_SIZE / 2) ]) hell1trans = rendering.Transform() hell1.add_attr(hell1trans) hell1trans.set_translation(hell1_center[0], MAZE_H * UNIT - hell1_center[1]) self.viewer.add_geom(hell1) # hell2_center = offset + self.hell2_coord * UNIT # hell2 = rendering.FilledPolygon([(-BLOCK_SIZE/2, -BLOCK_SIZE/2), (-BLOCK_SIZE/2, BLOCK_SIZE/2), (BLOCK_SIZE/2, BLOCK_SIZE/2), (BLOCK_SIZE/2, -BLOCK_SIZE/2)]) # hell2trans = rendering.Transform() # hell2.add_attr( hell2trans ) # hell2trans.set_translation( hell2_center[0], MAZE_H * UNIT - hell2_center[1] ) # self.viewer.add_geom( hell2 ) goal_center = offset + self.goal_coord * UNIT goal = rendering.FilledPolygon([(-BLOCK_SIZE / 2, -BLOCK_SIZE / 2), (-BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, -BLOCK_SIZE / 2)]) goal.set_color(1., 1., 0.) goaltrans = rendering.Transform() goal.add_attr(goaltrans) goaltrans.set_translation(goal_center[0], MAZE_H * UNIT - goal_center[1]) self.viewer.add_geom(goal) agent = rendering.FilledPolygon([ (-BLOCK_SIZE / 2, -BLOCK_SIZE / 2), (-BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, BLOCK_SIZE / 2), (BLOCK_SIZE / 2, -BLOCK_SIZE / 2) ]) agent.set_color(1., 0., 0.) self.agenttrans = rendering.Transform() agent.add_attr(self.agenttrans) self.viewer.add_geom(agent) agent_center = offset + self.agent_coord * UNIT self.agenttrans.set_translation(agent_center[0], MAZE_H * UNIT - agent_center[1]) return self.viewer.render(return_rgb_array=False)