visualiser package

Provides the visualiser modules

class visualiser.SimulatorVisualiser(windowID)[source]

Bases: object

The visualiser of the simulator, showing a graphical representation of the highway.

Parameters:windowIDstr set as the SDL_WINDOWID environment variable. Used to embed the pygame window into the GUI.
_backgrounds = None

list storing the active BackgroundPiece

_bgHeight = None

(int) Height of the background sprite

_cachedSprites = None

(boolean) Whether all sprites have been cached

_fps = None

(int) Frames per second target

_lastAction = None

(Actions) last action performed

_lastDraw = None

(float) Timestamp of the last time the screen was drawn

_lastSpawn = None

(int) Last timestamp an obstacle vehicle was spawned

_lastTick = None

(float) Timestamp of the last tick() call

_obstacles = None

list storing the currently spawned Obstacle instances

_targetDrawDelay = None

(float) Targeted milliseconds in between screen draws

agentCar = None

The Car controlled by the agent

agentCollisions = None

int indicating the number of agent collisions in the current run

canvas = None

A reference to the Surface for drawing

car = None

The Car controlled by the expert

collisions = None

int indicating the number of expert collisions in the current run

distanceTravelled = None

int indicating the number metres travelled in the current run

doAct(act, agent=False)[source]

Perform Action act

draw()[source]

Updates the canvas Attempts to achieve target FPS by blocking As such, it should run in its own thread so other things can be done in the background Should run in and endless loop to continuously redraw

fps

The current target frames per second

Getter:Get the current FPS
Setter:Set the current FPS. Calculates the necessary target draw interval
Type:int
keyPress(key)[source]

Handle key presses and perform the actions they map onto

lastActionTime = None

float Timestamp when the last action was performed

mode

The mode of the visualiser. See mode for details.

Getter:Return the current set mode
Setter:Set the current mode. Also sets the correct dimensions of the display, doubling the width if in compare mode
Type:int
pause = None

bool indicating whether the visualiser is paused

reset()[source]

Restart the visualiser by resetting properties

sessionTime = None

float indicating the milliseconds since starting the current run

stateVector(agent=False)[source]

Return a dictionary of features: {last action, current lane, distance of obstales in three lanes, offroad} Some features have been ‘pruned’ from our descision tree because they did not affect the accuracy of the tree. These have been commented out for clarity.

tick()[source]

This method tries to run at regular intervals of TickRate milliseconds. Performs update logic of the cars, obstacles, and background using time since the previous tick. Stores the current tick timestamp in _lastTick

togglePause()[source]

Toggle pause state of the game

Submodules

visualiser.util module

Utility functions and classes

class visualiser.util.Actions[source]

Bases: enum.Enum

Enumerations of the actions that the simulator can receive

LEFT = 2

Move into the lane to the left

NONE = 1

No action

PAUSE = 4

Toggle the pause state of the simulator

RIGHT = 3

Move into the lane to the right

class visualiser.util.Pos(vehicle, x=0, y=0)[source]

Bases: visualiser.util.Vector

2D vector that automatically updates its vehicle’s rect position

x

The x component of the vector :getter: Return x value :setter: Set x value, rounded to nearest int

y

The y component of the vector :getter: Return y value :setter: Set y value, rounded to nearest int

class visualiser.util.Vector(x=0, y=0)[source]

Bases: object

A 2D vector, with arithmetic magic methods implemented

x = None

The x component of the vector

y = None

The y component of the vector

visualiser.util.loadSprite(path, scale=1)[source]

Load an image as a pygame.Surface. Automatically caches loaded images in the background

Parameters:
  • path – (str) Path to desired image, relative to the sprites/ directory
  • scale – (float) Multiplied against the size of the image in order to scale as desired

visualiser.vehicles module

Vehicle classes used by the game

class visualiser.vehicles.Car(game)[source]

Bases: visualiser.vehicles.Vehicle

The controlled car

Parameters:game – The parent SimulatorVisualiser
tick(dt)[source]

Car tick method, currently does nothing

class visualiser.vehicles.Obstacle(game)[source]

Bases: visualiser.vehicles.Vehicle

An obstacle vehicle that the main car has to avoid

Parameters:game – The parent SimulatorVisualiser
_agentCollided = None

bool indicating whether this vehicle has collided with the agent car

_collided = None

bool indicating whether this vehicle has collided with the user car

