An advanced AI implementation of Flappy Bird using neural networks and genetic algorithms.
The AI learns to play Flappy Bird through evolutionary learning - a process inspired by natural selection:
- Start with Random Birds: Each bird has a neural network "brain" with random connections
- Play the Game: All birds try to play Flappy Bird simultaneously
- Survival of the Fittest: Birds that survive longer and pass more pipes get higher scores
- Create Next Generation: The best birds' "brains" are combined and mutated to create smarter offspring
- Repeat: Each generation gets better as successful strategies are passed down
- Timing: When to flap to avoid pipes
- Positioning: How to navigate through gaps
- Strategy: Balancing speed and precision
- Adaptation: Adjusting to changing pipe patterns
Within 50 generations, the AI typically masters the game, achieving scores over 1000 points consistently!
The AI typically achieves:
- Generation 1-10: Learning basic controls (0-100 points)
- Generation 10-30: Developing strategy (100-500 points)
- Generation 30-50: Optimizing performance (500-1000 points)
- Generation 50+: Mastering the game (1000+ points consistently)
- Survival Rate: Tracks population health over time
- Decision Confidence: Measures AI certainty in decisions
- Neural Complexity: Monitors brain development
- Genetic Diversity: Ensures population variety
- Learning Progress: Phase-based training advancementWatch as AI agents learn to play the game through evolutionary processes!
- Enhanced Neural Networks: 6-input, 12-hidden-node, 1-output architecture with ReLU and Sigmoid activations
- Advanced Genetic Algorithm: Multiple crossover strategies, adaptive mutation rates, and elitism
- Smart Bird Behavior: Improved decision-making with enhanced input features
- Dynamic Difficulty: Adaptive pipe spacing and speed based on generation performance
- Beautiful Graphics: Gradient backgrounds, detailed pipes, and animated clouds
- Real-time Neural Visualization: Live display of neural network activations
- Performance Tracking: Comprehensive statistics and generation analytics
- Best Bird Highlighting: Visual indicators for top-performing agents
- Advanced Analytics Dashboard: 12+ real-time graphs and visualizations
- Performance Optimized: 3x speed multiplier for faster learning
- Elitism Strategy: Top 10% of birds preserved each generation
- Tournament Selection: Better diversity through competitive selection
- Adaptive Mutations: Mutation rates adjust based on fitness scores
| Key | Action |
|---|---|
| SPACE | Pause/Resume the simulation |
| R | Reset the entire simulation |
| S | Toggle statistics display |
The analytics dashboard provides real-time insights into the AI learning process:
- Live Neural Networks: Watch the AI's "brain" make decisions
- Performance Metrics: Track fitness, survival, and learning progress
- Genetic Analytics: Monitor population diversity and evolution
- Decision Confidence: See how confident the AI is in its choices
- Training Progress: Follow the learning journey through phases
Inputs (6 nodes):
├── Bird Y position (normalized)
├── Bird velocity (normalized)
├── Distance to next gap (normalized)
├── Gap center position (normalized)
├── Height difference from gap center
└── Frames since last flap
Hidden Layer (12 nodes):
└── ReLU activation function
Output (1 node):
└── Flap decision (Sigmoid activation)
The neural network in AI Flappy Bird relies on matrix operations for efficient computation of the bird's decisions. This system uses linear algebra to process inputs through the network layers.
The network consists of two weight matrices that define the connections between neurons:
- Input-Hidden Matrix (6×12): Connects 6 input features to 12 hidden neurons
- Hidden-Output Matrix (12×1): Connects 12 hidden neurons to 1 output neuron
The decision-making happens through matrix multiplication:
Hidden_Activations = Input_Vector × Input_Hidden_Matrix
Hidden_Output = ReLU(Hidden_Activations)
Final_Output = Sigmoid(Hidden_Output × Hidden_Output_Matrix)
Let's walk through a concrete example with sample values:
Input Features (normalized between 0-1):
Bird Y Position: 0.5
Bird Velocity: 0.2
Distance to Gap: 0.8
Gap Center Y: 0.6
Height Difference: 0.1
Frames Since Flap: 0.3
Input Vector (1×6):
[0.5, 0.2, 0.8, 0.6, 0.1, 0.3]
Sample Input-Hidden Weights (6×12 matrix, showing first 3 columns):
┌ ┐
│ 0.12 -0.45 0.78 │
│ 0.34 0.67 -0.23 │
│-0.56 0.89 0.12 │
│ 0.45 -0.34 0.56 │
│ 0.78 0.23 -0.67 │
│-0.12 0.45 0.89 │
└ ┘
Hidden Layer Calculation (first 3 neurons):
Hidden[0] = (0.5×0.12) + (0.2×0.34) + (0.8×-0.56) + (0.6×0.45) + (0.1×0.78) + (0.3×-0.12) = 0.23
Hidden[1] = (0.5×-0.45) + (0.2×0.67) + (0.8×0.89) + (0.6×-0.34) + (0.1×0.23) + (0.3×0.45) = 0.67
Hidden[2] = (0.5×0.78) + (0.2×-0.23) + (0.8×0.12) + (0.6×0.56) + (0.1×-0.67) + (0.3×0.89) = 0.89
After ReLU Activation (negative values become 0):
Hidden_Output = [0.23, 0.67, 0.89, ...] (12 values)
Hidden-Output Weights (12×1 matrix, sample values):
┌ ┐
│ 0.45 │
│-0.23 │
│ 0.67 │
│ 0.12 │
│-0.78 │
│ 0.34 │
│ 0.56 │
│-0.45 │
│ 0.89 │
│ 0.23 │
│-0.67 │
│ 0.12 │
└ ┘
Final Output Calculation:
Raw_Output = Σ(Hidden_Output[i] × Hidden_Output_Weights[i]) = 0.76
Flap_Decision = Sigmoid(0.76) = 0.68
Decision: Since 0.68 > 0.5, the bird decides to flap!
This matrix system allows the AI to process complex relationships between game state variables and make intelligent decisions in real-time.
Through generations, the genetic algorithm evolves these weight matrices:
- Crossover: Combines weight matrices from parent birds
- Mutation: Randomly adjusts matrix values
- Selection: Preserves matrices from high-performing birds
- Population: 150 birds per generation
- Selection: 70% tournament selection, 30% roulette wheel
- Elitism: Top 10% preserved unchanged
- Crossover: Uniform, single-point, and blend strategies
- Mutation: Adaptive rates with Gaussian distribution
- Fitness: Multi-objective function considering survival, distance, and pipes passed
- Early Generations: Random behavior, learning basic survival
- Mid Generations: Developing timing and spatial awareness
- Advanced Generations: Optimized performance with precise control
The AI Flappy Bird features a comprehensive analytics dashboard with 12+ real-time visualizations:
- Fitness Graph: Tracks population fitness over generations
- Survival Rate: Shows percentage of birds surviving each generation
- Performance Histogram: Distribution of scores across the population
- Training Progress: Overall learning progress and phase tracking
- Neural Activity Heatmap: Real-time brain activation patterns
- Decision Confidence: Average confidence in flap decisions
- Complexity Graph: Neural network complexity over time
- Gene Pool Diversity: Genetic diversity metrics
- Species Graph: Population clustering and speciation
- Learning Rate: Adaptive mutation rate tracking
- Diversity Graph: Population diversity measurements
- Confidence Levels: Decision-making confidence trends
- Best Bird Neural Network: Live brain visualization of top performer
- Population Neural Network: Current generation's neural patterns
- Dynamic Statistics: Live updates of all performance metrics
- Xavier weight initialization for better learning
- Numerical stability improvements in activation functions
- Enhanced input feature engineering
- Multi-strategy crossover operations
- Efficient collision detection with margin adjustments
- Dynamic pipe generation based on performance
- Optimized rendering with gradient backgrounds
- Real-time FPS monitoring
- Comprehensive error handling and crash prevention
- Advanced analytics dashboard with 12+ visualizations
- Tournament selection for better diversity
- Adaptive mutation rates based on fitness
- Gaussian mutations for smoother changes
- Multi-objective fitness function
The AI typically achieves:
- Generation 1-10: Learning basic controls (0-100 points)
- Generation 10-30: Developing strategy (100-500 points)
- Generation 30-50: Optimizing performance (500-1000+ points)
- Generation 50+: Mastering the game (1000+ points consistently)
- Speciation Support: Framework for population clustering (future enhancement)
- Neural Visualization: Real-time display of network activations
- Performance Analytics: Detailed statistics and trend tracking
- Dynamic Environments: Adaptive difficulty based on AI performance
- Advanced Dashboard: 12+ real-time graphs and analytics
- Error Recovery: Robust error handling and crash prevention
- Performance Monitoring: FPS tracking and optimization
- Genetic Diversity: Advanced population diversity metrics
- Open
index.htmlin a modern web browser - Watch the AI learn to play automatically
- Use keyboard controls to interact with the simulation
- Observe neural network activations in real-time
- Monitor the analytics dashboard for learning insights
- Advanced Analytics Dashboard: 12+ real-time graphs and visualizations
- Improved Neural Networks: Enhanced architecture with better learning
- Error Recovery: Robust crash prevention and error handling
- Performance Monitoring: Real-time FPS and optimization tracking
- Enhanced UI: Modern design with gradient backgrounds and animations
- Genetic Diversity: Advanced population diversity metrics
- Decision Confidence: Real-time confidence measurements
- Training Progress: Phase-based learning progression tracking
Try modifying these parameters in main.js:
POP_SIZE: Population size (default: 150)MUTATION_RATE: Base mutation rate (default: 0.15)PIPE_GAP: Initial gap size (default: 200)SPEED_MULTIPLIER: Simulation speed (default: 3)GRAVITY: Bird gravity (default: 0.6)JUMP: Bird jump strength (default: -12)
HIDDEN_NODES: Neural network complexity (default: 12)INPUT_NODES: Input features (default: 6)ELITISM_RATE: Elite population percentage (default: 0.1)
- Survive 1000+ frames consistently
- Achieve all-time best scores over 2000
- Maintain stable performance across generations
- Observe emergent intelligent behaviors
Built with: Vanilla JavaScript, HTML5 Canvas, and advanced AI algorithms Inspired by: NEAT (NeuroEvolution of Augmenting Topologies) and genetic programming principles: How the System Works
Welcome! This is a fun project where we teach computers to play Flappy Bird using artificial intelligence. We'll explain everything step by step, like you're learning it for the first time. No fancy words without explanations!
Flappy Bird is a simple game where a bird flies through pipes. The bird falls down due to gravity, and you tap to make it flap up. The goal is to go through as many pipes as possible without hitting them.
In our version, no human plays - the computer controls everything!
AI means making computers think and learn like humans. Here, we use two main ideas:
- Neural Networks - Like a tiny brain inside each bird
- Evolution - Birds get better over time, like animals in nature
Each bird has a tiny "brain" that decides when to flap. Think of it as a decision-making machine.
The brain takes inputs (information) and gives outputs (decisions).
Inputs (4 pieces of information):
- Where the bird is on the screen (up/down)
- How fast the bird is moving (up or down)
- Height of the top pipe
- Height of the bottom pipe
Output (1 decision):
- Flap or don't flap (yes/no)
Inputs → Hidden Layer → Output
4 → 8 → 1
Neural Network Diagram:
Input Layer Hidden Layer Output Layer
[I1] [H1] [O1]
[I2] [H2] (Flap?)
[I3] [H3]
[I4] [H4]
[H5]
[H6]
[H7]
[H8]
Connections: I1→H1, I1→H2, ..., I4→H8, H1→O1, ..., H8→O1
- Input layer: 4 numbers (the information above)
- Hidden layer: 8 "thinking" units
- Output layer: 1 number (0 = don't flap, 1 = flap)
The brain uses simple math to make decisions:
-
Multiplication: Each connection has a "weight" (like importance)
Input × Weight = Signal -
Addition: Add up all signals
Signal1 + Signal2 + Signal3 + ... = Total -
Sigmoid Function: Squish the total into a number between 0 and 1
Sigmoid(x) = 1 / (1 + e^(-x))- If x is big positive: result ≈ 1
- If x is big negative: result ≈ 0
- If x is zero: result ≈ 0.5
Sigmoid Function Graph:
Output
1.0 | .--------
| .'
| .'
0.5 |....'
| .'
| .'
0.0 |.' --------
+-------------------- Input
-3 -2 -1 0 1 2 3
This gives us a number between 0 and 1. If > 0.5, the bird flaps!
Birds start with random brains and get better through "evolution" (like natural selection).
- Generation: A group of 100 birds plays the game
- Fitness: How well each bird did (score = how many pipes passed)
- Selection: Pick the best birds to be "parents"
- Crossover: Mix parents' brains to make babies
- Mutation: Add small random changes
- Repeat: New generation with improved brains
Fitness Calculation:
Fitness = Score × Score
(Better scores get much higher fitness - rewards good birds more)
Selection:
- Calculate total fitness of all birds
- Pick randomly, but better birds more likely to be chosen
- Like a lottery where good birds have more tickets
Crossover:
- Take half the "weights" from mom, half from dad
- Creates a mix of both parents' brains
Mutation:
- 10% chance to change each weight
- Add small random number (-0.1 to +0.1)
- Prevents getting stuck, adds new ideas
Fitness Distribution Chart:
Fitness
High | ████ (Best birds)
| ████████
| ████████████
Low |██████████████████ (Most birds)
+--------------------
Score: 0 → 1000
Selection Process:
Bird A (Score: 100) - Fitness: 10,000
Bird B (Score: 200) - Fitness: 40,000 ← More likely to be picked!
Bird C (Score: 50) - Fitness: 2,500
- Start: 100 birds with random brains
- Play: Each bird uses its brain to decide when to flap
- Die: Birds hit pipes or go off screen
- Evolve: Best birds make new generation
- Repeat: Birds get better each time!
Evolution Progress Over Time:
Generation
20 | ██████████ (Birds playing forever!)
|
15 | ████████
|
10 | ██████
|
5 | ████
|
1 | ██ (Random - crash immediately)
+---------------------------------- Time
Gen 1 5 10 15 20
Score Improvement:
Score
1000 | ██████████
|
750 | ████████
|
500 | ██████
|
250 | ████
|
0 | ██
+---------------------------------- Generation
1 5 10 15 20
In the top corners, you can see the neural networks "thinking":
- Best Bird Neural Network: The smartest bird so far
- Population Neural Network: Current generation's neural patterns
Colors show how active each part is:
- Red: Very active
- Yellow: Medium active
- Blue: Not active
Neural Activity Example:
Input Layer: [🔵] [🔵] [🟡] [🔴] ← Bird position, velocity, pipes
Hidden Layer: [🟡] [🔴] [🟡] [🔵] [🟡] [🔵] [🟡] [🔴] ← Processing
Output Layer: [🔴] ← FLAP! (High activation = flap)
Activity Levels:
0.0 - 0.3: 🔵 Blue (Inactive)
0.3 - 0.7: 🟡 Yellow (Medium)
0.7 - 1.0: 🔴 Red (Very Active)
The game includes a comprehensive analytics dashboard with multiple graphs:
- Fitness Graph: Shows how well the population is doing over time
- Survival Rate: Percentage of birds that survive each generation
- Training Progress: Overall learning progress through different phases
- Activity Heatmap: Real-time visualization of brain activity
- Decision Confidence: How confident the AI is in its decisions
- Complexity Graph: Measures neural network development
- Gene Pool Diversity: Genetic variety in the population
- Learning Rate: How fast the AI is adapting
- Species Graph: Population clustering patterns
- Trial and Error: Birds try different strategies
- Learning: Good strategies survive and spread
- Improvement: Each generation is better than the last
- No Human Help: The computer figures it out itself!
- Birds start terrible (crash immediately)
- After 10-20 generations, they get pretty good
- The best birds can go forever!
- Each bird's brain has 6×12 + 12×1 = 84 connections
- That's 84 numbers the computer has to learn!
- The analytics dashboard shows 12+ different metrics
- Decision confidence helps track AI learning progress
- Neural complexity increases as birds get smarter
- Open https://mehmetkahya0.github.io/ai-flappy-bird
- Watch the birds learn
- See the neural activity change
- Notice how scores improve over generations
Isn't it amazing how simple math can create intelligent behavior? 🤖🦅
mail: mehmetkahyakas5@gmail.com
github: @mehmetkahya0
website: https://mehmetkahya0.github.io/