Graphics

Environment rendering is done with pygame, which must be installed separately.

A window is created at the first call of env.render(). Its dimensions can be configured:

env = gym.make("roundabout-v0")
env.configure({
    "screen_width": 640,
    "screen_height": 480
})
env.reset()
env.render()

World surface

The simulation is rendered in a RoadSurface pygame surface, which defines the location and zoom of the rendered location. By default, the rendered area is always centered on the ego-vehicle. Its initial scale and offset can be set with the "scaling" and "centering_position" configurations, and can also be updated during simulation using the O,L keys and K,M keys, respectively.

Scene graphics

API

class highway_env.envs.common.graphics.EnvViewer(env: AbstractEnv)[source]

A viewer to render a highway driving environment.

SAVE_IMAGES = False
__init__(env: AbstractEnv) → None[source]

Initialize self. See help(type(self)) for accurate signature.

set_agent_display(agent_display: Callable) → None[source]

Set a display callback provided by an agent

So that they can render their behaviour on a dedicated agent surface, or even on the simulation surface.

Parameters

agent_display – a callback provided by the agent to display on surfaces

set_agent_action_sequence(actions: List[Action]) → None[source]

Set the sequence of actions chosen by the agent, so that it can be displayed

Parameters

actions – list of action, following the env’s action space specification

handle_events() → None[source]

Handle pygame events by forwarding them to the display and environment vehicle.

display() → None[source]

Display the road and vehicles on a pygame window.

get_image() → numpy.ndarray[source]

the rendered image as a rbg array.

window_position() → numpy.ndarray[source]

the world position of the center of the displayed window.

close() → None[source]

Close the pygame window.

__dict__ = mappingproxy({'__module__': 'highway_env.envs.common.graphics', '__doc__': 'A viewer to render a highway driving environment.', 'SAVE_IMAGES': False, '__init__': <function EnvViewer.__init__>, 'set_agent_display': <function EnvViewer.set_agent_display>, 'set_agent_action_sequence': <function EnvViewer.set_agent_action_sequence>, 'handle_events': <function EnvViewer.handle_events>, 'display': <function EnvViewer.display>, 'get_image': <function EnvViewer.get_image>, 'window_position': <function EnvViewer.window_position>, 'close': <function EnvViewer.close>, '__dict__': <attribute '__dict__' of 'EnvViewer' objects>, '__weakref__': <attribute '__weakref__' of 'EnvViewer' objects>})
__module__ = 'highway_env.envs.common.graphics'
__weakref__

list of weak references to the object (if defined)

class highway_env.envs.common.graphics.EventHandler[source]
classmethod handle_event(action_type: highway_env.envs.common.action.ActionType, event: Event) → None[source]

Map the pygame keyboard events to control decisions

Parameters
  • action_type – the ActionType that defines how the vehicle is controlled

  • event – the pygame event

classmethod handle_discrete_action_event(action_type: highway_env.envs.common.action.DiscreteMetaAction, event: Event) → None[source]
__dict__ = mappingproxy({'__module__': 'highway_env.envs.common.graphics', 'handle_event': <classmethod object>, 'handle_discrete_action_event': <classmethod object>, 'handle_continuous_action_event': <classmethod object>, '__dict__': <attribute '__dict__' of 'EventHandler' objects>, '__weakref__': <attribute '__weakref__' of 'EventHandler' objects>, '__doc__': None})
__module__ = 'highway_env.envs.common.graphics'
__weakref__

list of weak references to the object (if defined)

classmethod handle_continuous_action_event(action_type: highway_env.envs.common.action.ContinuousAction, event: Event) → None[source]
class highway_env.road.graphics.WorldSurface(size: Tuple[int, int], flags: object, surf: pygame.Surface)[source]

A pygame Surface implementing a local coordinate system so that we can move and zoom in the displayed area.

BLACK = (0, 0, 0)
GREY = (100, 100, 100)
GREEN = (50, 200, 0)
YELLOW = (200, 200, 0)
WHITE = (255, 255, 255)
INITIAL_SCALING = 5.5
INITIAL_CENTERING = [0.5, 0.5]
SCALING_FACTOR = 1.3
MOVING_FACTOR = 0.1
__init__(size: Tuple[int, int], flags: object, surf: pygame.Surface) → None[source]

