Skip to content

Flat-Earther/FlappyBird-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NEAT Flappy Bird

A neuroevolution implementation of the classic Flappy Bird game using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.

Overview

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.

What is NEAT?

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

Learn more about NEAT

Requirements

  • Python 3.6+
  • pygame
  • neat-python
  • numpy

Installation

  1. Clone the repository: �ash git clone <repository-url> cd NEAT-Flappy_BIRD

  2. Install dependencies: �ash pip install pygame neat-python numpy

  3. Ensure you have the required assets in an imgs/ directory:

    • bird1.png
    • bird2.png
    • bird3.png
    • pipe.png
    • base.png
    • bg.png
  4. Ensure the NEAT configuration file is present:

    • config-feedforward.txt

Usage

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

How It Works

Game Environment

  • Window: 500x800 pixels
  • Bird: Animated sprite that can jump
  • Pipes: Randomly generated obstacles with a 200-pixel gap
  • Gravity: Constant downward acceleration

Neural Network Input

Each bird's neural network receives 3 inputs:

  1. Bird's current Y position
  2. Vertical distance to top pipe
  3. Vertical distance to bottom pipe

Neural Network Output

  • Single output: If > 0, the bird jumps

Fitness Evaluation

  • +0.1 per frame alive
  • +5 for successfully passing a pipe
  • -1 for colliding with obstacles

Project Structure

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

Key Classes

Bird

Represents the player-controlled bird with physics simulation.

  • Handles movement, gravity, and animation
  • Collision detection via pixel masks

Pipe

Represents obstacles in the game.

  • Random height generation
  • Collision detection with the bird

Base

Represents the scrolling ground at the bottom.

  • Continuous scrolling effect

Configuration

The config-feedforward.txt file contains NEAT algorithm parameters including:

  • Population size
  • Mutation rates
  • Speciation thresholds
  • Activation functions
  • Network topology constraints

Output

During training, the program will display:

  • Generation number
  • Population statistics
  • Best fitness scores
  • Species information

Tips for Improvement

  • Adjust the config-feedforward.txt parameters 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

License

This project is provided as-is for educational purposes.

References