A neuroevolution implementation of the classic Flappy Bird game using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.
This project uses artificial neural networks that are evolved through the NEAT algorithm to learn how to play Flappy Bird. Instead of hand-coding the game AI, neural networks are trained over multiple generations to develop increasingly sophisticated strategies for navigating through pipes.
NEAT is a method for evolving neural networks that:
- Starts with simple networks and gradually adds complexity
- Evolves both the weights and structure of neural networks
- Uses speciation to protect innovative topologies
- Produces efficient, minimal networks that solve tasks
- Python 3.6+
- pygame
- neat-python
- numpy
-
Clone the repository:
�ash git clone <repository-url> cd NEAT-Flappy_BIRD -
Install dependencies:
�ash pip install pygame neat-python numpy -
Ensure you have the required assets in an
imgs/directory:bird1.pngbird2.pngbird3.pngpipe.pngbase.pngbg.png
-
Ensure the NEAT configuration file is present:
config-feedforward.txt
Run the training:
python main.py
The script will:
- Initialize a population of neural networks
- Simulate the Flappy Bird game for each network
- Evaluate fitness based on performance
- Evolve the population over 150 generations
- Display statistics for each generation
- Window: 500x800 pixels
- Bird: Animated sprite that can jump
- Pipes: Randomly generated obstacles with a 200-pixel gap
- Gravity: Constant downward acceleration
Each bird's neural network receives 3 inputs:
- Bird's current Y position
- Vertical distance to top pipe
- Vertical distance to bottom pipe
- Single output: If > 0, the bird jumps
+0.1per frame alive+5for successfully passing a pipe-1for colliding with obstacles
NEAT-Flappy_BIRD/ main.py # Main game and NEAT training logic config-feedforward.txt # NEAT algorithm configuration imgs/ # Game assets bird1.png bird2.png bird3.png pipe.png base.png bg.png README.md
Represents the player-controlled bird with physics simulation.
- Handles movement, gravity, and animation
- Collision detection via pixel masks
Represents obstacles in the game.
- Random height generation
- Collision detection with the bird
Represents the scrolling ground at the bottom.
- Continuous scrolling effect
The config-feedforward.txt file contains NEAT algorithm parameters including:
- Population size
- Mutation rates
- Speciation thresholds
- Activation functions
- Network topology constraints
During training, the program will display:
- Generation number
- Population statistics
- Best fitness scores
- Species information
- Adjust the
config-feedforward.txtparameters for different evolution rates - Modify fitness function values in
main()to emphasize different behaviors - Add more neural network inputs (e.g., bird velocity, pipe distance)
- Increase generations for longer training
This project is provided as-is for educational purposes.