2024-05-17.14-38-29.mp4
This project implements a genetic algorithm and neural networks to train AI agents to play Flappy Bird. The AI birds evolve over generations to improve their performance in the game.
The AI agents (birds) are controlled by a neural network. The neural network takes inputs about the bird's environment and outputs a decision whether the bird should jump. The goal is to navigate through the pipes without colliding. The fitness of each bird is determined by how far it travels and how many pipes it passes.
- Neural Network: The birds are controlled by a neural network with a customizable number of input, hidden, and output neurons.
- Genetic Algorithm: The neural networks are trained using a genetic algorithm, which includes crossover and mutation to evolve the birds over generations.
- Real-time Visualization: The game can be visualized in real-time to observe the birds' performance.
- Python 3.x
- Flask
- Pygame
- OpenCV
- NumPy
- Matplotlib
-
Clone the repository:
git clone https://github.com/Ori2846/FlappyBirdAI.git cd FlappyBirdAI
-
Install the required packages:
pip install -r requirements.txt
-
Start the Flask server:
python app.py
-
Open your web browser and navigate to
http://127.0.0.1:5000/
.
- Press the
Space
key to make the bird jump.
app.py
: Main Flask application file that runs the server and handles the game loop.neural_network.py
: Contains the implementation of the neural network and genetic algorithm functions.templates/index.html
: HTML template for the web interface.
- Initialization: The game initializes a population of neural networks (birds) with random weights.
- Game Loop: Each bird plays the game using its neural network to decide when to jump based on inputs such as the bird's position and the position of the nearest pipes.
- Fitness Calculation: Each bird's fitness is calculated based on how far it travels and how many pipes it passes.
- Selection and Reproduction: The top-performing birds are selected to create a new generation through crossover and mutation.
- Evolution: The process repeats for multiple generations, gradually improving the birds' performance.
- Implement more complex neural network architectures.
- Add additional game elements and obstacles.
- Improve the visualization and user interface.
Contributions are welcome! Feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.
- Inspired by the original Flappy Bird game.
- Uses Pygame for game rendering and control.