Workspace API¶
PyRat comes with a workspace that you should use to start working on your games.
It is created with the pyrat-init command that we ask you to run in the installation instructions, which puts it in a pyrat_workspace directory.
Your programs live in that directory, organized around the two directories documented in this section.
Your workspace is added to the directories Python imports from, which is what lets your programs import each other.
An import names the file to read as its path inside your workspace, with a dot instead of each / and without the .py, followed by what you want to take from it.
For instance, this is how the sample_game.py script imports the Random2 class written in players/random2.py:
from players.random2 import Random2
Nothing has to be declared anywhere for this to work, it works wherever you run your program from, and renaming or moving your workspace changes nothing.
The programs that control a character during a game.
A player is a class that inherits from Player, and decides which action to take at each turn.
The scripts that set up a game and make players compete in it. This is what you run to actually see a game.
You can later add more directories to your workspace if you want to organize your programs differently, and they are importable the same way: a class Tools written in utils/tools.py is imported with from utils.tools import Tools.
Your workspace also contains the files used by uv to describe the Python version and the libraries of your project, which you usually do not need to edit by hand.
Finally, it contains a .gitignore file listing what Git should not version (the virtual environment, temporary files, etc.), so that it is ready to be put under version control.
Tip
The programs documented in this section are the ones created in your workspace. Reading their source code, linked on each page, is the fastest way to understand how a PyRat program is written.
The big picture¶
The diagram below shows the programs of your workspace, and how they relate to the PyRat library: the players inherit from Player, and the game scripts create a Game in which they make some of these players compete, using the enumerations of the library to choose their skin and their starting location.
Tip
The diagram is interactive: hover it to magnify the area under the cursor, click on the name of a class or of a script to open its documentation page, or on one of the attributes or methods of a class to jump directly to its description. You can also open the diagram at full size in a new tab. The notation is the one of the class diagram of the library, which explains how to read it.
Tip
The diagram is interactive: hover it to magnify the area under the cursor, click on the name of a class or of a script to open its documentation page, or on one of the attributes or methods of a class to jump directly to its description. You can also open the diagram at full size in a new tab. The notation is the one of the class diagram of the library, which explains how to read it.