Here is the source code for a Python project that implements a machine learning algorithm in the Flappy Bird video game using neural networks and a genetic algorithm.
All code is written in Python using Pygame and NEAT for genetic algorithm implementation.
To play the game, each unit (bird) has its own neural network consisted of 2 layers:
-
an input layer with 3 neurons presenting what a bird sees:
1) bird's height 1) height difference between the bird and the next top Pipe 2) height difference between the bird and the next bottom Pipe
-
an output layer with 1 neuron used to provide an action as follows:
if output > 0.5 then flap else do nothing
The main concept of machine learning implemented in this program is based on the neuro-evolution form. It uses evolutionary algorithms such as a genetic algorithm to train artificial neural networks. Here are the main steps:
-
create a new population of 50 units (birds) with a random neural network
-
let all units play the game simultaneously by using their own neural networks
-
for each unit calculate its fitness function to measure its quality as:
fitness = 0.1 * total travelled distance + 5 * number of pillars passed through.
-
when all units are killed, evaluate the current population to the next one using genetic algorithm operators:
1) selection 2) crossover 3) mutation
-
go back to the step 2
The entire game logic is implemented in flappyBirds.py file.
Class to represent the moving floor.
class to represent a Bird sprite.
Class to represent a moving barrier. This contains a top and bottom sprite.
configuration file to modify neuroevolution algorithm parameters.