This project implements an AI agent to play the classic Snake game using Deep Q-Learning (DQN), a reinforcement learning algorithm. The game environment is built using PyGame, and the AI is trained using PyTorch.
Can an AI agent learn to play and master the classic Snake game using reinforcement learning techniques?
This project explores how Deep Q-Learning can be used to teach an AI agent to:
- Navigate the game grid
- Eat food to increase score
- Avoid collisions with walls and its own body
- Maximize performance through training
- Snake moves in a grid environment.
- Eats food to grow and increase score.
- Dies on collision with wall or itself.
- The agent must learn efficient and safe navigation.
- DQN is a value-based reinforcement learning algorithm.
- The agent uses a neural network to approximate Q-values.
- Key components:
- State: 11-dimensional binary vector representing current game state
- Action: [straight, right, left]
- Reward:
- +10 for eating food
- -10 for dying
- -0 per step (to encourage faster solutions)
- Experience Replay: Stores past experiences and trains on batches
- Target Network: Helps stabilize learning
- Balances exploration vs exploitation.
- Starts with high ε (exploration), gradually decays over time.
- Random action with probability ε.
- Best-known action with probability 1 - ε.
- Ensures the agent explores enough early on and exploits learned knowledge later.
| Technology | Purpose |
|---|---|
| Python | Programming language |
| PyTorch | Neural network and deep learning |
| PyGame | Game development and environment |
| NumPy | Numerical operations |
| Matplotlib | Plotting training graphs |
- Game Environment (PyGame)
- State Representation (11 features)
- Neural Network Model (PyTorch)
- Experience Replay Buffer
- Epsilon-Greedy Policy for action selection
- Q-value Calculation using Bellman Equation
- Network Optimization (Backpropagation)
- Performance Visualization (Matplotlib)
- Create the Snake game using PyGame.
- Represent the game state using an 11-dimensional binary vector.
- Build the Deep Q-Network using PyTorch.
- Use Epsilon-Greedy strategy for exploration.
- Implement Experience Replay and train with Q-learning updates.
- Plot training performance to visualize learning.
.
├── agent.py # Contains DQNAgent class for decision-making
├── model.py # Neural network definition using PyTorch
├── game.py # Snake game environment using PyGame
├── train.py # Training loop and performance tracking
├── utils.py # Helper functions (optional)
├── screenshots/ # Graphs and game visuals
├── requirements.txt # List of dependencies
└── README.md # Project documentation