โญ Star this repository if you find it interesting! โญ
๐ Quick Start โข ๐ Documentation โข ๐งฌ How It Works โข ๐ Results
Watch AI birds evolve from random chaos to expert Flappy Bird gameplay using neural networks and genetic algorithms!
This project implements an AI that learns to play Flappy Bird using:
- ๐ง Neural Networks for decision making
- ๐งฌ Genetic Algorithm for evolution
- ๐ฎ Pygame for game simulation
- ๐ Real-time visualization of learning progress
No training data needed! The AI learns purely through trial and error, just like biological evolution.
Note: Add your gameplay GIFs/videos here to showcase the evolution!
- Python 3.8+
- pip package manager
# Clone the repository
git clone https://github.com/antilneeraj/geneticalgorithm.git
cd geneticalgorithm
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Watch AI learn to play (recommended)
python main.py --mode ai_training --population 50
# Play yourself
python main.py --mode human
# Watch trained AI play
python main.py --mode ai_play
# Run diagnostics
python diagnostic_ai_debug.py| Key | Action |
|---|---|
SPACE |
Jump (Human mode) / Start game |
R |
Restart game |
P |
Pause/Unpause |
D |
Toggle debug info |
ESC |
Quit game |
Each bird has a neural network with:
- Input: Bird position, velocity, pipe distance, gap location
- Hidden Layers: 6 โ 4 neurons with tanh activation
- Output: Jump decision (sigmoid activation)
Generation 1: 50 random birds โ Most die quickly
โ
Fitness Evaluation: Survival time + Score + Bonuses
โ
Selection: Keep top 5 performers (elitism)
โ
Reproduction: Crossover + Mutation
โ
Generation 2: Improved birds โ Better performance
โ
Repeat for 100+ generations...
โ
Result: Expert-level AI players!
fitness = survival_time * 1.0 + score * 100 + bonuses- Survival bonus: +1 point per frame alive
- Pipe bonus: +100 points per pipe passed
- Death penalty: -50 points for crashing
| Generation | Best Score | Avg Survival | Best AI Behavior |
|---|---|---|---|
| 1-5 | 0 | 30-80 frames | Random chaos |
| 10-20 | 1-2 | 200-400 frames | Basic navigation |
| 30-50 | 3-8 | 500-800 frames | Smart pipe avoidance |
| 50+ | 10+ | 1000+ frames | Expert gameplay |
- Population Size: 50 birds per generation
- Training Time: ~2-5 minutes per generation
- Convergence: Expert level in ~50 generations
- Success Rate: 95%+ birds learn to score points
geneticalgorithm/
โโโ src/
โ โโโ game/ # Game engine and components
โ โ โโโ bird.py # Bird class with AI integration
โ โ โโโ pipe.py # Pipe generation and collision
โ โ โโโ game_engine.py # Main game loop and AI training
โ โ โโโ renderer.py # Graphics and UI rendering
โ โโโ ai/ # AI and machine learning components
โ โ โโโ neural_network.py # Neural network implementation
โ โ โโโ genetic_algorithm.py # Evolution logic
โ โ โโโ fitness.py # Fitness evaluation
โ โโโ utils/ # Utilities and configuration
โ โโโ constants.py # Game and AI parameters
โ โโโ asset_loader.py # Resource management
โโโ assets/ # Game sprites, sounds, fonts
โโโ data/ # Training data and saved models
โโโ main.py # Entry point
โโโ requirements.txt # Dependencies
โโโ README.md # This file
Customize training parameters in src/utils/constants.py:
# Genetic Algorithm Settings
POPULATION_SIZE = 50 # Number of birds per generation
GENERATIONS = 100 # Maximum generations
MUTATION_RATE = 0.1 # Probability of mutation
CROSSOVER_RATE = 0.8 # Probability of crossover
ELITE_COUNT = 5 # Top performers to preserve
# Neural Network Architecture
NN_INPUT_NODES = 4 # Game state inputs
NN_HIDDEN_NODES = [6, 4] # Hidden layer sizes
NN_OUTPUT_NODES = 1 # Jump decision output
# Fitness Parameters
FITNESS_BONUS_PIPE = 100 # Points per pipe passed
FITNESS_BONUS_DISTANCE = 1 # Points per frame survived
FITNESS_PENALTY_DEATH = -50 # Penalty for dying# Test neural network diversity
python diagnostic_ai_debug.py
# Validate AI components
python validate_ai_constants.py
# Run performance benchmarks
python benchmarks.py- โ Real-time AI training visualization
- โ Multiple game modes (Human, AI Training, AI Play)
- โ Configurable parameters for experimentation
- โ Performance analytics and statistics
- โ Save/load trained models
- โ Debug mode with detailed metrics
- โ Professional logging system
- โ Cross-platform compatibility
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
python -m pytest tests/
# Format code
python -m black src/
python -m isort src/Common Issues & Solutions
Issue: Birds not learning / identical behavior
# Run diagnostics to check neural network diversity
python diagnostic_ai_debug.pyIssue: Game crashes on startup
# Check pygame installation
pip install --upgrade pygameIssue: Poor AI performance
# Try adjusting parameters in constants.py
MUTATION_RATE = 0.15 # Increase for more diversity
POPULATION_SIZE = 100 # Larger populationIssue: Slow training
# Reduce population size for faster iterations
POPULATION_SIZE = 20This project is licensed under the MIT License - see the LICENSE file for details.
- Pygame Community for the excellent game development framework
- NEAT Algorithm inspiration for neural network evolution
- Flappy Bird original game concept by Dong Nguyen
- AI Research Community for genetic algorithm innovations
- GitHub Issues: Report bugs or request features
- Discussions: Ask questions or share ideas
โญ Don't forget to star this repository if you found it useful! โญ
Made with โค๏ธ and lots of โ by Neeraj Antil