hasCollided
Getter:Return whether the vehicle has just collided this tick. Updates _collided
hasCollidedAgent
Getter:Returns whether the vehicle has started colliding with agent car this tick. Updates _agentCollided
speed

The virtual forward speed of the vehicle, in metres per second

Getter:Get the vehicle speed
Setter:Set the vehicle speed in m/s. Also sets the real velocity, which is relative to the user car
tick(dt)[source]

Called regularly to perform update logic. Return False to indicate vehicle is entirely off screen and should be removed

visualiser.vehicles.SPRITE_SCALES = {'car.png': 0.4, 'lorry.png': 0.75, 'police.png': 0.42, 'taxi.png': 0.35, 'truck.png': 0.5, 'van.png': 0.55, 'viper.png': 0.37}

dict mapping obstacle sprites onto their respective float scales

class visualiser.vehicles.Vehicle(game)[source]

Bases: object

A vehicle in the game

Parameters:game – The parent SimulatorVisualiser
_sprite = None

str name of the sprite file, relative to the sprites/obstacles directory

_spriteScale = None

(float) Size multiplier of the sprite

colliding(veh)[source]

Return whether this vehicle is colliding with Vehicle veh

draw(canvas)[source]

Draw the vehicle

lane

The current lane of the vehicle

Getter:Get the current lane
Setter:Set the lane of the vehicle. The vehicle is centered in its lane. Will only accept lanes available
Type:int
pos

The position vector of the vehicle

Getter:Get position
Setter:Set the position
Type:Pos
rect = None

A reference to the vehicle’s Rect

speed

The virtual forward speed of the vehicle, in metres per second

Getter:Get the vehicle speed
Setter:Set the vehicle speed in m/s
sprite

The sprite object. Uses _sprite as the file name of the image to load

Getter:Return the sprite
Type:pygame.Surface
tick(dt)[source]

Called regularly to perform update logic. Updates vehicle position using its velocity

visualiser.visualiser module

The visualiser of the simulator - the game the user plays Uses the PyGame library

class visualiser.visualiser.SimulatorVisualiser(windowID)[source]

Bases: object

The visualiser of the simulator, showing a graphical representation of the highway.

Parameters:windowIDstr set as the SDL_WINDOWID environment variable. Used to embed the pygame window into the GUI.
_backgrounds = None

list storing the active BackgroundPiece

_bgHeight = None

(int) Height of the background sprite

_cachedSprites = None

(boolean) Whether all sprites have been cached

_fps = None

(int) Frames per second target

_lastAction = None

(Actions) last action performed

_lastDraw = None

(float) Timestamp of the last time the screen was drawn

_lastSpawn = None

(int) Last timestamp an obstacle vehicle was spawned

_lastTick = None

(float) Timestamp of the last tick() call

_obstacles = None

list storing the currently spawned Obstacle instances

_targetDrawDelay = None

(float) Targeted milliseconds in between screen draws

agentCar = None

The Car controlled by the agent

agentCollisions = None

int indicating the number of agent collisions in the current run

canvas = None

A reference to the Surface for drawing

car = None

The Car controlled by the expert

collisions = None

int indicating the number of expert collisions in the current run

distanceTravelled = None

int indicating the number metres travelled in the current run

doAct(act, agent=False)[source]

Perform Action act

draw()[source]

Updates the canvas Attempts to achieve target FPS by blocking As such, it should run in its own thread so other things can be done in the background Should run in and endless loop to continuously redraw

fps

The current target frames per second

Getter:Get the current FPS
Setter:Set the current FPS. Calculates the necessary target draw interval
Type:int
keyPress(key)[source]

Handle key presses and perform the actions they map onto

lastActionTime = None

float Timestamp when the last action was performed

mode

The mode of the visualiser. See mode for details.

Getter:Return the current set mode
Setter:Set the current mode. Also sets the correct dimensions of the display, doubling the width if in compare mode
Type:int
pause = None

bool indicating whether the visualiser is paused

reset()[source]

Restart the visualiser by resetting properties

sessionTime = None

float indicating the milliseconds since starting the current run

stateVector(agent=False)[source]

Return a dictionary of features: {last action, current lane, distance of obstales in three lanes, offroad} Some features have been ‘pruned’ from our descision tree because they did not affect the accuracy of the tree. These have been commented out for clarity.

tick()[source]

This method tries to run at regular intervals of TickRate milliseconds. Performs update logic of the cars, obstacles, and background using time since the previous tick. Stores the current tick timestamp in _lastTick

togglePause()[source]

Toggle pause state of the game