PyRat API¶
This section gives the documentation for functions and classes in the PyRat library.
All modules below are part of the PyRat library and can be used to create and manipulate mazes, players, etc.
To use any of them, you can import them as follows: from pyrat import <class_name>.
# A typical import line in one of your programs
from pyrat import Game, Player, Action, GameState, Maze
The big picture¶
The diagram below shows all the classes of the library, which class inherits from which, and how the game, the players, the mazes and the rendering engines fit together.
Your own players (in green) are the only classes you write: they inherit from Player, receive a Maze and a GameState at each turn, and return an Action.
Tip
The diagram is interactive: hover it to magnify the area under the cursor, click on the name of a class to open its documentation page, or on one of its attributes or methods to jump directly to its description. You can also open the diagram at full size in a new tab.
Tip
The diagram is interactive: hover it to magnify the area under the cursor, click on the name of a class to open its documentation page, or on one of its attributes or methods to jump directly to its description. You can also open the diagram at full size in a new tab.
How to read the diagram
The diagram follows the conventions of UML class diagrams. If you are not familiar with them, here is all you need to know.
Each box is a class, with three compartments: its name, its attributes, and its methods.
A name in italics denotes an abstract class, that cannot be instantiated directly, and an italic method is a method that subclasses must implement.
Boxes marked «enumeration» list constants, that you use as Action.NORTH, RenderMode.GUI, etc.
The symbol before each attribute or method tells who is meant to use it:
+ is public (you can use it), # is protected (meant for subclasses), and - is private (internal to the class).
Methods are listed with the names of their arguments and their return type.
A solid line with a hollow triangle is inheritance: the classes below the line are subclasses of the class at the triangle. For instance,
FixedPlayeris aPlayer.A line with a filled diamond is composition: the
Gamecreates and owns its maze and its rendering engine.A line with a hollow diamond is aggregation: the
Gamegroups players, but you create them yourself and register them withadd_player(). The label at the far end of these lines gives the attribute ofGamethat holds the objects, and how many of them there are.A dashed arrow is a dependency: the class at the tail creates, receives or returns the class at the head.
Orange boxes are the classes of the library, grouped by package in gray frames.
Blue boxes are the enumerations.
The green dashed box stands for the players you write, which inherit from Player.
The gray note describes internal components, that are not part of the public API.
Which class does the game create?
Several settings of Game decide which concrete class is instantiated behind the scenes. The diagram shows all the candidates, and the table below tells which one is chosen.
Setting of |
Value |
Class created |
|---|---|---|
|
|
|
|
||
|
||
|
A dictionary |
|
A |
||
A |
Used as is (a copy is made) |
|
|
|
|
|
ShellRenderingEngine (with or without colors) |
|
|
RenderingEngine (renders nothing) |
The packages of the library¶
The classes of the library are grouped in four subpackages, which also appear as gray frames in the diagram above. Each one has its own page, listing the classes it contains.
gameWhat runs a game: the class you instantiate to start one, what your player is given at each turn, the constants you pass around, and the exceptions raised when something goes wrong.
playersWhat a character in a game is: the class your own programs inherit from, and one ready-made player.
mazesThe place a game is played in: the graph a maze is built upon, the mazes you describe yourself, and the ones PyRat generates at random.
renderingWhat shows a game while it is played, in a window or directly in your terminal.