This project is a complete implementation of the classic Snake game, built from scratch using Python and Pygame. The primary goal is to train an Artificial Intelligence (AI) agent using Deep Q-Learning (DQN) to play the game autonomously and achieve the highest possible score.
- Complete Snake Game: Built from the ground up with Pygame, including all core game mechanics.
- DQN Agent: A Reinforcement Learning agent that learns from experience to master the game.
- Advanced State Representation: The agent uses a 19-feature state vector for broad spatial awareness, including immediate dangers, a map of its surroundings, and food location.
- Experience Replay: Implements a memory buffer to store and sample past experiences, stabilizing the learning process.
- Live Training Visualization: The training loop is accompanied by a real-time plot to monitor scores and their moving average.
- Optimized for Speed: The training loop is decoupled from human-viewable speed, allowing the agent to learn rapidly.
- Python 3
- Pygame for the game engine and graphics.
- PyTorch for building and training the neural network model.
- Matplotlib & IPython for live data visualization.
- NumPy for state representation.
To run this project on your local machine, follow these steps:
-
Clone the Repository
git clone [https://github.com/username/RL-Snake-Pygame.git](https://github.com/username/RL-Snake-Pygame.git) cd RL-Snake-Pygame(Replace
usernamewith your GitHub username.) -
Create and Activate a Virtual Environment
# Create the venv python -m venv venv # Activate on Windows .\venv\Scripts\activate # Activate on macOS/Linux source venv/bin/activate
-
Install Dependencies Use the
requirements.txtfile to install all necessary libraries.pip install -r requirements.txt
The project has two execution modes:
-
Train the AI Agent (Default Mode) Run the
agent.pyfile to start the training process.python agent.py
You will see the game window where the AI plays at high speed, and a plot will appear to track its progress.
-
Play as a Human If you want to try the game yourself, run the
game.pyfile.python game.py
.
├── agent.py # Main file for training the AI agent
├── game.py # Core logic and engine for the Snake game
├── model.py # Neural Network (Linear_QNet) and Trainer (QTrainer) definitions
├── plot.py # Script for plotting visualization
├── requirements.txt # List of project dependencies
├── LICENSE # MIT License
└── README.md # This documentation
