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')
viewer = rendering.Viewer(ScrW, ScrH) viewer.set_bgcolor(0, .5, .9) ck1 = Clock(60, 100, rad=60) ck2 = Clock(180, 100, rad=60) ck3 = Clock(300, 100, rad=60) ck4 = Clock(420, 100, rad=60) ck5 = Clock(540, 100, rad=60) viewer.add_geom(ck1.shape) viewer.add_geom(ck2.shape) viewer.add_geom(ck3.shape) viewer.add_geom(ck4.shape) viewer.add_geom(ck5.shape) l1 = rendering.Line(ck1.start, ck1.end) l2 = rendering.Line(ck2.start, ck2.end) l3 = rendering.Line(ck3.start, ck3.end) l4 = rendering.Line(ck4.start, ck4.end) l5 = rendering.Line(ck5.start, ck5.end) viewer.add_geom(l1) viewer.add_geom(l2) viewer.add_geom(l3) viewer.add_geom(l4) viewer.add_geom(l5) for i in range(10000): i = int(i / 10) r = 60 x, y = l1.start l1.end = (x + r * math.cos(math.pi * 2 * i / 8 * 0),
def __init__(self, start, end): viewer.add_geom(rendering.Line(start, end))
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)