I Like Trains is a multiplayer game where players take on the role of train operators, navigating a shared game world to collect passengers, strategically expand their trains, and skillfully avoid collisions. Built with Python and Pygame, the game employs a client-server architecture to enable networked gameplay, offering a blend of strategic decision-making and real-time reactions.
The student's objective will be to modify the agent.py file (and only this one) to remotely control a train managed by a server according to his environment. The agent must make travel decisions for the train, as well as the game board with the Pygam Library. The student will also have to edit the draw_passengers() and draw_trains() functions in client/renderer to display the passengers and trains.
The project is divided into two main parts:
The server is responsible for managing client connections and game synchronization. It is executed on a distant machine which the student is connecting to. The server files are included here so the student can have a better understanding of how the management of the game works.
server.py
: Manages client connections and game synchronizationgame.py
: Contains the main game logictrain.py
: Defines the Train class and its behaviorspassenger.py
: Manages passenger logic
The client is responsible for managing the game display and user interactions. It is executed on the student's machine when executing main.py
.
client.py
: Manages server connection and the main game loopnetwork.py
: Manages network communication with the serverrenderer.py
: Responsible for the graphical display of the gameevent_handler.py
: Manages events (keyboard, mouse)game_state.py
: Maintains the game state on the client sideagent.py
: Controls the player's train behaviorui.py
: Manages the user interface
Communication between the client and server is done via TCP/IP sockets:
- The client connects to the distant server (by default on localhost:5555)
- The client sends its agent name to the server
- The server regularly sends the game state to clients
- Clients send their actions (directions) to the server
- The server updates the game state and the cycle continues
As a student, you must implement two main components:
You must implement an intelligent agent that controls your train. The main method to implement is:
def get_direction(self, game_width, game_height):
"""
This method is regularly called by the client to get the next direction of the train.
It must return a valid direction that avoids walls and collisions.
"""
Helper functions are available in the Agent class:
will_hit_wall()
: Checks if the next position will hit a wallwill_hit_train_or_wagon()
: Checks if the direction leads to a collisionget_closest_passenger()
: Finds the closest passengerget_direction_to_target()
: Determines the best direction to reach a target
The agent can also call the method self.network.send_drop_passenger_request()
to send a request to the server to drop a passenger.
The train will then get a 0.25sec *1.5 speed boost and enter a 10sec boost cooldown. Calling this method will drop one passenger from the train (costing 1 point from the train's score).
You must implement the display of trains and passengers in the renderer. The two methods to implement are:
def draw_trains(self):
"""
Draws all trains and their wagons.
Tip: Use train_data.get("position", (0, 0)) to access a train's position
"""
def draw_passengers(self):
"""
Draws all passengers on the grid.
Tip: Use passenger["position"] to access a passenger's position
"""
- Screen size: 600x400 pixel
- Game screen size: 200x200 pixel
- Grid size: 20 pixels
- Possible directions: Up, Right, Down, Left
- Automatic respawn after collision (configurable)
- Scoreboard displayed on the right side of the screen
-
For the agent:
- Start with a simple strategy (e.g., go towards the closest passenger)
- Gradually add obstacle avoidance (other trains and wagons)
- Consider handling cases where the direct path is blocked
-
For the renderer:
- Ensure trains and passengers are clearly visible
- Consider the orientation of trains based on their direction
- Each train has a color by default, consider using dark blue to display yours (not used by default)
- > Python 3.10
Follow these steps to set up and run the game:
After cloning the project and entering the folder with cd .\i_like_trains\
, enter:
python -m venv venv
.\venv\Scripts\activate
source venv/bin/activate
After activating the virtual environment, install the necessary dependencies:
pip install -r requirements.txt
The student can start a local server by executing python server/server.py
. This will start a server on the default port (5555).
The student can then open another terminal and execute python main.py
to connect to the local server. This is optional, but recommended for testing before connecting to the distant server.
To execute the client and connect to the server. Replace <ip_adress>
with the IP address of the server (do not enter an IP address if you are connecting to the local server hosted on your machine).
python main.py <ip_adress>
- Launch the client:
python main.py <ip_adress>
- Enter your player name
- Wait in the waiting room until all players are connected
- Press SPACE to start the game when all players are ready
- Your agent will automatically control your train
- The goal is to collect as many passengers as possible while avoiding collisions
The game uses Python's built-in logging system to help with debugging and monitoring. Change the logging level in the logging.basicConfig
function at the beginning of each file from which you want to follow the logs.
Available log levels (from most to least verbose):
- DEBUG: Detailed information for debugging
- INFO: General information about game operation
- WARNING: Indicates potential issues
- ERROR: Serious problems that need attention
- CRITICAL: Critical errors that prevent the game from running
Logs are displayed in the console and include timestamps, module name, and log level.