Project No.I

2020 2020
Game

Game

AI AI
Implementation of the NEAT algorithm for learning an agent to play Flappy Bird.

The NEAT (NeuroEvolution of Augmenting Topologies) algorithm is a type of artificial neural network (ANN) that uses genetic algorithms to evolve the structure and weights of neural networks. Unlike other ANN approaches that require the network structure to be predefined, NEAT allows for the creation of new nodes and connections during the evolutionary process. This enables NEAT to discover complex and optimized network structures that may not have been found using other ANN methods.

The NEAT algorithm starts with a population of randomly generated neural networks, each with a simple structure. The networks are then evaluated based on their performance on a given task, and the best-performing networks are selected to breed and create a new population. The breeding process involves the creation of new nodes and connections between existing nodes, as well as the adjustment of the weights of these connections. The new population is then evaluated again, and the process repeats until the desired performance is achieved or a stopping criterion is met.

NEAT has been used to solve a variety of problems, such as game playing, control of robots, and image recognition. Its ability to evolve neural network structures and weights makes it a powerful tool for discovering complex and optimized solutions to a wide range of problems.


To use it for a Flappy Bird game, we need to first create a game environment using a game development library like Pygame.

Then, we create a NEAT neural network that takes in game data as input, such as the bird's current position and velocity, the distance to the next pipe, and the bird's current score. The output of the network would be a decision on whether to flap or not, which would be used to control the bird in the game.

After creating the neural network, we then use a genetic algorithm to evolve the network over many generations, with the fitness of each generation determined by how well the bird performs in the game. The goal is to create a neural network that is good enough to play the game well on its own.

To implement this, we would need to define the game mechanics and rules, as well as the inputs and outputs of the neural network. Then we would use the NEAT algorithm to train the neural network to play the game.

Creating the game using the Pygame Library

Pygame is a popular Python library that is used to create video games, multimedia applications, and other graphical programs. It provides a set of tools and functionalities that simplify the process of creating games and interactive applications.

Pygame is built on top of the Simple DirectMedia Layer (SDL) library, which provides low-level access to audio, keyboard, mouse, joystick, and graphics hardware. Pygame allows developers to create games and applications that run on a variety of platforms, including Windows, Linux, and macOS.


To make a Flappy Bird game using the Pygame library, we first need to set up the game window using Pygame's display module. Then, we can load our game assets such as images, sounds, and fonts using Pygame's image, mixer, and font modules. Next, we can create and manage game objects such as the bird, pipes, and score using Pygame's Sprite class and Group class.

To make the bird fly, we can use Pygame's event handling system to listen for key presses and update the bird's position accordingly. We can also use Pygame's collision detection system to detect collisions between the bird and the pipes. To add a score system, we can track the number of pipes the bird successfully passes through and display the score on the screen using Pygame's font module.

Finally, we can add game over logic by checking for collisions between the bird and the pipes and ending the game when a collision is detected. We then add a game restart function using Pygame's event handling system to listen for key presses or to automatically restart the game the the player looses.

Next Project

Object recognition