A beautiful, modern implementation of the classic Snake game with an AI agent that learns to play using Q-Learning reinforcement learning. Features a professional training dashboard with real-time statistics and performance monitoring.
- ๐ค Q-Learning AI Agent - Learns to play Snake through trial and error
- ๐จ Beautiful Modern Interface - Professional training dashboard with real-time stats
- ๐ Real-time Performance Monitoring - Track learning progress, exploration rate, and performance trends
- ๐พ Model Persistence - Save and load trained models
- ๐ฎ Interactive Controls - Pause, reset, and save during training
- ๐ Visual Learning Analytics - Progress bars, trend indicators, and performance metrics
The interface features:
- ๐ฎ Large, clear game area with enhanced graphics
- ๐ Real-time statistics panel showing score, episodes, and learning progress
- ๐ Exploration rate visualization with progress bars
- ๐ Performance trend indicators (Improving/Declining/Stable)
- โฑ๏ธ Training progress tracking
- ๐ Modern dark theme with professional color scheme
- ๐ Python 3.7 or higher
- ๐ฆ pip package manager
Install the required libraries:
pip install pygame numpyOr install them individually:
pip install pygame
pip install numpy# Create virtual environment
python -m venv snake_ai_env
# Activate it (Windows)
snake_ai_env\Scripts\activate
# Activate it (macOS/Linux)
source snake_ai_env/bin/activate
# Install dependencies
pip install pygame numpypython snake_ai.py- โธ๏ธ SPACE - Pause/Resume training
- ๐ R - Reset training (clear all progress)
- ๐พ S - Save model manually
- โ ESC - Exit application
On first run, the AI will start learning from scratch. The model will be automatically saved as snake_q_table.pkl every 100 episodes and when you exit the program.
If a saved model (snake_q_table.pkl) exists in the same directory, it will be automatically loaded when you start the program.
The AI uses Q-Learning, a model-free reinforcement learning algorithm that learns the quality of actions (Q-values) for different states:
-
๐ฏ State Representation: The game state includes:
โ ๏ธ Danger detection (walls and snake body in each direction)- ๐ Food direction relative to snake head
- โก๏ธ Current movement direction
- ๐ This creates a 12-dimensional boolean state space
-
๐ฎ Actions: The snake can take 3 actions:
- โฌ๏ธ Continue straight (0)
โ๏ธ Turn right (1)โ๏ธ Turn left (2)
-
๐ Rewards:
- โ +10 for eating food
- โ -10 for collision (game over)
- โฐ -0.1 for each step (encourages efficiency)
- ๐ -5 for taking too long without eating (prevents infinite loops)
-
โ๏ธ Learning Parameters:
- ๐ Learning rate (ฮฑ): 0.1
- ๐ฎ Discount factor (ฮณ): 0.95
- ๐ฒ Exploration rate (ฮต): Starts at 1.0, decays to 0.01
- ๐ฒ Exploration Phase: Initially, the AI explores randomly (high ฮต value)
- ๐ Learning Phase: As training progresses, the AI gradually exploits learned knowledge
- ๐ฏ Convergence: Eventually, the AI develops optimal strategies for playing Snake
snake_ai/
โ
โโโ ๐ snake_ai.py # Main game and AI implementation
โโโ ๐พ snake_q_table.pkl # Saved Q-table (created after training)
โโโ ๐ธ screenshot.png # Project screenshot
โโโ ๐ README.md # This file
๐ฎ SnakeGame- Game logic and environment๐ค QLearningAgent- AI agent with Q-learning implementation๐จ GameRenderer- Modern UI and visualization
get_state()- Converts game situation to state representationchoose_action()- Epsilon-greedy action selectionupdate()- Q-value updates using Bellman equationrender()- Beautiful dashboard rendering
The dashboard displays several key metrics:
- ๐ Score - Current game score
- ๐ฌ Episode - Number of games played
- ๐ง States - Number of unique states learned
- ๐ฒ Exploration Rate - Current ฮต value (exploration vs exploitation)
- ๐ Average Score - Running average of last 100 games
- ๐ Best Score - Highest score achieved
- ๐ Performance Trend - Whether AI is improving, declining, or stable
- ๐ฑ Initial Learning: The first few hundred episodes will show poor performance as the AI explores
- ๐ Improvement Phase: Around episodes 500-1000, you should see steady improvement
- ๐ฏ Convergence: After 1000+ episodes, performance should stabilize at a high level
- โณ Patience: Q-Learning can take time to converge - let it run for several thousand episodes
In the QLearningAgent class constructor:
agent = QLearningAgent(
alpha=0.1, # ๐ Learning rate
gamma=0.95, # ๐ฎ Discount factor
epsilon=1.0, # ๐ฒ Initial exploration rate
epsilon_decay=0.995, # ๐ Exploration decay rate
epsilon_min=0.01 # ๐ฏ Minimum exploration rate
)In the constants section:
GRID_SIZE = 40 # ๐ Size of each grid cell
GRID_WIDTH = 10 # โ๏ธ Number of cells horizontally
GRID_HEIGHT = 10 # โ๏ธ Number of cells vertically
WINDOW_WIDTH = 1200 # ๐ฅ๏ธ Window width
WINDOW_HEIGHT = 700 # ๐ฅ๏ธ Window heightThe GameRenderer class contains all visual settings including colors, fonts, and layout parameters.
- ๐ป Language: Python 3.7+
- ๐ฎ Graphics: Pygame
- ๐ค AI Algorithm: Q-Learning (Tabular)
- ๐ State Space: Discrete (2^12 possible states)
- ๐ฏ Action Space: Discrete (3 actions)
- ๐พ Model Persistence: Pickle format
- ๐ฆ Module not found: Make sure pygame and numpy are installed
- ๐ Slow performance: Try reducing
render_everyparameter or window size - ๐ No improvement: Increase training episodes or adjust learning parameters
- ๐พ Save/load errors: Ensure write permissions in the project directory
- ๐ฅ๏ธ Reduce
render_everyto update display less frequently - ๐ฑ Decrease window size for faster rendering
- โก Adjust
epsilon_decayfor faster convergence
This project is open source and available under the MIT License.
- ๐ Built with Python and Pygame
- ๐ค Uses Q-Learning reinforcement learning algorithm
- ๐จ Modern UI design inspired by professional ML dashboards
Feel free to fork this project and submit pull requests for improvements. Some areas where contributions would be welcome:
- ๐ง Additional AI algorithms
- โก Performance optimizations
- ๐จ UI/UX improvements
- ๐ Better state representations
- ๐ Documentation improvements
๐ Happy Learning! Watch your AI master the game of Snake through the power of reinforcement learning! ๐๐ค