Initialize self. See help(type(self)) for accurate signature.

pix(length: float) → int[source]

Convert a distance [m] to pixels [px].

Parameters

length – the input distance [m]

Returns

the corresponding size [px]

pos2pix(x: float, y: float) → Tuple[int, int][source]

Convert two world coordinates [m] into a position in the surface [px]

Parameters
  • x – x world coordinate [m]

  • y – y world coordinate [m]

Returns

the coordinates of the corresponding pixel [px]

vec2pix(vec: Union[Tuple[float, float], numpy.ndarray]) → Tuple[int, int][source]

Convert a world position [m] into a position in the surface [px].

Parameters

vec – a world position [m]

Returns

the coordinates of the corresponding pixel [px]

move_display_window_to(position: Union[Tuple[float, float], numpy.ndarray]) → None[source]

Set the origin of the displayed area to center on a given world position.

Parameters

position – a world position [m]

handle_event(event: Event) → None[source]

Handle pygame events for moving and zooming in the displayed area.

Parameters

event – a pygame event

__dict__ = mappingproxy({'__module__': 'highway_env.road.graphics', '__doc__': 'A pygame Surface implementing a local coordinate system so that we can move and zoom in the displayed area.', 'BLACK': (0, 0, 0), 'GREY': (100, 100, 100), 'GREEN': (50, 200, 0), 'YELLOW': (200, 200, 0), 'WHITE': (255, 255, 255), 'INITIAL_SCALING': 5.5, 'INITIAL_CENTERING': [0.5, 0.5], 'SCALING_FACTOR': 1.3, 'MOVING_FACTOR': 0.1, '__init__': <function WorldSurface.__init__>, 'pix': <function WorldSurface.pix>, 'pos2pix': <function WorldSurface.pos2pix>, 'vec2pix': <function WorldSurface.vec2pix>, 'move_display_window_to': <function WorldSurface.move_display_window_to>, 'handle_event': <function WorldSurface.handle_event>, '__dict__': <attribute '__dict__' of 'WorldSurface' objects>})
__module__ = 'highway_env.road.graphics'
class highway_env.road.graphics.LaneGraphics[source]

A visualization of a lane.

STRIPE_SPACING: float = 5

Offset between stripes [m]

STRIPE_LENGTH: float = 3

Length of a stripe [m]

STRIPE_WIDTH: float = 0.3

Width of a stripe [m]

classmethod display(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface) → None[source]

Display a lane on a surface.

Parameters
  • lane – the lane to be displayed

  • surface – the pygame surface

classmethod striped_line(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface, stripes_count: int, longitudinal: float, side: int) → None[source]

Draw a striped line on one side of a lane, on a surface.

Parameters
  • lane – the lane

  • surface – the pygame surface

  • stripes_count – the number of stripes to draw

  • longitudinal – the longitudinal position of the first stripe [m]

  • side – which side of the road to draw [0:left, 1:right]

classmethod continuous_curve(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface, stripes_count: int, longitudinal: float, side: int) → None[source]

Draw a striped line on one side of a lane, on a surface.

Parameters
  • lane – the lane

  • surface – the pygame surface

  • stripes_count – the number of stripes to draw

  • longitudinal – the longitudinal position of the first stripe [m]

  • side – which side of the road to draw [0:left, 1:right]

classmethod continuous_line(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface, stripes_count: int, longitudinal: float, side: int) → None[source]

Draw a continuous line on one side of a lane, on a surface.

Parameters
  • lane – the lane

  • surface – the pygame surface

  • stripes_count – the number of stripes that would be drawn if the line was striped

  • longitudinal – the longitudinal position of the start of the line [m]

  • side – which side of the road to draw [0:left, 1:right]

classmethod draw_stripes(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface, starts: List[float], ends: List[float], lats: List[float]) → None[source]

Draw a set of stripes along a lane.

