A Q-learning agent that learns to play Snake using reinforcement learning.
Learn2Slither is a reinforcement learning project that implements a Q-learning algorithm to teach an AI agent to play the classic Snake game. The agent learns through trial and error, gradually improving its performance over multiple episodes.
- Interactive visual display with pyglet
- Q-learning algorithm implementation
- Save and load trained models
- Adjustable game speed
- Training statistics
- Python 3.14 or newer
- pip package manager
-
Clone the repository
git clone <your-repo-url> cd Learn2Slither
-
Create a virtual environment (recommended)
# Windows python -m venv venv venv\Scripts\activate # Linux/Mac python3 -m venv venv source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
./snake [options]- Space: Pause/Resume
- Left Arrow: Decrease game speed
- Right Arrow: Increase game speed
- Escape: Quit game
In step-by-step mode:
- Space: Advance to next step
| Option | Description | Default |
|---|---|---|
-visual on/off |
Enable or disable game display | on |
-episodes <int> |
Number of training episodes | 1 |
-load <file> |
Load a pre-trained Q-table | None |
-save <file> |
Save the Q-table after training | None |
-dontlearn |
Disable learning (exploitation only) | False |
-step-by-step |
Wait for user input between steps | False |
-help |
Display help message | - |
Train a new agent:
./snake -visual on -episodes 1000 -save models/my_model.csvLoad and watch a trained agent:
./snake -visual on -load models/q_table_100000.csv -episodes 10 -dontlearnStep-by-step debugging:
./snake -visual on -load models/q_table_100000.csv -step-by-step -dontlearnTrain without visual display (faster):
./snake -visual off -episodes 10000 -save models/trained_model.csvConfigurable in config/config.py:

