def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return state = self.simulation._state if mode == 'ansi': print( 'theta_P: {:3f}, vtheta_P: {:3f}, 1theta_1: {:3f}, vtheta_1: {:3f}, theta_2: {:3f}, vtheta_2: {:3f}' .format(state[0], state[1], state[2], state[3], state[4], state[5])) return (M_P, L_P, L_1, L_2, b, g) = self.params if self.viewer is None: from gym.envs.classic_control import rendering # creating the screen screen_width = int(2 * self.scaling_factor * (L_P + L_1 + L_2) + 100) screen_height = int(self.scaling_factor * (L_P + L_1 + L_2) + 100) self.viewer = rendering.Viewer(screen_width, screen_height) s1w = 8 # segment 1 width s2w = 8 # segment 2 width pw = 7 # pendulum width # creating the movable segments and pendulum segment_1 = rendering.FilledPolygon([ (-s1w, s1w), (s1w, s1w), (s1w, -self.scaling_factor * L_1 - s1w), (-s1w, -self.scaling_factor * L_1 - s1w) ]) segment_2 = rendering.FilledPolygon([ (-s2w, s2w), (s2w, s2w), (s2w, -self.scaling_factor * L_2 - s2w), (-s2w, -self.scaling_factor * L_2 - s2w) ]) pendulum = rendering.FilledPolygon([ (-7, 7), (7, 7), (7, -self.scaling_factor * L_P - 7), (-7, -self.scaling_factor * L_P - 7) ]) # setting different colors segment_1.set_color(.8, .6, .4) segment_2.set_color(.2, .6, .4) pendulum.set_color(.8, .3, .8) # creating visible joints joint_1 = rendering.make_circle(s1w / 2) joint_2 = rendering.make_circle(s2w / 2) joint_p = rendering.make_circle(pw / 2) # setting all colors to black joint_1.set_color(.0, .0, .0) joint_2.set_color(.0, .0, .0) joint_p.set_color(.0, .0, .0) # defining initial transforms (everything upright for now, rotation might change depending on initial angle) self.segment_1_trans = rendering.Transform(translation=(300, 50)) self.segment_2_trans = rendering.Transform( translation=(300, 50 + self.scaling_factor * L_1)) self.pendulum_trans = rendering.Transform( translation=(300, 50 + self.scaling_factor * L_1 + self.scaling_factor * L_2)) # adding transforms to the created shapes segment_1.add_attr(self.segment_1_trans) segment_2.add_attr(self.segment_2_trans) pendulum.add_attr(self.pendulum_trans) # adding the same transforms to the joints joint_1.add_attr(self.segment_1_trans) joint_2.add_attr(self.segment_2_trans) joint_p.add_attr(self.pendulum_trans) # adding a line (maybe rectangle later) to serve as reference ("table") base = rendering.FilledPolygon([(0, 0), (0, 50), (screen_width, 50), (screen_width, 0)]) base.set_color(0.4, 0.4, 0.4) # add shapes to the display self.viewer.add_geom(base) self.viewer.add_geom(segment_1) self.viewer.add_geom(joint_1) self.viewer.add_geom(segment_2) self.viewer.add_geom(joint_2) self.viewer.add_geom(pendulum) self.viewer.add_geom(joint_p) # updating rotations and translations self.segment_1_trans.set_rotation(state[2]) x_trans = 300 + self.scaling_factor * L_1 * sin(state[2]) y_trans = 50 - self.scaling_factor * L_1 * cos(state[2]) self.segment_2_trans.set_translation(x_trans, y_trans) self.segment_2_trans.set_rotation(state[4] + state[2] - pi) pend_x_trans = x_trans + self.scaling_factor * L_2 * sin(state[4] + state[2] - pi) pend_y_trans = y_trans - self.scaling_factor * L_2 * cos(state[4] + state[2] - pi) self.pendulum_trans.set_translation(pend_x_trans, pend_y_trans) self.pendulum_trans.set_rotation(state[0]) return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))
def render(self, mode='human'): assert mode in ['human', 'state_pixels', 'rgb_array'] if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H) self.score_label = pyglet.text.Label('0000', font_size=36, x=20, y=WINDOW_H * 2.5 / 40.00, anchor_x='left', anchor_y='center', color=(255, 255, 255, 255)) self.transform = rendering.Transform() if "t" not in self.__dict__: return # reset() not called yet #zoom = 0.1*SCALE*max(1-self.t, 0) + ZOOM*SCALE*min(self.t, 1) # Animate zoom first second zoom = 0.1 * SCALE * 0 + ZOOM * SCALE * 1 # Animate zoom first second scroll_x = self.car.hull.position[0] scroll_y = self.car.hull.position[1] angle = -self.car.hull.angle vel = self.car.hull.linearVelocity if np.linalg.norm(vel) > 0.5: angle = math.atan2(vel[0], vel[1]) self.transform.set_scale(zoom, zoom) self.transform.set_translation( WINDOW_W / 2 - (scroll_x * zoom * math.cos(angle) - scroll_y * zoom * math.sin(angle)), WINDOW_H / 4 - (scroll_x * zoom * math.sin(angle) + scroll_y * zoom * math.cos(angle))) self.transform.set_rotation(angle) self.car.draw(self.viewer, mode != "state_pixels") arr = None win = self.viewer.window win.switch_to() win.dispatch_events() win.clear() t = self.transform if mode == 'rgb_array': VP_W = VIDEO_W VP_H = VIDEO_H elif mode == 'state_pixels': VP_W = STATE_W VP_H = STATE_H else: pixel_scale = 1 if hasattr(win.context, '_nscontext'): pixel_scale = win.context._nscontext.view().backingScaleFactor( ) # pylint: disable=protected-access VP_W = int(pixel_scale * WINDOW_W) VP_H = int(pixel_scale * WINDOW_H) gl.glViewport(0, 0, VP_W, VP_H) t.enable() self.render_road() for geom in self.viewer.onetime_geoms: geom.render() self.viewer.onetime_geoms = [] t.disable() self.render_indicators(WINDOW_W, WINDOW_H) if mode == 'human': win.flip() return self.viewer.isopen image_data = pyglet.image.get_buffer_manager().get_color_buffer( ).get_image_data() format = 'RGBA' pitch = image_data.width * len(format) arr = np.fromstring(image_data.get_data(format, pitch), dtype=np.uint8, sep='') arr = arr.reshape(VP_H, VP_W, 4) arr = arr[::-1, :, 0:3] return arr
def render(self, mode='human'): screen_width = self.screen_width screen_height = self.screen_height #world_width = self.max_position - self.min_position #scale = screen_width/world_width carwidth = 20 carheight = 80 if self.viewer is None: from gym.envs.classic_control 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 #self.planet_radius+carheight, self.planet_radius car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.car_transform = rendering.Transform(translation=(0, 0)) car.add_attr(self.car_transform) self.cartrans = rendering.Transform() car.add_attr(self.cartrans) self.viewer.add_geom(car) frontwheel = rendering.FilledPolygon([ (-carwidth / 4, 0), (-carwidth / 2, carheight / 2), (-carwidth, -carheight / 2), (-(3 * carwidth) / 4, -carheight) ]) 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.FilledPolygon([ (-carwidth / 4, carheight), (-carwidth / 2, carheight / 2), (-carwidth, (3 * carheight) / 2), (-(3 * carwidth) / 4, (2 * carheight)) ]) 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) #Planets rendering #self.planetsTrans = rendering.Transform() origin_planet = rendering.make_circle(self.planet_radius) origin_planet.set_color(.7, .7, .3) #origin_planet.add_attr(self.planetsTrans) self.origin_planet_transform = rendering.Transform( translation=(self.origin_planet_center[0], self.origin_planet_center[1] + self.planet_radius)) origin_planet.add_attr(self.origin_planet_transform) self.viewer.add_geom(origin_planet) target_planet = rendering.make_circle(self.planet_radius) target_planet.set_color(.6, .2, .1) #target_planet.add_attr(self.planetsTrans) self.target_planet_transform = rendering.Transform( translation=(self.target_planet_center[0], self.target_planet_center[1] + self.planet_radius)) target_planet.add_attr(self.target_planet_transform) self.viewer.add_geom(target_planet) pos = self.state[0] + self.target_planet_center[0] posY = self.state[1] + self.target_planet_center[1] rot = self.state[2] self.cartrans.set_translation(pos, posY) #self._height(pos) self.cartrans.set_rotation(rot) # math.cos(3 * pos) self.target_planet_transform.set_translation( self.target_planet_center[0], self.target_planet_center[1]) self.origin_planet_transform.set_translation( self.origin_planet_center[0], self.origin_planet_center[1]) ##Planets #self.planetsTrans.set_translation(self.min_position * scale, self.min_y_position*scale) #self.planetsTrans.set_rotation(0) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(self.window_width, self.window_height) self.viewer.set_bounds(0, self.window_width, 0, self.window_height) import os __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) scaleX = self.window_width / self.area_width scaleY = self.window_height / self.area_height scaleZ = self.window_z / self.area_z # draw red aircraft pos, vt, att, pos_hist = self._redAC.get_sta() pos_hist_scale = pos_hist.copy() pos_hist_scale[::, 0] = pos_hist_scale[::, 0] * scaleX pos_hist_scale[::, 1] = pos_hist_scale[::, 1] * scaleY red_ac_img = rendering.Image( os.path.join(__location__, 'images/f16_red.png'), 36, 36) jtransform = rendering.Transform( rotation=-att[2], translation=np.array([pos[1] * scaleY, pos[0] * scaleX])) red_ac_img.add_attr(jtransform) self.viewer.onetime_geoms.append(red_ac_img) self.viewer.draw_polyline(pos_hist_scale[::5, [-2, -3]], ).set_color( 1, 0, 0) transform2 = rendering.Transform( translation=(self.goal_pos[1] * scaleY, self.goal_pos[0] * scaleX)) # Relative offset self.viewer.draw_circle(50 * scaleX).add_attr(transform2) transform2 = rendering.Transform( translation=(self.goal_pos[1] * scaleY, self.goal_pos[0] * scaleX)) # Relative offset self.viewer.draw_circle(300 * scaleX, filled=False).add_attr(transform2) transform3 = rendering.Transform(translation=(pos[1] * scaleY, pos[0] * scaleX)) # red dangerous circle self.viewer.draw_circle(2000 * scaleX, filled=False).add_attr(transform3) l, r, t, b = 0, 10, -pos[2] / self.area_z * self.window_height, 0 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) cart.set_color(1, 0, 0) self.viewer.add_onetime(cart) # velocity bar l, r, t, b = 20, (vt - 61) / 186 * self.window_width, 10, 0 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) cart.set_color(1, 0, 0) self.viewer.add_onetime(cart) # draw blue aircraft pos, vt, att, pos_hist = self._blueAC.get_sta() pos_hist_scale = pos_hist.copy() pos_hist_scale[::, 0] = pos_hist_scale[::, 0] * scaleX pos_hist_scale[::, 1] = pos_hist_scale[::, 1] * scaleY blue_ac_img = rendering.Image( os.path.join(__location__, 'images/f16_blue.png'), 36, 36) jtransform = rendering.Transform( rotation=-att[2], translation=np.array([pos[1] * scaleY, pos[0] * scaleX])) blue_ac_img.add_attr(jtransform) self.viewer.onetime_geoms.append(blue_ac_img) self.viewer.draw_polyline(pos_hist_scale[::5, [-2, -3]]).set_color(0, 0, 1) l, r, t, b = 10, 20, -pos[2] / self.area_z * self.window_height, 0 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) cart.set_color(0, 0, 1) self.viewer.add_onetime(cart) # velocity bar l, r, t, b = 20, (vt - 61) / 186 * self.window_width, 20, 10 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) cart.set_color(0, 0, 1) self.viewer.add_onetime(cart) return self.viewer.render(return_rgb_array=mode == "rgb_array")
def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return scale = 10 screen_width = (self.region[1] - self.region[0]) * scale screen_height = (self.region[3] - self.region[2]) * scale world_width = self.region[1] - self.region[0] scale = screen_width / world_width objwidth = 40 objheight = 40 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) #render obstacles self.render_obsts = [] self.obsts_trans_list = [] for obstacle in self.obstacles: obst_trans = rendering.Transform() obst = rendering.make_circle(obstacle[2] * scale) obst.add_attr(obst_trans) obst.set_color(0, 0, 0) self.viewer.add_geom(obst) self.obsts_trans_list.append(obst_trans) self.render_obsts.append(obst) #render goal self.goal_trans = rendering.Transform() self.goal_ren = rendering.make_circle(self.goal[2] * scale) self.goal_ren.add_attr(self.goal_trans) self.goal_ren.set_color(0, 255, 0) self.viewer.add_geom(self.goal_ren) #print("goal[0]={} goal[1]={}".format(self.goal[0],self.goal[1])) #render arrows fname = path.join(path.dirname(__file__), "assets/arrow.png") self.img_list = [] self.imgtrans_list = [] for id in xrange(self.agent_num): img = rendering.Image(fname, 20., 40.) #print("img={}".format(img)) imgtrans = rendering.Transform() img.add_attr(imgtrans) self.img_list.append(img) self.imgtrans_list.append(imgtrans) #print("self.img_list={}, self.imgtrans_list={}".format(self.img_list,self.imgtrans_list)) self.objtrans = rendering.Transform() l, r, t, b = -objwidth / 2, objwidth / 2, -objheight / 2, objheight / 2 self.obj = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.obj.add_attr(self.objtrans) self.obj.set_color(255, 0, 0) self.viewer.add_geom(self.obj) self.scales = [] self.rotations = [] for id in xrange(self.agent_num): self.scales.append(np.abs(self.action[id * 2])) self.rotations.append(self.action[id * 2 + 1] - math.pi / 2) #anti-clockwise self.arrow_offsets = [[30, 0], [0, -30], [-30, 0], [0, 30]] * scale for id in xrange(self.agent_num): self.viewer.add_onetime(self.img_list[id]) self.imgtrans_list[id].set_translation(self.state[0]*scale+self.arrow_offsets[id][0],\ self.state[2]*scale+self.arrow_offsets[id][1]) #follow object #arrow vis V1 #self.imgtrans_list[id].set_translation(self.state[0]*scale,self.state[2]*scale) #arrow vis V2, follow object #self.imgtrans_list[id].set_translation(100+60*id, 200) #arrow vis V3, fixed position self.imgtrans_list[id].set_rotation(self.rotations[id]) # rotation self.imgtrans_list[id].scale = (self.scales[id], self.scales[id]) for i, obst_trans in enumerate(self.obsts_trans_list): obst_trans.set_translation(self.obstacles[i][0] * scale, self.obstacles[i][1] * scale) self.goal_trans.set_translation(self.goal[0] * scale, self.goal[1] * scale) self.objtrans.set_translation(self.state[0] * scale, self.state[2] * scale) #self.objtrans.set_translation(100, 200) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): screen_width = 600 * 2 screen_height = 400 * 2 world_width = self.x_threshold * 2 scale = screen_width / world_width carty = 300 # TOP OF CART polewidth = 10.0 polelen = scale * (2 * self.length) cartwidth = 50.0 cartheight = 30.0 # position # 0 # 0.350 if self.viewer is None: from gym.envs.classic_control import rendering # self.x_label = pyglet.text.Label('0000', font_size=12, # x=10, y=200, anchor_x='left', anchor_y='center', color=(0,0,255,255)) self.viewer = rendering.Viewer(screen_width, screen_height) self.x_label = pyglet.text.Label('0000', font_size=36, x=20, y=400, anchor_x='left', anchor_y='center', color=(255, 0, 0, 255)) self.angle_label = pyglet.text.Label('0000', font_size=36, x=20, y=450, anchor_x='left', anchor_y='center', color=(0, 255, 0, 255)) self.episode_label = pyglet.text.Label('0000', font_size=36, x=20, y=500, anchor_x='left', anchor_y='center', color=(0, 255, 0, 255)) self.viewer.add_geom(DrawText(self.x_label)) self.viewer.add_geom(DrawText(self.angle_label)) self.viewer.add_geom(DrawText(self.episode_label)) 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, .0, .0) 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) self._pole_geom = pole # Edit the pole polygon vertex pole = self._pole_geom l, r, t, b = -polewidth / 2, polewidth / 2, polelen - polewidth / 2, -polewidth / 2 pole.v = [(l, b), (l, t), (r, t), (r, b)] # x = self.state pendulum_angle, pendulum_velocity, cart_position, cart_velocity, cart_acceleration, limit_A, limit_B = self.status # x, x_dot, theta, theta_dot self.x_label.text = 'x={}'.format(str(cart_position)) self.angle_label.text = 'angle={0:.2f}'.format(self.state[0]) # self.episode_label.text = 'ep={}'.format(str('ep')) cartx = cart_position * scale + screen_width / 2.0 # MIDDLE OF CART self.carttrans.set_translation(cartx, carty) self.poletrans.set_rotation(np.deg2rad(pendulum_angle) + np.pi) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() return 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: from gym.envs.classic_control 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 = 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)) self.viewer.render() if mode == 'rgb_array': return self.viewer.get_array() elif mode is 'human': pass else: return super(MountainCarEnv, self).render(mode=mode)
def render(self, mode='human'): from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H) self.viewer.set_bounds(0, VIEWPORT_W / SCALE, 0, VIEWPORT_H / SCALE) for obj in self.particles: obj.ttl -= 0.15 obj.color1 = (max(0.2, 0.2 + obj.ttl), max(0.2, 0.5 * obj.ttl), max(0.2, 0.5 * obj.ttl)) obj.color2 = (max(0.2, 0.2 + obj.ttl), max(0.2, 0.5 * obj.ttl), max(0.2, 0.5 * obj.ttl)) self._clean_particles(False) for p in self.sky_polys: self.viewer.draw_polygon(p, color=(0, 0, 0)) for obj in self.particles + self.drawlist: for f in obj.fixtures: trans = f.body.transform if type(f.shape) is circleShape: t = rendering.Transform(translation=trans * f.shape.pos) self.viewer.draw_circle(f.shape.radius, 20, color=obj.color1).add_attr(t) self.viewer.draw_circle(f.shape.radius, 20, color=obj.color2, filled=False, linewidth=2).add_attr(t) else: path = [trans * v for v in f.shape.vertices] self.viewer.draw_polygon(path, color=obj.color1) path.append(path[0]) self.viewer.draw_polyline(path, color=obj.color2, linewidth=2) for x in [self.helipad_x1, self.helipad_x2]: flagy1 = self.helipad_y flagy2 = flagy1 + 50 / SCALE self.viewer.draw_polyline([(x, flagy1), (x, flagy2)], color=(1, 1, 1)) self.viewer.draw_polygon([(x, flagy2), (x, flagy2 - 10 / SCALE), (x + 25 / SCALE, flagy2 - 5 / SCALE)], color=(0.8, 0.8, 0)) W = VIEWPORT_W / SCALE H = VIEWPORT_H / SCALE if FUEL_CAPACITY is not None: fuel_y = self.fuel / FUEL_CAPACITY * 100.0 self.viewer.draw_polygon([(10 / SCALE, H / 2), (20 / SCALE, H / 2), (20 / SCALE, H / 2 + fuel_y / SCALE), (10 / SCALE, H / 2 + fuel_y / SCALE)], color=(0.9, 0.5, 0.5)) self.viewer.draw_polyline([(10 / SCALE, H / 2), (20 / SCALE, H / 2), (20 / SCALE, H / 2 + 100 / SCALE), (10 / SCALE, H / 2 + 100 / SCALE)], color=(0.9, 0.5, 0.5)) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): radius = 20 diameter = radius * 2 separation_factor = 1.2 separation = int(diameter * separation_factor) left_padding = radius + separation screen_width = left_padding + (self.chain_length + 2) * separation + radius screen_height = 500 resolution = 300 y_center = screen_height // 2 y_top = y_center + radius * separation_factor y_bottom = y_center - radius * separation_factor if self.viewer is None: self.viewer = rendering.Viewer(screen_width, screen_height) initial_circle = rendering.make_circle(radius=radius, res=resolution) translation = rendering.Transform(translation=(left_padding, y_center)) initial_circle.add_attr(translation) self.viewer.add_geom(initial_circle) self.viewer.add_geom( rendering.Line((left_padding + radius, y_center), (left_padding + separation // 2, y_center))) self.viewer.add_geom( rendering.Line((left_padding + separation // 2, y_bottom), (left_padding + separation // 2, y_top))) self.viewer.add_geom( rendering.Line((left_padding + separation // 2, y_top), (left_padding + separation - radius, y_top))) self.viewer.add_geom( rendering.Line((left_padding + separation // 2, y_bottom), (left_padding + separation - radius, y_bottom))) self.circles = [] self.translations = [] x_offset = left_padding + separation for t in range(self.chain_length - 1): top_circle = rendering.make_circle(radius=radius, res=resolution, filled=False) bottom_circle = rendering.make_circle(radius=radius, res=resolution, filled=False) top_translation = rendering.Transform(translation=(x_offset, y_top)) bottom_translation = rendering.Transform( translation=(x_offset, y_bottom)) top_circle.add_attr(top_translation) bottom_circle.add_attr(bottom_translation) self.viewer.add_geom(top_circle) self.viewer.add_geom(bottom_circle) top_line = rendering.Line( (x_offset + radius, y_top), (x_offset + separation - radius, y_top)) bottom_line = rendering.Line( (x_offset + radius, y_bottom), (x_offset + separation - radius, y_bottom)) self.viewer.add_geom(top_line) self.viewer.add_geom(bottom_line) if self.rewarded_action == 1: self.circles.append(top_circle) self.translations.append(top_translation) else: self.circles.append(bottom_circle) self.translations.append(bottom_translation) x_offset += separation terminal_top_circle = rendering.make_circle(radius=radius, res=resolution) terminal_bottom_circle = rendering.make_circle(radius=radius, res=resolution) terminal_top_translation = rendering.Transform( translation=(x_offset, y_top)) terminal_bottom_translation = rendering.Transform( translation=(x_offset, y_bottom)) terminal_top_circle.add_attr(terminal_top_translation) terminal_bottom_circle.add_attr(terminal_bottom_translation) terminal_top_circle.set_color(52 / 255, 235 / 255, 95 / 255) # green terminal_bottom_circle.set_color(235 / 255, 64 / 255, 52 / 255) # red self.viewer.add_geom(terminal_top_circle) self.viewer.add_geom(terminal_bottom_circle) if self.timestep < self.chain_length: filled_circle = self.viewer.draw_circle(radius=radius, res=resolution) stroke_circle = self.viewer.draw_circle(radius=radius, res=resolution, filled=False) if self.reward == 1: filled_circle.set_color(52 / 255, 235 / 255, 95 / 255) # green elif self.reward == -1: filled_circle.set_color(235 / 255, 64 / 255, 52 / 255) # red filled_circle.add_attr(self.translations[self.timestep - 1]) stroke_circle.add_attr(self.translations[self.timestep - 1]) self.viewer.add_geom(filled_circle) self.viewer.add_geom(stroke_circle) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, trace=True): screen_width = 600 screen_height = 600 offset = 25 arrow_size = 50 width_scale = (screen_width - 2 * offset) / (2 * self.rmax_norm) height_scale = (screen_height - 2 * offset) / (2 * self.rmax_norm) if self.viewer is None: self.viewer = rendering.Viewer(screen_width, screen_height) # 绘制P circle_p = rendering.make_circle(5) self.circle_p_trans = rendering.Transform() circle_p.add_attr(self.circle_p_trans) circle_p.set_color(*self.color_p) self.viewer.add_geom(circle_p) # 绘制E circle_e = rendering.make_circle(5) self.circle_e_trans = rendering.Transform() circle_e.add_attr(self.circle_e_trans) circle_e.set_color(*self.color_e) self.viewer.add_geom(circle_e) # 绘制轨迹 if trace: # P轨迹 line_p = rendering.make_polyline(self.trace_p) line_p.set_color(*self.color_p) self.viewer.add_geom(line_p) # E轨迹 line_e = rendering.make_polyline(self.trace_e) line_e.set_color(*self.color_e) self.viewer.add_geom(line_e) if self.state is None: return None r_p, v_p, theta_p, gamma_p, m_p = self.state_p r_e, v_e, theta_e, gamma_e, m_e = self.state_e x_p = r_p * cos(theta_p) y_p = r_p * sin(theta_p) x_e = r_e * cos(theta_e) y_e = r_e * sin(theta_e) if not self.norm: x_p /= DU y_p /= DU x_e /= DU y_e /= DU circle_p_x = width_scale * x_p + screen_width / 2 circle_e_x = width_scale * x_e + screen_width / 2 circle_p_y = height_scale * y_p + screen_height / 2 circle_e_y = height_scale * y_e + screen_height / 2 self.trace_p.append((circle_p_x, circle_p_y)) self.trace_e.append((circle_e_x, circle_e_y)) self.circle_p_trans.set_translation(circle_p_x, circle_p_y) self.circle_e_trans.set_translation(circle_e_x, circle_e_y) if self.last_action_p is not None: arrow_p = self.last_action_p.arrow(arrow_size) if arrow_p is not None: arrow_p.add_attr( rendering.Transform(translation=(circle_p_x, circle_p_y), rotation=theta_p)) arrow_p.set_color(*self.color_p) self.viewer.add_geom(arrow_p) if self.last_action_e is not None: arrow_e = self.last_action_e.arrow(arrow_size) if arrow_e is not None: arrow_e.add_attr( rendering.Transform(translation=(circle_e_x, circle_e_y), rotation=theta_e)) arrow_e.set_color(*self.color_e) self.viewer.add_geom(arrow_e) return self.viewer.render()
def render(self, mode='human'): from gym.envs.classic_control import rendering screen_width = 600 screen_height = 400 world_width = self.max_position - self.min_position x_scale = screen_width / (world_width + 0.1) y_scale = screen_height * 0.8 carwidth = 40 carheight = 20 if self.viewer is None: self.viewer = rendering.Viewer(screen_width, screen_height) xs = np.linspace(self.min_position - 0.05, self.max_position + 0.05, 100) ys = self._ys(xs) xys = list(zip(self._xs(xs) * x_scale, ys * y_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) for color, pos in zip(GOAL_COLORS, self.goal_position): flagx = self._xs(pos) * x_scale flagy1 = self._ys(pos) * y_scale flagy2 = flagy1 + 50 flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2)) self.viewer.add_onetime(flagpole) sign = -1 if (self.max_position - pos) < 0.5 else 1 flag = rendering.FilledPolygon([(flagx, flagy2), (flagx, flagy2 - 10), (flagx + sign * 25, flagy2 - 5)]) flag.set_color(*color) self.viewer.add_onetime(flag) pos = self.state[0] self.cartrans.set_translation( self._xs(pos) * x_scale, self._ys(pos) * y_scale) # height is (1 + cos(2 * pi * x)) / 2 # so gradient is -pi * sin(2 * pi * x) # take arctan of this to get the angle of the slope self.cartrans.set_rotation(np.arctan(np.sin(2 * np.pi * pos) * -np.pi)) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, car_idx=None, mode='human', pts=None): # If car_idx = None, then all cars should be shown in different windows if car_idx is None: for i in range(NUM_VEHICLES): self.render(i, mode, pts) return # Make the transforms and score labels if needed if "score_labels" not in self.__dict__: self.score_labels = [[] for i in range(NUM_VEHICLES)] if "transforms" not in self.__dict__: self.transforms = [None for i in range(NUM_VEHICLES)] # Construct a viewer for this car with score label and transform object if self.viewers[car_idx] is None: self.viewers[car_idx] = rendering.Viewer(WINDOW_W, WINDOW_H) self.score_labels[car_idx].append(pyglet.text.Label('traffic_light: ?', font_size=12, x=10, y=80, anchor_x='left', anchor_y='center', font_name='Helvetica', color=(255,255,255,255))) self.score_labels[car_idx].append(pyglet.text.Label('lane_localization: ?', font_size=12, x=10, y=60, anchor_x='left', anchor_y='center', font_name='Helvetica', color=(255,255,255,255))) self.score_labels[car_idx].append(pyglet.text.Label('type_intersection: ?', font_size=12, x=10, y=40, anchor_x='left', anchor_y='center', font_name='Helvetica', color=(255,255,255,255))) self.score_labels[car_idx].append(pyglet.text.Label('only_turn: ?', font_size=12, x=10, y=20, anchor_x='left', anchor_y='center', font_name='Helvetica', color=(255,255,255,255))) self.transforms[car_idx] = rendering.Transform() if "t" not in self.__dict__: return # reset() not called yet # Create zoom effect and car following for this specific car # zoom = 0.1*SCALE*max(1-self.t[car_idx], 0) + ZOOM*SCALE*min(self.t[car_idx], 1) # Animate zoom first second zoom = ZOOM*SCALE scroll_x = self.cars[car_idx].hull.position[0] scroll_y = self.cars[car_idx].hull.position[1] angle = -self.cars[car_idx].hull.angle vel = self.cars[car_idx].hull.linearVelocity if np.linalg.norm(vel) > 0.5: angle = math.atan2(vel[0], vel[1]) self.transforms[car_idx].set_scale(zoom, zoom) self.transforms[car_idx].set_translation( WINDOW_W/2 - (scroll_x*zoom*math.cos(angle) - scroll_y*zoom*math.sin(angle)), WINDOW_H/2 - (scroll_x*zoom*math.sin(angle) + scroll_y*zoom*math.cos(angle)) ) self.transforms[car_idx].set_rotation(angle) # Comment out the block above and uncomment below if you want to see whole map # Also do the changes at the beginning of the file # self.transforms[car_idx].set_translation(WINDOW_W/2, WINDOW_H/2) # Iterate through traffic lights # We only want to show the relevant traffic lights # 1. Find out which node in the lattice is relevant # 2. Find out traffic lights in current rectangle (if we have r/l lane classification) # 3. Show just that showed_traffic_light = None count_lights = None shift_pos = None if self.last_pid[car_idx] != -1: relevant_node = self.last_relevant_node[car_idx] classification_val = self.loc[car_idx] == "right" shift_pos = self.off_params shift_pos = ( shift_pos[1]+relevant_node[1]*EDGE_WIDTH, shift_pos[0]+relevant_node[0]*EDGE_WIDTH ) if classification_val: i = -1 if relevant_node in self.which_points: i = self.which_points.index(relevant_node) if i != -1: for light in self.lights[i]: light_polygons = light.get_polygons(shift_pos) # should also step traffic_light_pos = Point(light.shifted_pos(shift_pos)) our_polygon = Polygon(self.road_poly[self.last_pid[car_idx]][0]) # print(light.pos, self.road_poly[self.last_pid[car_idx]][0]) if not our_polygon.contains(traffic_light_pos): continue # Uncomment if you want a circle outside traffic light ## self.viewer.draw_polygon(light_polygons[0], color=TRAFFIC_LIGHT_OUTER_COLOR) # Draw triangles for directions or square box for stop showed_traffic_light = light.state_cycle[light.state_idx] count_lights = len(self.lights[i]) self.viewers[car_idx].draw_polygon(light_polygons[1], color=TRAFFIC_LIGHT_INNER_COLOR) # Get the only possible turn at this junction my_last_rect_pid = self.last_rect_pid[car_idx] only_turn = None if my_last_rect_pid and my_last_rect_pid != -1: rni, rnj = self.last_relevant_node[car_idx] neighbor_info = list(self.lattice[rni, rnj, 1:]) my_dir_last = self.directions[my_last_rect_pid] if np.sum(neighbor_info) == 2: if neighbor_info == [False, False, True, True]: if my_dir_last == 'r': only_turn = "right" elif my_dir_last == 't': only_turn = "left" elif neighbor_info == [True, True, False, False]: if my_dir_last == 'l': only_turn = "right" elif my_dir_last == 'b': only_turn = "left" elif neighbor_info == [False, True, True, False]: if my_dir_last == 'l': only_turn = "left" elif my_dir_last == 't': only_turn = "right" elif neighbor_info == [True, False, False, True]: if my_dir_last == 'r': only_turn = "left" elif my_dir_last == 'b': only_turn = "right" # Lane separators for polygon in self.ls_polygons: self.viewers[car_idx].draw_polygon(polygon, color=LANE_SEP_COLOR) # Draw all cars on the viewer for ci in range(NUM_VEHICLES): self.cars[ci].draw(self.viewers[car_idx], mode!="state_pixels") # Do the actual rendering using pyglet.gl arr = None win = self.viewers[car_idx].window # If play mode, then assign key press and key release events if self.play_mode: if car_idx == self.play_mode_car_idx: win.on_key_press = lambda k, mod: self.key_press(k, mod, car_idx) win.on_key_release = lambda k, mod: self.key_release(k, mod, car_idx) if mode != 'state_pixels': win.switch_to() win.dispatch_events() if mode=="rgb_array" or mode=="state_pixels": win.clear() t = self.transforms[car_idx] if mode=='rgb_array': VP_W = VIDEO_W VP_H = VIDEO_H else: VP_W = STATE_W VP_H = STATE_H gl.glViewport(0, 0, VP_W, VP_H) t.enable() self.render_road() self.show_risk() self.render_additional_points(pts) for geom in self.viewers[car_idx].onetime_geoms: geom.render() t.disable() # self.render_indicators(WINDOW_W, WINDOW_H, car_idx=car_idx) # TODO: find why 2x needed image_data = pyglet.image.get_buffer_manager().get_color_buffer().get_image_data() # image_data.save('tmp%d.png'%car_idx) arr = np.frombuffer(image_data.data, dtype=np.uint8) arr = arr.reshape(VP_H, VP_W, 4) arr = arr[::-1, :, 0:3].astype(np.float64) # arr = rgb2gray(arr) # arr = arr.reshape(arr.shape[0], arr.shape[1], 1) arr /= 255.0 if mode=="rgb_array" and not self.human_render: # agent can call or not call env.render() itself when recording video. win.flip() if mode=='human': self.human_render = True win.clear() t = self.transforms[car_idx] gl.glViewport(0, 0, WINDOW_W, WINDOW_H) t.enable() self.render_road() self.show_risk() self.render_additional_points(pts) for geom in self.viewers[car_idx].onetime_geoms: geom.render() t.disable() # self.render_indicators(WINDOW_W, WINDOW_H, car_idx=car_idx) win.flip() self.viewers[car_idx].onetime_geoms = [] return arr, [showed_traffic_light, count_lights if count_lights else None, only_turn, shift_pos]
def render(self, mode='human'): """ Renders the environment. if_vis_trajectory : whether to vis the trajectory if_vis_visual_aid : whether vis the collision range, fetchable range, and stealable range. """ from gym.envs.classic_control import rendering if not self.if_vis_trajectory: if self.viewer is None: self.viewer = rendering.Viewer(940 + 100, 500 + 100) # feet # coordinates self.viewer.set_bounds(0 - 5, 94 + 5, 0 - 5, 50 + 5) # background img fname = path.join(path.dirname(__file__), "fullcourt.png") img = rendering.Image(fname, 94, 50) imgtrans = rendering.Transform(translation=(47.0, 25.0)) img.add_attr(imgtrans) self.viewer.add_geom(img) # defensive players for _ in range(5): def_player = rendering.make_circle(radius=2.) def_player.set_color(0, 0, 1) def_trans = rendering.Transform() self.def_pl_transforms.append(def_trans) def_player.add_attr(def_trans) self.viewer.add_geom(def_player) if self.if_vis_visual_aid: def_player_screen = rendering.make_circle( radius=self.screen_radius, filled=False) def_player_wingspan = rendering.make_circle( radius=self.wingspan_radius, filled=False) def_player_screen.set_color(0, 0, 0.75) def_player_wingspan.set_color(0.5, 0.5, 0.5) def_player_screen.add_attr(def_trans) def_player_wingspan.add_attr(def_trans) self.viewer.add_geom(def_player_screen) self.viewer.add_geom(def_player_wingspan) # offensive players for _ in range(5): off_player = rendering.make_circle(radius=2.) off_player.set_color(1, 0, 0) off_trans = rendering.Transform() self.off_pl_transforms.append(off_trans) off_player.add_attr(off_trans) self.viewer.add_geom(off_player) if self.if_vis_visual_aid: off_player_screen = rendering.make_circle( radius=self.screen_radius, filled=False) off_player_wingspan = rendering.make_circle( radius=self.wingspan_radius, filled=False) off_stolen_range = rendering.make_circle( radius=self.stolen_radius, filled=False) off_player_screen.set_color(0.75, 0, 0) off_player_wingspan.set_color(0.5, 0.5, 0.5) off_stolen_range.set_color(0, 0, 0.75) off_player_screen.add_attr(off_trans) off_player_wingspan.add_attr(off_trans) off_stolen_range.add_attr(off_trans) self.viewer.add_geom(off_player_screen) self.viewer.add_geom(off_player_wingspan) self.viewer.add_geom(off_stolen_range) # ball ball = rendering.make_circle(radius=1.) ball.set_color(0, 1, 0) ball_trans = rendering.Transform() self.ball_transform = ball_trans ball.add_attr(ball_trans) self.viewer.add_geom(ball) ### set translations ### # defensive players for trans, pos in zip(self.def_pl_transforms, self.states.defense_positions): trans.set_translation(pos[0], pos[1]) # offensive players for trans, pos in zip(self.off_pl_transforms, self.states.offense_positions): trans.set_translation(pos[0], pos[1]) # ball ball_pos = self.states.ball_position self.ball_transform.set_translation(ball_pos[0], ball_pos[1]) # vis the end position if self.states.done: shoot_icon = rendering.make_circle(radius=3.) shoot_icon.set_color(0, 1, 0) shoot_icon_trans = rendering.Transform() shoot_icon_pos = self.states.ball_position shoot_icon_trans.set_translation(shoot_icon_pos[0], shoot_icon_pos[1]) shoot_icon.add_attr(shoot_icon_trans) self.viewer.add_onetime(shoot_icon) if self.if_vis_trajectory: if self.viewer is None: self.viewer = rendering.Viewer(940 + 100, 500 + 100) # feet # coordinates self.viewer.set_bounds(0 - 5, 94 + 5, 0 - 5, 50 + 5) # background img fname = path.join(path.dirname(__file__), "fullcourt.png") img = rendering.Image(fname, 94, 50) imgtrans = rendering.Transform(translation=(47.0, 25.0)) img.add_attr(imgtrans) self.viewer.add_geom(img) # defensive players for i in range(5): def_player = rendering.make_circle(radius=2.) def_player.set_color(0, 0, 1) def_trans = rendering.Transform() pos = self.states.defense_positions[i] def_trans.set_translation(pos[0], pos[1]) def_player.add_attr(def_trans) self.viewer.add_geom(def_player) if self.if_vis_visual_aid: def_player_screen = rendering.make_circle( radius=self.screen_radius, filled=False) def_player_wingspan = rendering.make_circle( radius=self.wingspan_radius, filled=False) def_player_screen.set_color(0, 0, 0.75) def_player_wingspan.set_color(0.5, 0.5, 0.5) def_player_screen.add_attr(def_trans) def_player_wingspan.add_attr(def_trans) self.viewer.add_geom(def_player_screen) self.viewer.add_geom(def_player_wingspan) # offensive players for i in range(5): off_player = rendering.make_circle(radius=2.) off_player.set_color(1, 0, 0) off_trans = rendering.Transform() pos = self.states.offense_positions[i] off_trans.set_translation(pos[0], pos[1]) off_player.add_attr(off_trans) self.viewer.add_geom(off_player) if self.if_vis_visual_aid: off_player_screen = rendering.make_circle( radius=self.screen_radius, filled=False) off_player_wingspan = rendering.make_circle( radius=self.wingspan_radius, filled=False) off_stolen_range = rendering.make_circle( radius=self.stolen_radius, filled=False) off_player_screen.set_color(0.75, 0, 0) off_player_wingspan.set_color(0.5, 0.5, 0.5) off_stolen_range.set_color(0, 0, 0.75) off_player_screen.add_attr(off_trans) off_player_wingspan.add_attr(off_trans) off_stolen_range.add_attr(off_trans) self.viewer.add_geom(off_player_screen) self.viewer.add_geom(off_player_wingspan) self.viewer.add_geom(off_stolen_range) # ball ball = rendering.make_circle(radius=1.) ball.set_color(0, 1, 0) ball_trans = rendering.Transform() ball_pos = self.states.ball_position ball_trans.set_translation(ball_pos[0], ball_pos[1]) ball.add_attr(ball_trans) self.viewer.add_geom(ball) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H) self.viewer.set_bounds(0, W, 0, H) sky = rendering.FilledPolygon(((0, 0), (0, H), (W, H), (W, 0))) self.sky_color = rgb(126, 150, 233) sky.set_color(*self.sky_color) self.sky_color_half_transparent = np.array( (np.array(self.sky_color) + rgb(255, 255, 255))) / 2 self.viewer.add_geom(sky) self.rockettrans = rendering.Transform() engine = rendering.FilledPolygon( ((0, 0), (ENGINE_WIDTH / 2, -ENGINE_HEIGHT), (-ENGINE_WIDTH / 2, -ENGINE_HEIGHT))) self.enginetrans = rendering.Transform() engine.add_attr(self.enginetrans) engine.add_attr(self.rockettrans) engine.set_color(.4, .4, .4) self.viewer.add_geom(engine) self.fire = rendering.FilledPolygon( ((ENGINE_WIDTH * 0.4, 0), (-ENGINE_WIDTH * 0.4, 0), (-ENGINE_WIDTH * 1.2, -ENGINE_HEIGHT * 5), (0, -ENGINE_HEIGHT * 8), (ENGINE_WIDTH * 1.2, -ENGINE_HEIGHT * 5))) self.fire.set_color(*rgb(255, 230, 107)) self.firescale = rendering.Transform(scale=(1, 1)) self.firetrans = rendering.Transform(translation=(0, -ENGINE_HEIGHT)) self.fire.add_attr(self.firescale) self.fire.add_attr(self.firetrans) self.fire.add_attr(self.enginetrans) self.fire.add_attr(self.rockettrans) smoke = rendering.FilledPolygon( ((ROCKET_WIDTH / 2, THRUSTER_HEIGHT * 1), (ROCKET_WIDTH * 3, THRUSTER_HEIGHT * 1.03), (ROCKET_WIDTH * 4, THRUSTER_HEIGHT * 1), (ROCKET_WIDTH * 3, THRUSTER_HEIGHT * 0.97))) smoke.set_color(*self.sky_color_half_transparent) self.smokescale = rendering.Transform(scale=(1, 1)) smoke.add_attr(self.smokescale) smoke.add_attr(self.rockettrans) self.viewer.add_geom(smoke) self.gridfins = [] for i in (-1, 1): finpoly = ((i * ROCKET_WIDTH * 1.1, THRUSTER_HEIGHT * 1.01), (i * ROCKET_WIDTH * 0.4, THRUSTER_HEIGHT * 1.01), (i * ROCKET_WIDTH * 0.4, THRUSTER_HEIGHT * 0.99), (i * ROCKET_WIDTH * 1.1, THRUSTER_HEIGHT * 0.99)) gridfin = rendering.FilledPolygon(finpoly) gridfin.add_attr(self.rockettrans) gridfin.set_color(0.25, 0.25, 0.25) self.gridfins.append(gridfin) if self.stepnumber % round(FPS / 10) == 0 and self.power > 0: s = [ MAX_SMOKE_LIFETIME * self.power, # total lifetime 0, # current lifetime self.power * (1 + 0.2 * np.random.random()), # size np.array(self.lander.position) + self.power * ROCKET_WIDTH * 10 * np.array( (np.sin(self.lander.angle + self.gimbal), -np.cos(self.lander.angle + self.gimbal))) + self.power * 5 * (np.random.random(2) - 0.5) ] # position self.smoke.append(s) for s in self.smoke: s[1] += 1 if s[1] > s[0]: self.smoke.remove(s) continue t = rendering.Transform(translation=(s[3][0], s[3][1] + H * s[1] / 2000)) self.viewer.draw_circle( radius=0.05 * s[1] + s[2], color=self.sky_color + (1 - (2 * s[1] / s[0] - 1)**2) / 3 * (self.sky_color_half_transparent - self.sky_color)).add_attr(t) self.viewer.add_onetime(self.fire) for g in self.gridfins: self.viewer.add_onetime(g) for obj in self.drawlist: for f in obj.fixtures: trans = f.body.transform path = [trans * v for v in f.shape.vertices] self.viewer.draw_polygon(path, color=obj.color1) for l in zip(self.legs, [-1, 1]): path = [ self.lander.fixtures[0].body.transform * (l[1] * ROCKET_WIDTH / 2, ROCKET_HEIGHT / 8), l[0].fixtures[0].body.transform * (l[1] * LEG_LENGTH * 0.8, 0) ] self.viewer.draw_polyline(path, color=self.ship.color1, linewidth=1 if START_HEIGHT > 500 else 2) self.viewer.draw_polyline( ((self.helipad_x2, self.terrainheigth + SHIP_HEIGHT), (self.helipad_x1, self.terrainheigth + SHIP_HEIGHT)), color=rgb(206, 206, 2), linewidth=1) self.rockettrans.set_translation(*self.lander.position) self.rockettrans.set_rotation(self.lander.angle) self.enginetrans.set_rotation(self.gimbal) self.firescale.set_scale(newx=1, newy=self.power * np.random.uniform(1, 1.3)) self.smokescale.set_scale(newx=self.force_dir, newy=1) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def _render(self, mode='human', close=False, values=None): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return size = 6 px_per_cell = 120 size_px = size * px_per_cell from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(size_px, size_px) for x in range(size + 1): for y in range(size + 1): x_line = rendering.Line((px_per_cell * x, 0), (px_per_cell * x, size_px)) y_line = rendering.Line((0, px_per_cell * y), (size_px, px_per_cell * y)) self.viewer.add_geom(x_line) self.viewer.add_geom(y_line) robot_width = px_per_cell*3/4 poly = [(-robot_width/2,-robot_width/2), (-robot_width/2,robot_width/2), (robot_width/2,robot_width/2), (robot_width/2,-robot_width/2)] self.robot = self.viewer.draw_polygon(poly,filled=False) self.viewer.add_geom(self.robot) self.robot_transform = rendering.Transform() self.robot.add_attr(self.robot_transform) self.robot.attrs[0].vec4 = (0,0,1,1) self.robot.attrs[1] = rendering.LineWidth(5) self.opponent = self.viewer.draw_polygon(poly,filled=False) self.viewer.add_geom(self.opponent) self.opponent_transform = rendering.Transform() self.opponent.add_attr(self.opponent_transform) self.opponent.attrs[0].vec4 = (1,0,0,1) self.opponent.attrs[1] = rendering.LineWidth(5) location = getxy(self.state.blue_data[0]) self.robot_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2, size_px - location[1] * px_per_cell - px_per_cell / 2 - px_per_cell/8) location = getxy(self.state.red_data[0]) self.opponent_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2, size_px - location[1] * px_per_cell - px_per_cell / 2 - px_per_cell/8) for i in range(self.red_goals): goal = self.viewer.draw_circle(radius=px_per_cell / 5, filled=True) self.viewer.add_geom(goal) goal_transform = rendering.Transform() goal.add_attr(goal_transform) goal.attrs[0].vec4 = (1,0,0,0.5) location = getxy(self.state.goal_data[i][0]) goal_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2, size_px - location[1] * px_per_cell - px_per_cell / 2) label = pyglet.text.Label(str(self.state.goal_data[i][1]), font_name='Times New Roman', font_size=15, color=(0,0,0,255), x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 2, anchor_x='center', anchor_y='center') self.viewer.add_geom(label) for i in range(self.blue_goals): goal = self.viewer.draw_circle(radius=px_per_cell / 5, filled=True) self.viewer.add_geom(goal) goal_transform = rendering.Transform() goal.add_attr(goal_transform) goal.attrs[0].vec4 = (0,0,1,0.5) location = getxy(self.state.goal_data[i+self.red_goals][0]) goal_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2, size_px - location[1] * px_per_cell - px_per_cell / 2) label = pyglet.text.Label(str(self.state.goal_data[i+self.red_goals][1]), font_name='Times New Roman', font_size=15, color=(0,0,0,255), x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 2, anchor_x='center', anchor_y='center') self.viewer.add_geom(label) for i in range(self.field_locations): if self.state.tile_data[i] >0: location = getxy(i) label = pyglet.text.Label(str(self.state.tile_data[i]), font_name='Times New Roman', font_size=15, color=(0,0,0,255), x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8, anchor_x='center', anchor_y='center') self.viewer.add_geom(label) location = getxy(12) redLoads = pyglet.text.Label(str(self.state.red_data[3]), font_name='Times New Roman', font_size=15, color=(255,0,0,255), x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8, anchor_x='center', anchor_y='center') self.viewer.add_geom(redLoads) location = getxy(2) blueLoads = pyglet.text.Label(str(self.state.blue_data[3]), font_name='Times New Roman', font_size=15, color=(0,0,255,255), x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8, anchor_x='center', anchor_y='center') self.viewer.add_geom(blueLoads) return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))
def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return screen_width = 800 screen_height = 800 scale_width = screen_width / (self.bound_box[0, 1] - self.bound_box[0, 0]) scale_height = screen_height / (self.bound_box[1, 1] - self.bound_box[1, 0]) zero_width = scale_width * (-self.bound_box[0, 0]) zero_height = scale_height * (-self.bound_box[1, 0]) drone_width = 20 drone_height = 20 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) l, r, t, b = -drone_width / 2, drone_width / 2, drone_height / 2, -drone_height / 2 drone = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.drone_trans = rendering.Transform() drone.add_attr(self.drone_trans) self.viewer.add_geom(drone) goal = rendering.make_circle(scale_height * 0.2) goal.set_color(.0, 1.0, .0) self.goal_trans = rendering.Transform() goal.add_attr(self.goal_trans) self.viewer.add_geom(goal) obstacles = [] self.obstacle_trans = [] # add_geom for it_obstacle in range(self.obstacle_num): if self.obstacle_type[it_obstacle] == 'cylinder': radius = scale_height * self.obstacle_dim[it_obstacle] obstacles.append(rendering.make_circle(radius)) obstacles[it_obstacle].set_color(1.0, .0, .0) self.obstacle_trans.append(rendering.Transform()) obstacles[it_obstacle].add_attr( self.obstacle_trans[it_obstacle]) self.viewer.add_geom(obstacles[it_obstacle]) elif self.obstacle_type[it_obstacle] == 'rectangle': obst_dim = self.obstacle_dim[it_obstacle] rect_width = obst_dim[0] * scale_width rect_height = obst_dim[1] * scale_height l, r, t, b = -rect_width / 2, rect_width / 2, rect_height / 2, -rect_height / 2 obstacles.append( rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])) obstacles[it_obstacle].set_color(1.0, .0, .0) self.obstacle_trans.append(rendering.Transform()) obstacles[it_obstacle].add_attr( self.obstacle_trans[it_obstacle]) self.viewer.add_geom(obstacles[it_obstacle]) elif self.obstacle_type[it_obstacle] == 'wall': wall_vector = self.obstacle_dim[ it_obstacle] - self.obstacle_pos[it_obstacle, :2] wall_width = np.linalg.norm(wall_vector) * scale_width wall_height = 5 l, r, t, b = 0, wall_width, 0, wall_height obstacles.append( rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])) obstacles[it_obstacle].set_color(1.0, 0.0, 0.0) self.obstacle_trans.append(rendering.Transform()) obstacles[it_obstacle].add_attr( self.obstacle_trans[it_obstacle]) self.viewer.add_geom(obstacles[it_obstacle]) laser_readings = [] self.laser_readings_array = [] for it_laser in range(self.num_samples_laser): laser_readings.append(rendering.make_circle(3)) laser_readings[it_laser].set_color(0.0, 0.0, 1.0) self.laser_readings_array.append(rendering.Transform()) laser_readings[it_laser].add_attr( self.laser_readings_array[it_laser]) self.viewer.add_geom(laser_readings[it_laser]) if self.state is None: return None drone_x = self.state[0] * scale_width + zero_width drone_y = self.state[1] * scale_height + zero_height self.drone_trans.set_translation(drone_x, drone_y) self.drone_trans.set_rotation(self.state[2]) goal_x = self.goal_pos[0] * scale_width + zero_width goal_y = self.goal_pos[1] * scale_height + zero_height self.goal_trans.set_translation(goal_x, goal_y) # set_translation for it_obstacles in range(self.obstacle_num): if self.obstacle_type[it_obstacles] == 'cylinder': object_it_pos = self.obstacle_pos[it_obstacles] obstacle_x = object_it_pos[0] * scale_width + zero_width obstacle_y = object_it_pos[1] * scale_height + zero_height self.obstacle_trans[it_obstacles].set_translation( obstacle_x, obstacle_y) elif self.obstacle_type[it_obstacles] == 'rectangle': object_it_pos = self.obstacle_pos[it_obstacles] obstacle_x = object_it_pos[0] * scale_width + zero_width obstacle_y = object_it_pos[1] * scale_height + zero_height self.obstacle_trans[it_obstacles].set_translation( obstacle_x, obstacle_y) self.obstacle_trans[it_obstacles].set_rotation( object_it_pos[2]) elif self.obstacle_type[it_obstacles] == 'wall': wall_pose = self.obstacle_pos[it_obstacles] wall_x = wall_pose[0] * scale_width + zero_width wall_y = wall_pose[1] * scale_height + zero_height self.obstacle_trans[it_obstacles].set_translation( wall_x, wall_y) wall_vector = self.obstacle_dim[it_obstacles] - wall_pose[:2] rotation_angle = np.arctan2(wall_vector[1], wall_vector[0]) self.obstacle_trans[it_obstacles].set_rotation(rotation_angle) if True: rays = np.linspace(-np.pi, np.pi, self.num_samples_laser) for it_laser in range(self.num_samples_laser): laser_reading_it = self.laser_obs[it_laser] if laser_reading_it > self.max_measurement_laser: laser_reading_it = self.max_measurement_laser laser_intersect = self.state[:2] + laser_reading_it *\ np.array([np.cos(self.state[2] + rays[it_laser]), np.sin(self.state[2] + rays[it_laser])]) laser_x = laser_intersect[0] * scale_width + zero_width laser_y = laser_intersect[1] * scale_height + zero_height self.laser_readings_array[it_laser].set_translation( laser_x, laser_y) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human', close=False): """ Graphic UI by openAI gym """ if close: if self.viewer is not None: self.viewer.close() self.viewer = None return zero = (0, 0) u_size = self.u_size m = 2 # gaps between two cells # 如果还没有设定屏幕对象,则初始化整个屏幕具备的元素。 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(self.width, self.height) # 在Viewer里绘制一个几何图像的步骤如下: # the following steps just tells how to render an shape in the environment. # 1. 建立该对象需要的数据本身 # 2. 使用rendering提供的方法返回一个geom对象 # 3. 对geom对象进行一些对象颜色、线宽、线型、变换属性的设置(有些对象提供一些个 # 性化的方法来设置属性,具体请参考继承自这些Geom的对象),这其中有一个重要的 # 属性就是变换属性, # 该属性负责对对象在屏幕中的位置、渲染、缩放进行渲染。如果某对象 # 在呈现时可能发生上述变化,则应建立关于该对象的变换属性。该属性是一个 # Transform对象,而一个Transform对象,包括translate、rotate和scale # 三个属性,每个属性都由以np.array对象描述的矩阵决定。 # 4. 将新建立的geom对象添加至viewer的绘制对象列表里,如果在屏幕上只出现一次, # 将其加入到add_onegeom()列表中,如果需要多次渲染,则将其加入add_geom() # 5. 在渲染整个viewer之前,对有需要的geom的参数进行修改,修改主要基于该对象 # 的Transform对象 # 6. 调用Viewer的render()方法进行绘制 ''' 绘制水平竖直格子线,由于设置了格子之间的间隙,可不用此段代码 for i in range(self.n_width+1): line = rendering.Line(start = (i*u_size, 0), end =(i*u_size, u_size*self.n_height)) line.set_color(0.5,0,0) self.viewer.add_geom(line) for i in range(self.n_height): line = rendering.Line(start = (0, i*u_size), end = (u_size*self.n_width, i*u_size)) line.set_color(0,0,1) self.viewer.add_geom(line) ''' # 绘制格子, draw cells for x in range(self.n_width): for y in range(self.n_height): v = [(x * u_size + m, y * u_size + m), ((x + 1) * u_size - m, y * u_size + m), ((x + 1) * u_size - m, (y + 1) * u_size - m), (x * u_size + m, (y + 1) * u_size - m)] rect = rendering.FilledPolygon(v) r = self.grids.get_reward(x, y) / 10 if r < 0: rect.set_color(0.9 - r, 0.9 + r, 0.9 + r) elif r > 0: rect.set_color(0.3, 0.5 + r, 0.3) else: rect.set_color(0.9, 0.9, 0.9) self.viewer.add_geom(rect) # 绘制边框, draw frameworks v_outline = [(x * u_size + m, y * u_size + m), ((x + 1) * u_size - m, y * u_size + m), ((x + 1) * u_size - m, (y + 1) * u_size - m), (x * u_size + m, (y + 1) * u_size - m)] outline = rendering.make_polygon(v_outline, False) outline.set_linewidth(3) # the goal if self._is_end_state(x, y): # give end state cell a red . g = [(x * 40 + 12, y * 40 + 12), ((x + 1) * 40 - 12, y * 40 + 12), ((x + 1) * 40 - 12, (y + 1) * 40 - 12), (x * 40 + 12, (y + 1) * 40 - 12)] goal_rect = rendering.FilledPolygon(g) goal_rect.set_color(1, 0, 0) outline.set_color(1, 0, 0) self.viewer.add_geom(goal_rect) self.viewer.add_geom(outline) # agent start green colour for start in self.starts: if start[0] == x and start[1] == y: outline.set_color(0.0, 1.0, 0.0) self.viewer.add_geom(outline) # obstacle cells are with gray color if self.grids.get_type(x, y) == 0: rect.set_color(0.3, 0.3, 0.3) else: pass # draw agent self.agent = rendering.make_circle(u_size / 4, 30, True) self.agent.set_color(0.0, 1.0, 0.0) self.viewer.add_geom(self.agent) self.agent_trans = rendering.Transform() self.agent.add_attr(self.agent_trans) # update position of an agent x, y = self.agent_states self.agent_trans.set_translation( (x + 0.5) * u_size, (y + 0.5) * u_size) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def __init__(self): self.viewer = rendering.Viewer(600, 400) # 600x400 是画板的长和框
def _render(self, mode='human', close=False): #未完全看懂 if close: #未懂close的作用 if self.viewer is not None: self.viewer.close() self.viewer = None return zero = (0, 0) u_size = self.u_size m = 2 # 格子之间的间隙尺寸 # 如果还没有设定屏幕对象,则初始化整个屏幕具备的元素。 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(self.width, self.height) # 在Viewer里绘制一个几何图像的步骤如下: # 1. 建立该对象需要的数据本身 # 2. 使用rendering提供的方法返回一个geom对象 # 3. 对geom对象进行一些对象颜色、线宽、线型、变换属性的设置(有些对象提供一些个 # 性化的方法来设置属性,具体请参考继承自这些Geom的对象),这其中有一个重要的 # 属性就是变换属性, # 该属性负责对对象在屏幕中的位置、渲染、缩放进行渲染。如果某对象 # 在呈现时可能发生上述变化,则应建立关于该对象的变换属性。该属性是一个 # Transform对象,而一个Transform对象,包括translate、rotate和scale # 三个属性,每个属性都由以np.array对象描述的矩阵决定。 # 4. 将新建立的geom对象添加至viewer的绘制对象列表里,如果在屏幕上只出现一次, # 将其加入到add_onegeom()列表中,如果需要多次渲染,则将其加入add_geom() # 5. 在渲染整个viewer之前,对有需要的geom的参数进行修改,修改主要基于该对象 # 的Transform对象 # 6. 调用Viewer的render()方法进行绘制 '''#绘制水平竖直格子线,由于设置了格子之间的间隙,可不用此段代码 for i in range(self.n_width+1): line = rendering.Line(start = (i*u_size, 0), end =(i*u_size, u_size*self.n_height)) line.set_color(1,0,0) self.viewer.add_geom(line) for i in range(self.n_height): line = rendering.Line(start = (0, i*u_size), end = (u_size*self.n_width, i*u_size)) line.set_color(0,0,1) self.viewer.add_geom(line) ''' # 绘制格子 for x in range(self.n_width): for y in range(self.n_height): v = [(x * u_size + m, y * u_size + m), ((x + 1) * u_size - m, y * u_size + m), ((x + 1) * u_size - m, (y + 1) * u_size - m), (x * u_size + m, (y + 1) * u_size - m)] rect = rendering.FilledPolygon(v) r = self.grids.get_reward(x, y) / 10 if r < 0: rect.set_color(0.9 - r, 0.9 + r, 0.9 + r) elif r > 0: rect.set_color(0.3, 0.5 + r, 0.3) else: rect.set_color(0.9, 0.9, 0.9) self.viewer.add_geom(rect) # 绘制边框 v_outline = [(x * u_size + m, y * u_size + m), ((x + 1) * u_size - m, y * u_size + m), ((x + 1) * u_size - m, (y + 1) * u_size - m), (x * u_size + m, (y + 1) * u_size - m)] outline = rendering.make_polygon(v_outline, False) outline.set_linewidth(3) if self._is_end_state(x, y): # 给终点方格添加金黄色边框 outline.set_color(0.9, 0.9, 0) self.viewer.add_geom(outline) if self.start[0] == x and self.start[1] == y: outline.set_color(0.5, 0.5, 0.8) self.viewer.add_geom(outline) if self.grids.get_type(x, y) == 1: # 障碍格子用深灰色表示 rect.set_color(0.3, 0.3, 0.3) else: pass # 绘制个体 self.agent = rendering.make_circle(u_size / 4, 30, True) self.agent.set_color(1.0, 1.0, 0.0) self.viewer.add_geom(self.agent) self.agent_trans = rendering.Transform() self.agent.add_attr(self.agent_trans) # 更新个体位置 x, y = self._state_to_xy(self.state) self.agent_trans.set_translation((x + 0.5) * u_size, (y + 0.5) * u_size) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): scale = self.SCALE carty = 100 # TOP OF CART polewidth = 10.0 cartwidth = 50.0 cartheight = 30.0 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(self.SCREEN_W, self.SCREEN_H) body = rendering.FilledPolygon(self.LanderPoly) body.set_color(0.5,0.4,0.9) main_engine_flare = rendering.FilledPolygon(self.MainEnginePoly) main_engine_flare.set_color(0.9, 0.9, 0.1) left_engine_flare = rendering.FilledPolygon(self.LeftEnginePoly) left_engine_flare.set_color(0.9, 0.9, 0.1) right_engine_flare = rendering.FilledPolygon(self.RightEnginePoly) right_engine_flare.set_color(0.9, 0.9, 0.1) self.Body = body self.MainEngine = main_engine_flare self.RightEngine = right_engine_flare self.LeftEngine = left_engine_flare sky = rendering.FilledPolygon([ (0.0, 0.0), (self.SCREEN_W, 0.0), (self.SCREEN_W, self.SCREEN_H), (0.0, self.SCREEN_H) ]) sky.set_color(0,0,0) self.viewer.add_geom(sky) base = rendering.FilledPolygon([ (0.0, 0.0), (self.SCREEN_W, 0.0), (self.SCREEN_W, self.ZERO_Y), (0.0, self.ZERO_Y) ]) base.set_color(0.9, 0.9, 0.9) self.viewer.add_geom(base) self.LanderTransform = rendering.Transform() for g in (self.Body, self.MainEngine, self.RightEngine, self.LeftEngine): g.add_attr(self.LanderTransform) #if self.crash: # self.Body.set_color(1.0, 0.4, 0.1) self.viewer.add_geom(body) state = self.state if state is None: return None x, y, _, _, phi, _ = state x0, y0 = self.SCREEN_W/2 + x*self.SCALE, self.ZERO_Y + y*self.SCALE self.LanderTransform.set_translation(x0, y0) self.LanderTransform.set_rotation(phi) if self.action == 1: self.viewer.add_onetime(self.MainEngine) elif self.action == 2: self.viewer.add_onetime(self.LeftEngine) elif self.action == 3: self.viewer.add_onetime(self.RightEngine) return self.viewer.render(return_rgb_array = mode=='rgb_array')
def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H) self.viewer.set_bounds(0, VIEWPORT_W / SCALE, 0, VIEWPORT_H / SCALE) for obj in self.particles: obj.ttl -= 0.15 obj.color1 = (max(0.2, 0.2 + obj.ttl), max(0.2, 0.5 * obj.ttl), max(0.2, 0.5 * obj.ttl)) obj.color2 = (max(0.2, 0.2 + obj.ttl), max(0.2, 0.5 * obj.ttl), max(0.2, 0.5 * obj.ttl)) self._clean_particles(False) for p in self.sky_polys: self.viewer.draw_polygon(p, color=(0, 0, 0)) for obj in self.particles + self.drawlist: for f in obj.fixtures: trans = f.body.transform if type(f.shape) is circleShape: t = rendering.Transform(translation=trans * f.shape.pos) self.viewer.draw_circle(f.shape.radius, 20, color=obj.color1).add_attr(t) self.viewer.draw_circle(f.shape.radius, 20, color=obj.color2, filled=False, linewidth=2).add_attr(t) else: path = [trans * v for v in f.shape.vertices] self.viewer.draw_polygon(path, color=obj.color1) path.append(path[0]) self.viewer.draw_polyline(path, color=obj.color2, linewidth=2) for x in [self.helipad_x1, self.helipad_x2]: flagy1 = self.helipad_y flagy2 = flagy1 + 50 / SCALE self.viewer.draw_polyline([(x, flagy1), (x, flagy2)], color=(1, 1, 1)) self.viewer.draw_polygon([(x, flagy2), (x, flagy2 - 10 / SCALE), (x + 25 / SCALE, flagy2 - 5 / SCALE)], color=(0.8, 0.8, 0)) clock_prog = self.curr_step / MAX_NUM_STEPS self.viewer.draw_polyline( [(0, 0.05 * VIEWPORT_H / SCALE), (clock_prog * VIEWPORT_W / SCALE, 0.05 * VIEWPORT_H / SCALE)], color=(255, 0, 0), linewidth=5) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return screen_width = 480 screen_height = 520 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) #创建网格世界 # for self.line1 = rendering.Line((40, 20), (40, 500)) self.line2 = rendering.Line((80, 20), (80, 500)) self.line3 = rendering.Line((120, 20), (120, 500)) self.line4 = rendering.Line((160, 20), (160, 500)) self.line5 = rendering.Line((200, 20), (200, 500)) self.line6 = rendering.Line((240, 20), (240, 500)) self.line7 = rendering.Line((280, 20), (280, 500)) self.line8 = rendering.Line((320, 20), (320, 500)) self.line9 = rendering.Line((360, 20), (360, 500)) self.line10 = rendering.Line((400, 20), (400, 500)) self.line11 = rendering.Line((440, 20), (440, 500)) self.line12 = rendering.Line((40, 20), (440, 20)) self.line13 = rendering.Line((40, 60), (440, 60)) self.line14 = rendering.Line((40, 100), (440, 100)) self.line15 = rendering.Line((40, 140), (440, 140)) self.line16 = rendering.Line((40, 180), (440, 180)) self.line17 = rendering.Line((40, 220), (440, 220)) self.line18 = rendering.Line((40, 260), (440, 260)) self.line19 = rendering.Line((40, 300), (440, 300)) self.line20 = rendering.Line((40, 340), (440, 340)) self.line21 = rendering.Line((40, 380), (440, 380)) self.line22 = rendering.Line((40, 420), (440, 420)) self.line23 = rendering.Line((40, 460), (440, 460)) self.line24 = rendering.Line((40, 500), (440, 500)) self.robot = rendering.make_circle(15) self.robotrans = rendering.Transform() self.robot.add_attr(self.robotrans) self.robot.set_color(0.8, 0.6, 0.4) self.line1.set_color(0, 0, 0) self.line2.set_color(0, 0, 0) self.line3.set_color(0, 0, 0) self.line4.set_color(0, 0, 0) self.line5.set_color(0, 0, 0) self.line6.set_color(0, 0, 0) self.line7.set_color(0, 0, 0) self.line8.set_color(0, 0, 0) self.line9.set_color(0, 0, 0) self.line10.set_color(0, 0, 0) self.line11.set_color(0, 0, 0) self.line12.set_color(0, 0, 0) self.line13.set_color(0, 0, 0) self.line14.set_color(0, 0, 0) self.line15.set_color(0, 0, 0) self.line16.set_color(0, 0, 0) self.line17.set_color(0, 0, 0) self.line18.set_color(0, 0, 0) self.line19.set_color(0, 0, 0) self.line20.set_color(0, 0, 0) self.line21.set_color(0, 0, 0) self.line22.set_color(0, 0, 0) self.line23.set_color(0, 0, 0) self.line24.set_color(0, 0, 0) self.viewer.add_geom(self.line1) self.viewer.add_geom(self.line2) self.viewer.add_geom(self.line3) self.viewer.add_geom(self.line4) self.viewer.add_geom(self.line5) self.viewer.add_geom(self.line6) self.viewer.add_geom(self.line7) self.viewer.add_geom(self.line8) self.viewer.add_geom(self.line9) self.viewer.add_geom(self.line10) self.viewer.add_geom(self.line11) self.viewer.add_geom(self.line12) self.viewer.add_geom(self.line13) self.viewer.add_geom(self.line14) self.viewer.add_geom(self.line15) self.viewer.add_geom(self.line16) self.viewer.add_geom(self.line17) self.viewer.add_geom(self.line18) self.viewer.add_geom(self.line19) self.viewer.add_geom(self.line20) self.viewer.add_geom(self.line21) self.viewer.add_geom(self.line22) self.viewer.add_geom(self.line23) self.viewer.add_geom(self.line24) self.viewer.add_geom(self.robot) if self.state is None: return None self.robotrans.set_translation(self.x[self.state - 1], self.y[self.state - 1]) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): if self.viewer is None: # 注意绘制坐标系原点在左下角 from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(self.screen_width, self.screen_height) lines = [] for i in range(4): lines.append( rendering.Line((self.offset_width + i * self.unit_width, self.offset_height), (self.offset_width + i * self.unit_width, self.screen_height - self.offset_height))) lines[-1].set_color(0, 0, 0) lines.append( rendering.Line( (self.offset_width, self.offset_height + i * self.unit_height), (self.screen_width - self.offset_width, self.offset_height + i * self.unit_height))) lines[-1].set_color(0, 0, 0) traps = [] traps.append(rendering.make_circle(50)) traps[-1].add_attr( rendering.Transform( translation=(1.5 * self.unit_width + self.offset_width, 2.5 * self.unit_height + self.offset_height))) traps[-1].set_color(1, 0, 0) traps.append(rendering.make_circle(50)) traps[-1].add_attr( rendering.Transform( translation=(2.5 * self.unit_width + self.offset_width, 1.5 * self.unit_height + self.offset_height))) traps[-1].set_color(1, 0, 0) gold = rendering.make_circle(50) gold.add_attr( rendering.Transform( translation=(2.5 * self.unit_width + self.offset_width, 2.5 * self.unit_height + self.offset_height))) gold.set_color(1, 1, 0) for v in lines: self.viewer.add_geom(v) for v in traps: self.viewer.add_geom(v) self.viewer.add_geom(gold) agent_view = rendering.make_circle(50) ix, iy = self._get_loc() self.agent_view_trans = rendering.Transform() agent_view.add_attr(self.agent_view_trans) agent_view.set_color(0, 1, 0) self.viewer.add_geom(agent_view) print("step = %d, current state = %d" % (self.times, self.s)) ix, iy = self._get_loc() self.agent_view_trans.set_translation( newx=(0.5 + ix) * self.unit_width + self.offset_width, newy=(0.5 + iy) * self.unit_height + self.offset_height) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H) self.score_label = pyglet.text.Label('0000', font_size=36, x=20, y=WINDOW_H*2.5/40.00, anchor_x='left', anchor_y='center', color=(255,255,255,255)) self.transform = rendering.Transform() if "t" not in self.__dict__: return # reset() not called yet zoom = ZOOM*SCALE zoom_state = ZOOM*SCALE*STATE_W/WINDOW_W zoom_video = ZOOM*SCALE*VIDEO_W/WINDOW_W scroll_x = self.car.hull.position[0] scroll_y = self.car.hull.position[1] angle = -self.car.hull.angle vel = self.car.hull.linearVelocity if np.linalg.norm(vel) > 0.5: angle = math.atan2(vel[0], vel[1]) self.transform.set_scale(zoom, zoom) self.transform.set_translation( WINDOW_W/2 - (scroll_x*zoom*math.cos(angle) - scroll_y*zoom*math.sin(angle)), WINDOW_H/4 - (scroll_x*zoom*math.sin(angle) + scroll_y*zoom*math.cos(angle)) ) self.transform.set_rotation(angle) self.car.draw(self.viewer, mode!="state_pixels") arr = None win = self.viewer.window if mode != 'state_pixels': win.switch_to() win.dispatch_events() #KOE: Added if to try to fix reendering problem #if "dispatch_events_called" not in self.__dict__ and mode == 'state_pixels': # win.dispatch_events() # self.dispatch_events_called = True if mode=="rgb_array" or mode=="state_pixels": win.clear() t = self.transform if mode=='rgb_array': VP_W = VIDEO_W VP_H = VIDEO_H else: VP_W = STATE_W VP_H = STATE_H gl.glViewport(0, 0, VP_W, VP_H) t.enable() self.render_road() for geom in self.viewer.onetime_geoms: geom.render() t.disable() self.render_indicators(WINDOW_W, WINDOW_H) # TODO: find why 2x needed, wtf image_data = pyglet.image.get_buffer_manager().get_color_buffer().get_image_data() arr = np.fromstring(image_data.data, dtype=np.uint8, sep='') arr = arr.reshape(VP_H, VP_W, 4) arr = arr[::-1, :, 0:3] if mode=="rgb_array" and not self.human_render: # agent can call or not call env.render() itself when recording video. win.flip() if mode=='human': self.human_render = True win.clear() t = self.transform gl.glViewport(0, 0, WINDOW_W, WINDOW_H) t.enable() self.render_road() for geom in self.viewer.onetime_geoms: geom.render() t.disable() self.render_indicators(WINDOW_W, WINDOW_H) win.flip() self.viewer.onetime_geoms = [] return arr
def render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return scale = self.width / self.l_unit # 计算两者映射关系 rad = self.radius * scale # 随后都是用世界尺寸来描述 t_rad = self.target_radius * scale # target radius action = self.action if action is None: length = 0.00 else: length = np.sqrt(np.sum(np.dot(action, action))) # print(action, length) # 如果还没有设定屏幕对象,则初始化整个屏幕具备的元素。 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(self.width, self.height) # 在Viewer里绘制一个几何图像的步骤如下: # 1. 建立该对象需要的数据本身 # 2. 使用rendering提供的方法返回一个geom对象 # 3. 对geom对象进行一些对象颜色、线宽、线型、变换属性的设置(有些对象提供一些个 # 性化的方法 # 来设置属性,具体请参考继承自这些Geom的对象),这其中有一个重要的属性就是 # 变换属性,该属性负责对对象在屏幕中的位置、渲染、缩放进行渲染。如果某对象 # 在呈现时可能发生上述变化,则应建立关于该对象的变换属性。该属性是一个 # Transform对象,而一个Transform对象,包括translate、rotate和scale # 三个属性,每个属性都由以np.array对象描述的矩阵决定。 # 4. 将新建立的geom对象添加至viewer的绘制对象列表里,如果在屏幕上只出现一次, # 将其加入到add_onegeom()列表中,如果需要多次渲染,则将其加入add_geom() # 5. 在渲染整个viewer之前,对有需要的geom的参数进行修改,修改主要基于该对象 # 的Transform对象 # 6. 调用Viewer的render()方法进行绘制 target = rendering.make_circle(t_rad, 30, True) target.set_color(0.1, 0.9, 0.1) self.viewer.add_geom(target) target_circle = rendering.make_circle(t_rad, 30, False) target_circle.set_color(0, 0, 0) self.viewer.add_geom(target_circle) self.target_trans = rendering.Transform() target.add_attr(self.target_trans) target_circle.add_attr(self.target_trans) self.agent = rendering.make_circle(rad, 30, True) self.agent.set_color(0, 1, 0) self.viewer.add_geom(self.agent) self.agent_trans = rendering.Transform() self.agent.add_attr(self.agent_trans) agent_circle = rendering.make_circle(rad, 30, False) agent_circle.set_color(0, 0, 0) agent_circle.add_attr(self.agent_trans) self.viewer.add_geom(agent_circle) start_p = (0, 0) end_p = (0.7 * rad * length, 0) self.line = rendering.Line(start_p, end_p) self.line.linewidth = rad / 10 self.line_trans = rendering.Transform() self.line.add_attr(self.line_trans) self.viewer.add_geom(self.line) self.arrow = rendering.FilledPolygon([ (0.7 * rad * length, 0.15 * rad), (rad * length, 0), (0.7 * rad * length, -0.15 * rad) ]) self.arrow.set_color(0, 0, 0) self.arrow.add_attr(self.line_trans) self.viewer.add_geom(self.arrow) # 如果已经为屏幕准备好了要绘制的对象 # 本例中唯一要做的就是改变小车的位置和旋转 ppx, ppy, _, _, tx, ty = self.state self.target_trans.set_translation(tx * scale, ty * scale) self.agent_trans.set_translation(ppx * scale, ppy * scale) # 按距离给Agent着色 vv, ms = self.reward + 0.3, 1 r, g, b, = 0, 1, 0 if vv >= 0: r, g, b = 1 - ms * vv, 1, 1 - ms * vv else: r, g, b = 1, 1 + ms * vv, 1 + ms * vv self.agent.set_color(r, g, b) if length == 0: self.line.set_color(r, g, b) self.arrow.set_color(r, g, b) # 背景色 else: if action[1] >= 0: rotate = math.acos(action[0] / length) # action[0]水平方向 else: # 垂直方向 rotate = 2 * math.pi - math.acos(action[0] / length) self.line_trans.set_translation(ppx * scale, ppy * scale) self.line_trans.set_rotation(rotate) self.line.set_color(0, 0, 0) self.arrow.set_color(0, 0, 0) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human', close=False): from gym.envs.classic_control import rendering # uniform width/height for window for now screen_width, screen_height = 600, 600 scale_x = screen_width / (4 * (self.x_upper - self.x_lower)) scale_y = screen_height / (4 * (self.y_upper - self.y_lower)) scale = 3 * scale_x if scale_x != scale_y: scale = np.min((scale_x, scale_y)) print('Scales not matching') if self.viewer is None: # Define viewer self.viewer = rendering.Viewer(screen_width, screen_height) # Draw base base = rendering.make_circle(scale * self.rs) base.set_color(0., 0., 0.) self.basetrans = rendering.Transform() base.add_attr(self.basetrans) self.viewer.add_geom(base) # Draw link 1 xs = np.linspace(0, scale * self.Ls, 100) ys = np.zeros(xs.shape) xys = list(zip(xs, ys)) l1 = rendering.make_polyline(xys) # draw a straight line l1.set_color(1., 0., 0.) l1.set_linewidth(3) self.l1trans = rendering.Transform( ) # create transform object for that line l1.add_attr(self.l1trans) self.viewer.add_geom(l1) # Draw link 2 xs = np.linspace(0, scale * self.Lo, 100) ys = np.zeros(xs.shape) xys = list(zip(xs, ys)) l2 = rendering.make_polyline(xys) l2.set_color(0., 1., 0.) l2.set_linewidth(3) self.l2trans = rendering.Transform() l2.add_attr(self.l2trans) self.viewer.add_geom(l2) # Draw obj obj = rendering.make_circle(scale * self.ro) obj.set_color(.5, .5, .5) self.objtrans = rendering.Transform() obj.add_attr(self.objtrans) self.viewer.add_geom(obj) # Draw panel 1 xs = np.linspace(0, scale * self.panel1_len, 100) ys = np.zeros(xs.shape) xys = list(zip(xs, ys)) p1 = rendering.make_polyline(xys) p1.set_color(0., 0., 1.) p1.set_linewidth(4) self.p1trans = rendering.Transform() p1.add_attr(self.p1trans) self.viewer.add_geom(p1) # Draw panel 2 xs = np.linspace(0, scale * self.panel2_len, 100) ys = np.zeros(xs.shape) xys = list(zip(xs, ys)) p2 = rendering.make_polyline(xys) p2.set_color(0., 0., 1.) p2.set_linewidth(4) self.p2trans = rendering.Transform() p2.add_attr(self.p2trans) self.viewer.add_geom(p2) # Calculate poses for geometries xs, ys, ths, vxs, vys, vths, xo, yo, tho, vxo, vyo, vtho = self.state # velocity direction ths_vel = np.arctan2(vys, vxs) tho_vel = np.arctan2(vyo, vxo) # NOTE: x_conn_s&y_conn_s definitions are NOT same as defined above x_conn_s = xs + np.cos(ths_vel) * self.rs y_conn_s = ys + np.sin(ths_vel) * self.rs x_conn_o = xo + np.cos(tho_vel) * self.ro y_conn_o = yo + np.sin(tho_vel) * self.ro xp1 = xs - np.cos(ths + self.panel1_angle) * (self.rs + self.panel1_len) yp1 = ys - np.sin(ths + self.panel1_angle) * (self.rs + self.panel1_len) xp2 = xs - np.cos(ths + self.panel2_angle) * (self.rs + self.panel2_len) yp2 = ys - np.sin(ths + self.panel2_angle) * (self.rs + self.panel2_len) # Update poses for geometries self.basetrans.set_translation(screen_width / 2 + scale * xs, screen_height / 2 + scale * ys) self.basetrans.set_rotation(ths) self.l1trans.set_translation(screen_width / 2 + scale * x_conn_s, screen_height / 2 + scale * y_conn_s) # pointing along spacecraft velocity self.l1trans.set_rotation(ths_vel) self.l2trans.set_translation(screen_width / 2 + scale * x_conn_o, screen_height / 2 + scale * y_conn_o) self.l2trans.set_rotation(tho_vel) self.objtrans.set_translation(screen_width / 2 + scale * xo, screen_height / 2 + scale * yo) self.objtrans.set_rotation(tho) self.p1trans.set_translation(screen_width / 2 + scale * xp1, screen_height / 2 + scale * yp1) self.p1trans.set_rotation(ths + self.panel1_angle) self.p2trans.set_translation(screen_width / 2 + scale * xp2, screen_height / 2 + scale * yp2) self.p2trans.set_rotation(ths + self.panel2_angle) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return screen_width = 600 screen_height = 600 # before was 400 world_width = 5 # max visible position of cart scale = screen_width / world_width carty = screen_height / 2 # TOP OF CART polewidth = 6.0 polelen = scale * self.l # 0.6 or self.l cartwidth = 40.0 cartheight = 20.0 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2 cart = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.carttrans = rendering.Transform() cart.add_attr(self.carttrans) cart.set_color(1, 0, 0) 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(0, 0, 1) self.poletrans = rendering.Transform(translation=(0, 0)) 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(0.1, 1, 1) self.viewer.add_geom(self.axle) # Make another circle on the top of the pole self.pole_bob = rendering.make_circle(polewidth / 2) self.pole_bob_trans = rendering.Transform() self.pole_bob.add_attr(self.pole_bob_trans) self.pole_bob.add_attr(self.poletrans) self.pole_bob.add_attr(self.carttrans) self.pole_bob.set_color(0, 0, 0) self.viewer.add_geom(self.pole_bob) self.wheel_l = rendering.make_circle(cartheight / 4) self.wheel_r = rendering.make_circle(cartheight / 4) self.wheeltrans_l = rendering.Transform( translation=(-cartwidth / 2, -cartheight / 2)) self.wheeltrans_r = rendering.Transform(translation=(cartwidth / 2, -cartheight / 2)) self.wheel_l.add_attr(self.wheeltrans_l) self.wheel_l.add_attr(self.carttrans) self.wheel_r.add_attr(self.wheeltrans_r) self.wheel_r.add_attr(self.carttrans) self.wheel_l.set_color(0, 0, 0) # Black, (B, G, R) self.wheel_r.set_color(0, 0, 0) # Black, (B, G, R) self.viewer.add_geom(self.wheel_l) self.viewer.add_geom(self.wheel_r) self.track = rendering.Line( (screen_width / 2 - self.x_threshold * scale, carty - cartheight / 2 - cartheight / 4), (screen_width / 2 + self.x_threshold * scale, carty - cartheight / 2 - cartheight / 4)) 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]) self.pole_bob_trans.set_translation(-self.l * np.sin(x[2]), self.l * np.cos(x[2])) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): from gym.envs.classic_control import rendering s = self.state w_size = 8 if self.viewer is None: self.viewer = rendering.Viewer(500, 250) self.viewer.set_bounds(-w_size, w_size, -w_size / 2, w_size / 2) if s is None: return None box_l = 0.5 # define center points and angles arm_xy = np.array([4.0, 0.0]) pen_xy = np.array([-4.0, 0.0]) thetas = [s[0] + np.pi, s[1] + np.pi] # define colors rgb_dred = [0.5, 0, 0] rgb_lred = [0.9, 0, 0] rgb_gray = [0.5, 0.5, 0.5] rgb_light = [0.9, 0.9, 0.9] self.viewer.draw_line((0, -w_size), (0, w_size)) self.viewer.draw_line((arm_xy[0], -w_size), (arm_xy[0], w_size)).set_color(*rgb_light) self.viewer.draw_line((pen_xy[1], -w_size), (pen_xy[1], w_size)).set_color(*rgb_light) l, r, t, b = -0.2, 0.2, 3, 0 # link dimintions # ARM arm_box = self.viewer.draw_polygon([(-box_l, box_l), (box_l, box_l), (box_l, -box_l), (-box_l, -box_l)]) arm_box.set_color(0, 0, 0) arm_bos_tra = rendering.Transform(rotation=0, translation=(arm_xy[0], arm_xy[1])) arm_box.add_attr(arm_bos_tra) arm = self.viewer.draw_polygon([(l, t), (r, t), (r, b), (l, b)]) arm_tra = rendering.Transform(rotation=thetas[0], translation=(arm_xy[0], arm_xy[1])) arm.add_attr(arm_tra) arm.set_color(*rgb_gray) arm_circ = self.viewer.draw_circle(.2) arm_circ.add_attr(arm_tra) arm_circ.set_color(*rgb_dred) # PENDULUM + box_l + .2 pen_box = self.viewer.draw_polygon([(-box_l, box_l), (box_l, box_l), (box_l, -box_l), (-box_l, -box_l)]) pen_box_tra = rendering.Transform(rotation=0, translation=(pen_xy[0], pen_xy[1])) pen_box.add_attr(pen_box_tra) pen_bar = self.viewer.draw_polygon([(-box_l, -0.1), (box_l, -0.1), (box_l, -0.2), (-box_l, -0.2)]) pen_bar.add_attr(pen_box_tra) pen_bar.set_color(*rgb_lred) pen = self.viewer.draw_polygon([(l, t), (r, t), (r, b), (l, b)]) pen_tra = rendering.Transform(rotation=thetas[1], translation=(pen_xy[0], pen_xy[1] + box_l + 0.2)) pen.add_attr(pen_tra) pen.set_color(*rgb_dred) pen_circ = self.viewer.draw_circle(.2) pen_circ.add_attr(pen_tra) pen_circ.set_color(*rgb_gray) return self.viewer.render()
def _render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H) self.viewer.set_bounds(self.scroll, VIEWPORT_W / SCALE + self.scroll, 0, VIEWPORT_H / SCALE) self.viewer.draw_polygon([ (self.scroll, 0), (self.scroll + VIEWPORT_W / SCALE, 0), (self.scroll + VIEWPORT_W / SCALE, VIEWPORT_H / SCALE), (self.scroll, VIEWPORT_H / SCALE), ], color=(0.9, 0.9, 1.0)) for poly, x1, x2 in self.cloud_poly: if x2 < self.scroll / 2: continue if x1 > self.scroll / 2 + VIEWPORT_W / SCALE: continue self.viewer.draw_polygon([(p[0] + self.scroll / 2, p[1]) for p in poly], color=(1, 1, 1)) for poly, color in self.terrain_poly: if poly[1][0] < self.scroll: continue if poly[0][0] > self.scroll + VIEWPORT_W / SCALE: continue self.viewer.draw_polygon(poly, color=color) self.lidar_render = (self.lidar_render + 1) % 100 i = self.lidar_render if i < 2 * len(self.lidar): l = self.lidar[i] if i < len( self.lidar) else self.lidar[len(self.lidar) - i - 1] self.viewer.draw_polyline([l.p1, l.p2], color=(1, 0, 0), linewidth=1) for obj in self.drawlist: for f in obj.fixtures: trans = f.body.transform if type(f.shape) is circleShape: t = rendering.Transform(translation=trans * f.shape.pos) self.viewer.draw_circle(f.shape.radius, 30, color=obj.color1).add_attr(t) self.viewer.draw_circle(f.shape.radius, 30, color=obj.color2, filled=False, linewidth=2).add_attr(t) else: path = [trans * v for v in f.shape.vertices] self.viewer.draw_polygon(path, color=obj.color1) path.append(path[0]) self.viewer.draw_polyline(path, color=obj.color2, linewidth=2) flagy1 = TERRAIN_HEIGHT flagy2 = flagy1 + 50 / SCALE x = TERRAIN_STEP * 3 self.viewer.draw_polyline([(x, flagy1), (x, flagy2)], color=(0, 0, 0), linewidth=2) f = [(x, flagy2), (x, flagy2 - 10 / SCALE), (x + 25 / SCALE, flagy2 - 5 / SCALE)] self.viewer.draw_polygon(f, color=(0.9, 0.2, 0)) self.viewer.draw_polyline(f + [f[0]], color=(0, 0, 0), linewidth=2) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def render(self, mode='human'): from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H) self.viewer.set_bounds(self.scroll, VIEWPORT_W / SCALE + self.scroll, 0, VIEWPORT_H / SCALE) # Purple sky self.viewer.draw_polygon([ (self.scroll, 0), (self.scroll + VIEWPORT_W / SCALE, 0), (self.scroll + VIEWPORT_W / SCALE, VIEWPORT_H / SCALE), (self.scroll, VIEWPORT_H / SCALE), ], color=(0.9, 0.9, 1.0)) # Clouds for poly, x1, x2 in self.cloud_poly: if x2 < self.scroll / 2: continue if x1 > self.scroll / 2 + VIEWPORT_W / SCALE: continue self.viewer.draw_polygon([(p[0] + self.scroll / 2, p[1]) for p in poly], color=(1, 1, 1)) # Ground fill (areas under the terrain) for poly, color in self.terrain_poly: # Ignore polygons off-screen if poly[1][0] < self.scroll: continue if poly[0][0] > self.scroll + VIEWPORT_W / SCALE: continue self.viewer.draw_polygon(poly, color=color) # Red line for lidar self.lidar_render = (self.lidar_render + 1) % 100 i = self.lidar_render if i < 2 * len(self.lidar): l = self.lidar[i] if i < len( self.lidar) else self.lidar[len(self.lidar) - i - 1] self.viewer.draw_polyline([l.p1, l.p2], color=(1, 0, 0), linewidth=1) # Draw walker and terrain for obj in self.drawlist: # Collision bodies for f in obj.fixtures: trans = f.body.transform if type(f.shape) is circleShape: t = rendering.Transform(translation=trans * f.shape.pos) self.viewer.draw_circle(f.shape.radius, 30, color=obj.color1).add_attr(t) self.viewer.draw_circle(f.shape.radius, 30, color=obj.color2, filled=False, linewidth=2).add_attr(t) else: path = [trans * v for v in f.shape.vertices] self.viewer.draw_polygon(path, color=obj.color1) # Turn the list of vertices into a path by closing the loop path.append(path[0]) # Draw outline of shape self.viewer.draw_polyline(path, color=obj.color2, linewidth=2) # Draw starting flag flagy1 = TERRAIN_HEIGHT flagy2 = flagy1 + 50 / SCALE x = TERRAIN_STEP * 3 # Flag pole self.viewer.draw_polyline([(x, flagy1), (x, flagy2)], color=(0, 0, 0), linewidth=2) # Flag triangle f = [(x, flagy2), (x, flagy2 - 10 / SCALE), (x + 25 / SCALE, flagy2 - 5 / SCALE)] self.viewer.draw_polygon(f, color=(0.9, 0.2, 0)) # Flag outline self.viewer.draw_polyline(f + [f[0]], color=(0, 0, 0), linewidth=2) use_rgb_array = mode == 'rgb_array' return self.viewer.render(return_rgb_array=use_rgb_array)