Parameters
  • lane – the lane

  • surface – the surface to draw on

  • starts – a list of starting longitudinal positions for each stripe [m]

  • ends – a list of ending longitudinal positions for each stripe [m]

  • lats – a list of lateral positions for each stripe [m]

classmethod draw_ground(lane: highway_env.road.lane.AbstractLane, surface: highway_env.road.graphics.WorldSurface, color: Tuple[float], width: float, draw_surface: pygame.Surface = None) → None[source]
__annotations__ = {'STRIPE_LENGTH': <class 'float'>, 'STRIPE_SPACING': <class 'float'>, 'STRIPE_WIDTH': <class 'float'>}
__dict__ = mappingproxy({'__module__': 'highway_env.road.graphics', '__annotations__': {'STRIPE_SPACING': <class 'float'>, 'STRIPE_LENGTH': <class 'float'>, 'STRIPE_WIDTH': <class 'float'>}, '__doc__': 'A visualization of a lane.', 'STRIPE_SPACING': 5, 'STRIPE_LENGTH': 3, 'STRIPE_WIDTH': 0.3, 'display': <classmethod object>, 'striped_line': <classmethod object>, 'continuous_curve': <classmethod object>, 'continuous_line': <classmethod object>, 'draw_stripes': <classmethod object>, 'draw_ground': <classmethod object>, '__dict__': <attribute '__dict__' of 'LaneGraphics' objects>, '__weakref__': <attribute '__weakref__' of 'LaneGraphics' objects>})
__module__ = 'highway_env.road.graphics'
__weakref__

list of weak references to the object (if defined)

class highway_env.road.graphics.RoadGraphics[source]

A visualization of a road lanes and vehicles.

static display(road: highway_env.road.road.Road, surface: highway_env.road.graphics.WorldSurface) → None[source]

Display the road lanes on a surface.

Parameters
  • road – the road to be displayed

  • surface – the pygame surface

static display_traffic(road: highway_env.road.road.Road, surface: highway_env.road.graphics.WorldSurface, simulation_frequency: int = 15, offscreen: bool = False) → None[source]

Display the road vehicles on a surface.

Parameters
  • road – the road to be displayed

  • surface – the pygame surface

  • simulation_frequency – simulation frequency

  • offscreen – render without displaying on a screen

static display_road_objects(road: highway_env.road.road.Road, surface: highway_env.road.graphics.WorldSurface, offscreen: bool = False) → None[source]

Display the road objects on a surface.

Parameters
  • road – the road to be displayed

  • surface – the pygame surface

  • offscreen – whether the rendering should be done offscreen or not

__dict__ = mappingproxy({'__module__': 'highway_env.road.graphics', '__doc__': 'A visualization of a road lanes and vehicles.', 'display': <staticmethod object>, 'display_traffic': <staticmethod object>, 'display_road_objects': <staticmethod object>, '__dict__': <attribute '__dict__' of 'RoadGraphics' objects>, '__weakref__': <attribute '__weakref__' of 'RoadGraphics' objects>})
__module__ = 'highway_env.road.graphics'
__weakref__

list of weak references to the object (if defined)

class highway_env.road.graphics.RoadObjectGraphics[source]

A visualization of objects on the road.

YELLOW = (200, 200, 0)
BLUE = (100, 200, 255)
RED = (255, 100, 100)
GREEN = (50, 200, 0)
BLACK = (60, 60, 60)
DEFAULT_COLOR = (200, 200, 0)
__dict__ = mappingproxy({'__module__': 'highway_env.road.graphics', '__doc__': 'A visualization of objects on the road.', 'YELLOW': (200, 200, 0), 'BLUE': (100, 200, 255), 'RED': (255, 100, 100), 'GREEN': (50, 200, 0), 'BLACK': (60, 60, 60), 'DEFAULT_COLOR': (200, 200, 0), 'display': <classmethod object>, 'blit_rotate': <staticmethod object>, 'get_color': <classmethod object>, '__dict__': <attribute '__dict__' of 'RoadObjectGraphics' objects>, '__weakref__': <attribute '__weakref__' of 'RoadObjectGraphics' objects>})
__module__ = 'highway_env.road.graphics'
__weakref__

list of weak references to the object (if defined)

