Game¶
This module provides the main game loop and manages the game state. It initializes the game with the specified parameters, manages players, and handles the game rendering. It is the core of the PyRat game engine, allowing players to interact with the maze and each other.
- class Game.Game(random_seed=None, random_seed_maze=None, random_seed_cheese=None, random_seed_players=None, maze_width=None, maze_height=None, cell_percentage=None, wall_percentage=None, mud_percentage=None, mud_range=None, fixed_maze=None, nb_cheese=None, fixed_cheese=None, random_maze_algorithm=None, render_mode=None, render_simplified=None, rendering_speed=None, trace_length=None, fullscreen=None, clear_shell_each_turn=None, save_path=None, save_game=None, preprocessing_time=None, turn_time=None, game_mode=None, continue_on_error=None)[source]¶
A game is a class that allows to play a game of PyRat. It is initialized with the parameters of the game. Players should then be added to the game using the
add_player()
method. Finally, thestart()
method should be called to start the game. Once the game is over, it will provide statistics about the game.Set your own parameters to define interesting objectives for the players. However, make sure that parameters make sense together. For instance, you cannot set a number of cheese that is larger than the number of cells in the maze.
To ease creating games, we provide a set of default parameters. These are used when the parameters are not set in the constructor:
- DEFAULT_RANDOM_SEED = None¶
- DEFAULT_RANDOM_SEED_MAZE = None¶
- DEFAULT_RANDOM_SEED_CHEESE = None¶
- DEFAULT_RANDOM_SEED_PLAYERS = None¶
- DEFAULT_MAZE_WIDTH = 15¶
- DEFAULT_MAZE_HEIGHT = 13¶
- DEFAULT_CELL_PERCENTAGE = 80.0¶
- DEFAULT_WALL_PERCENTAGE = 60.0¶
- DEFAULT_MUD_PERCENTAGE = 20.0¶
- DEFAULT_MUD_RANGE = (4, 9)¶
- DEFAULT_FIXED_MAZE = None¶
- DEFAULT_RANDOM_MAZE_ALGORITHM = 'big_holes'¶
- DEFAULT_NB_CHEESE = 21¶
- DEFAULT_FIXED_CHEESE = None¶
- DEFAULT_RENDER_MODE = 'gui'¶
- DEFAULT_RENDER_SIMPLIFIED = False¶
- DEFAULT_RENDERING_SPEED = 1.0¶
- DEFAULT_TRACE_LENGTH = 0¶
- DEFAULT_FULLSCREEN = False¶
- DEFAULT_CLEAR_SHELL_EACH_TURN = True¶
- DEFAULT_SAVE_PATH = '.'¶
- DEFAULT_SAVE_GAME = False¶
- DEFAULT_PREPROCESSING_TIME = 3.0¶
- DEFAULT_TURN_TIME = 0.1¶
- DEFAULT_GAME_MODE_SINGLE_TEAM = 'sequential'¶
- DEFAULT_GAME_MODE_MULTI_TEAM = 'match'¶
- DEFAULT_CONTINUE_ON_ERROR = False¶
- __init__(random_seed=None, random_seed_maze=None, random_seed_cheese=None, random_seed_players=None, maze_width=None, maze_height=None, cell_percentage=None, wall_percentage=None, mud_percentage=None, mud_range=None, fixed_maze=None, nb_cheese=None, fixed_cheese=None, random_maze_algorithm=None, render_mode=None, render_simplified=None, rendering_speed=None, trace_length=None, fullscreen=None, clear_shell_each_turn=None, save_path=None, save_game=None, preprocessing_time=None, turn_time=None, game_mode=None, continue_on_error=None)[source]¶
Initializes a new instance of the class. Any unset parameter will be set to its default value defined in the class.
- Parameters:
random_seed (
int
|None
) – Global random seed for all elements, orNone
for a random value.random_seed_maze (
int
|None
) – Random seed for maze generation, orNone
for a random value.random_seed_cheese (
int
|None
) – Random seed for cheese distribution, orNone
for a random value.random_seed_players (
int
|None
) – Random seed for initial player locations, orNone
for a random value.maze_width (
int
|None
) – Width of the maze (number of cells).maze_height (
int
|None
) – Height of the maze (number of cells).cell_percentage (
float
|None
) – Percentage of accessible cells in the maze (0% = useless maze, 100% = full rectangle).wall_percentage (
float
|None
) – Percentage of walls in the maze (0% = empty, 100% = max walls while connected).mud_percentage (
float
|None
) – Percentage of adjacent cell pairs separated by mud.mud_range (
tuple
[int
,int
] |None
) – Interval of turns needed to cross mud.fixed_maze (
Maze
|dict
[int
,dict
[int
,int
]] |object
|None
) – Fixed maze in any PyRat-accepted representation (Maze
,dict
,numpy.ndarray
, ortorch.tensor
).random_maze_algorithm (
RandomMazeAlgorithm
|None
) – Algorithm to generate the maze.nb_cheese (
int
|None
) – Number of pieces of cheese in the maze.fixed_cheese (
list
[int
] |None
) – Fixed list of cheese locations.render_mode (
RenderMode
|None
) – Method to display the game.render_simplified (
bool
|None
) – IfTrue
, hides non-essential elements in rendering.rendering_speed (
float
|None
) – Controls the speed of the game when rendering.trace_length (
int
|None
) – Maximum trace length to display (GUI rendering only).fullscreen (
bool
|None
) – IfTrue
, renders the game in fullscreen (GUI only).clear_shell_each_turn (
bool
|None
) – IfTrue
, clears the shell each turn (shell rendering only).save_path (
str
|None
) – Path where games are saved.save_game (
bool
|None
) – IfTrue
, saves the game.preprocessing_time (
float
|None
) – Time given to players before the game starts.turn_time (
float
|None
) – Time after which players miss a turn.game_mode (
GameMode
|None
) – Indicates concurrency mode for players.continue_on_error (
bool
|None
) – IfTrue
, continues the game if a player crashes.
- add_player(player, team='', location=StartingLocation.CENTER)[source]¶
Adds a player to the game. In a PyRat game, players are identified by their name, which should be unique. If a player with the same name is added, an error will be raised.
If the target location is not reachable (if there is a hole), the player will be placed at the closest reachable location.
- Parameters:
player (
Player
) – Player to add.team (
str
) – Team of the player.location (
StartingLocation
|int
) – Initial location of the player (fixed index or value of theStartingLocation
enumeration).
- Return type:
None
- reset(keep_players=True, same=True)[source]¶
Resets the game to its initial state. The initial state is defined by the parameters given in the constructor.
If the game was generated with random elements, we will generate new random elements or keep the same ones, depending on the
same
parameter. Ifsame
isTrue
, we will keep the same random seeds that were used to generate the previous game. Ifsame
isFalse
, we will generate new random seeds for the game, based on what was provided in the constructor.Note that even if you set
same
toFalse
but you provided random seeds in the constructor, the game will use these random seeds. Thus, if you declaresame=False
but created a game withrandom_seed=42
, this will be equivalentsame=True
.Another example: if you declare
same=False
but created a game withrandom_seed_maze=42
, the game will happen on the same maze as before, but with a different cheese distribution and random initial player locations.- Parameters:
keep_players (
bool
) – IfTrue
, keeps the players in the game, otherwise removes them.same (
bool
) – IfTrue
, keeps the same random seeds as before, otherwise generates new random seeds.
- Return type:
None