This project implements a complete Flappy Bird game with AI using reinforcement learning. It features both a web-based version with optimized Q-learning and a Python version with advanced Deep Q-Network (DQN) implementation.
- Double-click
launch_training.htmlto open the launcher - Click "🚀 Start Training" button
- Select "Let AI Play" to watch the AI learn
- Watch the AI master the game in real-time!
- Open
web/index.htmlin any modern web browser - Choose your mode:
- "Play Yourself": Manual control (Spacebar/Click to jump)
- "Let AI Play": Watch AI training with live statistics
- No installation required! Everything runs in your browser
- ✅ Consistent pipe pattern: Same sequence every time for faster learning
- ✅ No randomness: Predictable map layout for pattern recognition
- ✅ Enhanced Q-learning: Optimized hyperparameters for rapid convergence
- ✅ Real-time visualization: Live training statistics and progress tracking
- 📊 Episode counter and training progress
- 🎯 Exploration rate (epsilon) tracking
- 📈 Average & best scores with progress bar
- 🧠 Learning rate and training status
- 💾 Auto-save: Progress saved in browser storage
- Episodes 1-25: Learning basic controls (scores 0-3)
- Episodes 25-75: Pattern recognition (scores 3-8)
- Episodes 75-150: Consistent performance (scores 8-15)
- Episodes 150+: Mastery level (scores 15-30+)
# Install required packages
pip install pygame tensorflow numpy matplotlib
# Or install from requirements file
pip install -r python/requirements.txt# Ultra-fast GPU training (recommended)
python python/fast_train.py
# Standard training with visualization
python python/train.py --episodes 1000
# Training without rendering (fastest)
python python/train.py --episodes 2000 --no-render# Watch trained AI play
python python/play.py --mode ai
# Play manually yourself
python python/play.py --mode manual
# Test specific model
python python/fast_train.py testflappy-bird-ai/
├── 🚀 launch_training.html # Quick launcher for web version
├── 📁 web/ # Web-based AI training
│ ├── index.html # Main game interface
│ ├── game.js # Game engine with fixed patterns
│ ├── ai.js # Optimized Q-learning AI
│ ├── main.js # Application controller
│ └── style.css # Enhanced UI styling
├── 📁 python/ # Advanced Python implementation
│ ├── flappy_env.py # PyGame environment
│ ├── dqn_agent.py # Deep Q-Network with GPU support
│ ├── fast_train.py # GPU-optimized training
│ ├── train.py # Standard training script
│ ├── play.py # Testing and play script
│ └── requirements.txt # Python dependencies
└── 📄 README.md # This file
- Launch: Open
launch_training.htmlorweb/index.html - Train: Click "Let AI Play" and watch the learning process
- Monitor: Observe real-time statistics and progress
- Play: Switch to "Play Yourself" to try manual control
- Install: Run
pip install -r python/requirements.txt - Train: Execute
python python/fast_train.pyfor GPU training - Monitor: Watch training plots and statistics
- Test: Run
python python/play.py --mode aito see results
- Algorithm: Enhanced Q-learning with pattern recognition
- State Space: 9 features including bird position, pipe gaps, velocity
- Action Space: Jump (1) or Do Nothing (0)
- Learning Rate: 0.3 (optimized for fixed patterns)
- Memory: 2000 experience replay buffer
- Exploration: Epsilon-greedy with decay (0.8 → 0.05)
- Architecture: Neural network with batch normalization
- GPU Support: TensorFlow GPU acceleration
- State Space: 9 normalized features with lookahead
- Experience Replay: 20,000 experience buffer
- Target Network: Double DQN with periodic updates
- Optimization: Adam optimizer with learning rate scheduling
- Training Speed: ~50-100 episodes for basic competency
- Target Performance: Average score >15 in 50 episodes
- Best Scores: 20-40+ points achievable
- Training Time: 5-15 minutes for good performance
- Training Speed: ~200-500 episodes for mastery
- Target Performance: Average score >25 in 50 episodes
- Best Scores: 50-100+ points achievable
- Training Time: 10-30 minutes (with GPU)
- +10: Successfully passing through a pipe
- +2: Being close to gap center
- +1: Good positioning near pipes
- +0.1: Survival bonus per frame
- -1: Poor positioning (too high/low)
- -100: Collision with pipe or ground
// In web/ai.js - modify these values:
this.learningRate = 0.3; // How fast AI learns
this.epsilon = 0.8; // Initial exploration rate
this.epsilonDecay = 0.998; // Exploration decay rate# In python/dqn_agent.py - modify these values:
self.learning_rate = 0.001 # Neural network learning rate
self.epsilon = 0.95 # Initial exploration rate
self.batch_size = 128 # Training batch size// In web/game.js - modify fixed pattern:
const fixedPattern = [200, 150, 250, 180, 220, 160, 240, 190, 210, 170];
// Change these numbers to adjust pipe heights- AI not learning: Refresh page and try again
- Slow performance: Close other browser tabs
- No statistics: Ensure JavaScript is enabled
- Reset training: Clear browser storage/cookies
# Common fixes:
pip install --upgrade tensorflow pygame numpy
# For GPU issues:
pip install tensorflow-gpu
# For memory errors:
# Reduce batch_size in dqn_agent.py
# For slow training:
python python/fast_train.py # Use optimized version- Progress Bar: Shows mastery level (target: 100%)
- Training Status: Color-coded learning phases
- Episode Counter: Total training episodes completed
- Best Score: Highest score achieved so far
- Real-time plots: Training curves and statistics
- Console logs: Episode scores and performance metrics
- Model checkpoints: Automatic saving of best models
- Performance analysis: Detailed training summaries
- ✅ AI consistently scores 5+ points
- ✅ Passes through 3+ pipes regularly
- ✅ Training completes in <100 episodes
- ✅ AI consistently scores 20+ points
- ✅ Passes through 10+ pipes regularly
- ✅ Achieves mastery in <500 episodes
- Web Version: Let it run continuously for best learning
- Python Version: Use GPU acceleration when available
- Both: Monitor the statistics to track improvement
- Patience: AI learning takes time - watch the progress!
- Experimentation: Try adjusting hyperparameters for different results
For immediate results: Open launch_training.html and click "Start Training"!
For advanced training: Run python python/fast_train.py after installing dependencies.
Watch your AI evolve from random flapping to masterful gameplay! 🚀
- Web: Spacebar or mouse click to jump
- Python: Spacebar to jump, ESC to quit
- The AI learns automatically through trial and error
- Training progress is displayed in real-time
- Models are saved periodically during training
- State Space: Bird position, pipe positions, velocity, distance
- Action Space: Do nothing (0) or jump (1)
- Learning: Q-table with discretized states
- Memory: Experience replay buffer (1000 experiences)
- Neural Network: 3 hidden layers (128, 128, 64 neurons)
- State Space: 5 normalized features (bird_y, gap_top, gap_bottom, velocity, distance)
- Experience Replay: 10,000 experience buffer
- Target Network: Updated every 100 training steps
- Optimization: Adam optimizer with learning rate 0.001
The AI typically shows improvement patterns:
- Episodes 0-100: Random exploration, low scores
- Episodes 100-500: Learning basic survival, scores 1-10
- Episodes 500-1000: Consistent pipe passing, scores 10-30
- Episodes 1000+: Advanced play, scores 30-100+
- index.html: Game interface with mode selection and statistics
- game.js: Core game engine with bird physics, pipe generation, collision detection
- ai.js: Q-learning implementation with epsilon-greedy policy
- main.js: Application controller managing game loop and AI integration
- style.css: Modern responsive design with animations
- flappy_env.py: Complete game environment implementing OpenAI Gym interface
- dqn_agent.py: DQN agent with neural network, experience replay, and target network
- train.py: Training script with progress visualization and model saving
- play.py: Testing script for both manual and AI play modes
- Modify
updateDifficulty()ingame.jsor_update_difficulty()inflappy_env.py - Change pipe speed, gap size, or spawn intervals
- Web: Edit learning rate, epsilon decay, memory size in
ai.js - Python: Modify network architecture, learning rate, batch size in
dqn_agent.py
- Adjust rewards in
getReward()(web) or_calculate_reward()(python) - Try different reward structures for faster learning
- Ensure JavaScript is enabled in your browser
- Use a modern browser (Chrome, Firefox, Safari, Edge)
- Check browser console for any error messages
- Install all dependencies:
pip install -r python/requirements.txt - Ensure Python 3.7+ is installed
- For GPU acceleration, install
tensorflow-gpu
- Slow training: Use
--no-renderflag for faster training - Memory issues: Reduce batch size or memory size in agent
- Poor performance: Try adjusting learning rate or network architecture
- Web: The AI learns faster with consistent play sessions
- Python: Use GPU acceleration for faster neural network training
- Both: Adjust epsilon decay rate for better exploration/exploitation balance
- Training: Start with smaller episode counts to test setup
This implementation provides a complete learning environment for understanding reinforcement learning concepts while enjoying the classic Flappy Bird game!