classmethod display(object_: RoadObject, surface: highway_env.road.graphics.WorldSurface, transparent: bool = False, offscreen: bool = False)[source]

Display a road objects on a pygame surface.

The objects is represented as a colored rotated rectangle

Parameters
  • object – the vehicle to be drawn

  • surface – the surface to draw the object on

  • transparent – whether the object should be drawn slightly transparent

  • offscreen – whether the rendering should be done offscreen or not

static blit_rotate(surf: pygame.Surface, image: pygame.Surface, pos: Union[numpy.ndarray, Sequence[float]], angle: float, origin_pos: Union[numpy.ndarray, Sequence[float]] = None, show_rect: bool = False) → None[source]

Many thanks to https://stackoverflow.com/a/54714144.

classmethod get_color(object_: RoadObject, transparent: bool = False)[source]
class highway_env.vehicle.graphics.VehicleGraphics[source]
RED = (255, 100, 100)
GREEN = (50, 200, 0)
BLUE = (100, 200, 255)
YELLOW = (200, 200, 0)
BLACK = (60, 60, 60)
PURPLE = (200, 0, 150)
DEFAULT_COLOR = (200, 200, 0)
EGO_COLOR = (50, 200, 0)
classmethod display(vehicle: highway_env.vehicle.kinematics.Vehicle, surface: WorldSurface, transparent: bool = False, offscreen: bool = False, label: bool = False) → None[source]

Display a vehicle on a pygame surface.

The vehicle is represented as a colored rotated rectangle.

Parameters
  • vehicle – the vehicle to be drawn

  • surface – the surface to draw the vehicle on

  • transparent – whether the vehicle should be drawn slightly transparent

  • offscreen – whether the rendering should be done offscreen or not

  • label – whether a text label should be rendered

static blit_rotate(surf: pygame.Surface, image: pygame.Surface, pos: Union[numpy.ndarray, Sequence[float]], angle: float, origin_pos: Union[numpy.ndarray, Sequence[float]] = None, show_rect: bool = False) → None[source]

Many thanks to https://stackoverflow.com/a/54714144.

classmethod display_trajectory(states: List[highway_env.vehicle.kinematics.Vehicle], surface: WorldSurface, offscreen: bool = False) → None[source]

Display the whole trajectory of a vehicle on a pygame surface.

Parameters
  • states – the list of vehicle states within the trajectory to be displayed

  • surface – the surface to draw the vehicle future states on

  • offscreen – whether the rendering should be done offscreen or not

classmethod display_history(vehicle: highway_env.vehicle.kinematics.Vehicle, surface: WorldSurface, frequency: float = 3, duration: float = 2, simulation: int = 15, offscreen: bool = False) → None[source]

Display the whole trajectory of a vehicle on a pygame surface.

Parameters
  • vehicle – the vehicle states within the trajectory to be displayed

  • surface – the surface to draw the vehicle future states on

  • frequency – frequency of displayed positions in history

  • duration – length of displayed history

  • simulation – simulation frequency

  • offscreen – whether the rendering should be done offscreen or not

classmethod get_color(vehicle: highway_env.vehicle.kinematics.Vehicle, transparent: bool = False) → Tuple[int][source]
__dict__ = mappingproxy({'__module__': 'highway_env.vehicle.graphics', 'RED': (255, 100, 100), 'GREEN': (50, 200, 0), 'BLUE': (100, 200, 255), 'YELLOW': (200, 200, 0), 'BLACK': (60, 60, 60), 'PURPLE': (200, 0, 150), 'DEFAULT_COLOR': (200, 200, 0), 'EGO_COLOR': (50, 200, 0), 'display': <classmethod object>, 'blit_rotate': <staticmethod object>, 'display_trajectory': <classmethod object>, 'display_history': <classmethod object>, 'get_color': <classmethod object>, '__dict__': <attribute '__dict__' of 'VehicleGraphics' objects>, '__weakref__': <attribute '__weakref__' of 'VehicleGraphics' objects>, '__doc__': None})
__module__ = 'highway_env.vehicle.graphics'
__weakref__

list of weak references to the object (if defined)