game¶
| author: | Laurens Koppenol |
|---|
The game engine for a 2D racing game, using the pygame library. Built to demonstrate the concept behavioral learning.
Custom track can be built in paint, please see How it works for more information.
-
class
game.Engine(environment, players, headless=False)[source]¶ The game engine is responsible for the game logic per turn, listening game events (key inputs) and drawing the game. Can be kicked off by calling engine.play(), which repeatedly calls engine._turn().
-
add_player(player)[source]¶ Add a player to the game while the game is running. :param player: instance of player.Player :return: self
-
bind_action(key, action)[source]¶ Bind an action to a pygame key. Example usage:
> game_engine.bind_action(pygame.K_a, lambda: print(123)) prints 123 when ‘a’ is pressed during the game.
Parameters: - key – pygame key index
- action – function to call
Returns:
-
get_best_player()[source]¶ Get the player object of the player with the lowest score (furthest in the race)
Returns: player.Player object
-
get_player(player_id)[source]¶ Get the player object that corresponds with given player_id
Parameters: player_id – integer Returns: player.Player object
-
get_worst_player()[source]¶ Get the player object of the player with the highest score (furthest from the finish)
Returns: player.Player object
-
-
class
game.Environment(track)[source]¶ Object that takes a png image and transforms it into a racing track. To create a new track take the following into account: Walls must have red-channel >= 128 Starting position is the first pixel with green-channel >= 128 Finish are all pixels with blue-channel >= 128
To determine score all pixels that are reachable from the finish are given their manhattan distance to the nearest finish point.
This class also contains environment related helper functions.
-
check_collision(player)[source]¶ check if a given player is colliding with a boundary (red pixel)
Parameters: player – subclass of Player Returns: True or False
-
get_distance(player)[source]¶ Check how many pixels (manhattan distance) a player is located from the finish
Parameters: player – subclass of Player Returns: integer, 0 for wall, 1 for finish, > 1 for anything else
-
get_distance_matrix()[source]¶ Generate the distance map with manhattan distances to the finish by calling the private recursive_distance function.
Returns: numpy ndarray with distances
-
static
location_to_pixel(coordinate)[source]¶ Round a coordinate to integer pixel coordinates
Parameters: coordinate – (x, y) Returns: (int, int)
-
static
parse_track(track_img)[source]¶ Extract track boundaries, start and finish from a png-pillow image.
Parameters: track_img – png-pillow image Returns: boundaries, finish and start as Numpy ndarrays
-
ray_trace_to_wall(position, angle, distance)[source]¶ Use the bresenham algorithm to find the nearest wall over a angle and distance, returns None if no wall found. See the bresenham module for more information about the algorithm.
Parameters: - position – origin (x, y)
- angle – angle in degrees
- distance – int or float
Returns: distance to nearest wall or None
-
static
translate(position, distance, rotation, pixel=False)[source]¶ Translate a coordinaten given a distance and rotation. Can round to integer pixel coordinates
Parameters: - position – origin (x, y)
- distance – int or float
- rotation – angle in degrees
- pixel – whether to round to integer pixel coorindates
Returns: new position (x, y)
-