FlappyBird game with Reinforcement Learning AI
Project description
๐ FlappyAlpha: Reinforcement Learning vs Human
๐ฎ Overview
FlappyAlpha is a reinforcement learning (RL) project inspired by AlphaGo, where an RL agent learns to play a custom Flappy Bird game environment with realistic physics (gravity, collision, etc.). The agent (FlappyAlpha) is trained using Q-Learning to maximize its score by passing obstacles and avoiding crashes. The project also features human vs. agent battles to showcase the agent's learning progress.
๐ฎ Features
- Custom Flappy Bird environment built with Pygame
- Q-Learning agent with reward/punishment system
- RL training for "Beginner" and "Hard" agents
- Human vs Agent battle playground with score visualization
- Performance interpretation and result plots
- Easy to use and play for normal users (a PyPI package is prepared)
๐ฎ Tech Stack
- Game & UI: Python, Pygame
- Reinforcement Learning: Q-Learning (NumPy)
- Data Analysis & Result Visualization: NumPy, Matplotlib
๐ฎ Simple Demo
๐ Project Structure
The project uses a standard src layout to separate the installable package from developer scripts.
zephyra/
โ
โโโ .github/workflows/ # CI/CD pipeline for PyPI publishing
โโโ src/
โ โโโ flappy_rl/ # Core package source code
โ โโโ game/ # Game environment logic
โ โโโ models/ # Pre-trained Q-table models (.npy)
โ โโโ assets/ # Game assets
โ โโโ __init__.py
โ โโโ main.py # Entry point for PyPI package to run
โ
โโโ scripts/ # Scripts for developers (training, testing)
โ โโโ train_qLearningBEGINNER.py
โ โโโ train_qLearningHARD.py
โ โโโ playground.py
โ โโโ playgroundHARD.py
โ
โโโ results/ # Output for training & playing result graphs
โโโ LICENSE
โโโ pyproject.toml # Main project configuration for packaging
โโโ README.md
โโโ requirements.txt # Dependencies
๐ Quick Start (For Users that are just want to play only)
Get started in under a minute! Play against the AI directly from your terminal. I've make it as an offical python package.
1. Install from PyPI
pip install -U flappy-rl
2. Challenge the Agent
- Play against the Beginner agent:
flappy-alpha --mode beginner
- Play against the Hard agent:
flappy-alpha --mode hard
Example
๐ ๏ธ For Developers & Researchers
This section is for those who want to dive deeper, retrain the models, or experiment with the code.
1. Clone the Repository
git clone https://github.com/kaifeng-cmd/zephyra.git
cd zephyra
2. Install Dependencies
It's recommended to use a virtual environment.
# Create and activate a virtual environment (optional but recommended)
python -m venv venv
# For Windows
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
3. Train the RL Agent
Pre-trained models are included, but you can start your own training sessions.
- Train the Beginner agent (2500 episodes):
python scripts/train_qLearningBEGINNER.py - Train the Hard agent (3000 episodes):
python scripts/train_qLearningHARD.pyTraining progress and results will be saved as
.pngplots in theresults/folder.
4. Run the Playground
Test the agents or play against them using the original developer scripts.
- Play against the Beginner agent:
python scripts/playground.py - Play against the Hard agent:
python scripts/playgroundHARD.py
๐ฎ Highlights & Insights
- The RL agent receives positive rewards for passing pipes/blocks and negative penalties for crashing.
- As training episodes increase, the agent's performance improves dramatically:
- After 2500 episodes
(beginner mode): Best score 17. - After 3000 episodes
(hard mode): Best score 75 (441% increase). - After 4000 episodes: Best score 3693 (near-perfect play).
U can modify the training parameter, ex. EPISODES = 4000 if u want.
- After 2500 episodes
- Human players can easily beat the Beginner agent
(2500 episodes), but the Hard agent(3000 episodes)is a tough opponent, often outperforming humans in average score, and I'm sure for> 4000 episodestraining, human can't beat RL agent as it plays near perfect.
๐ฎ Acknowledgements
Inspired by AlphaGo and the power of reinforcement learning.
๐ Feel free to fork, do experiment, and challenge FlappyAlpha.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flappy_rl-0.2.3.tar.gz.
File metadata
- Download URL: flappy_rl-0.2.3.tar.gz
- Upload date:
- Size: 16.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1864639c7e22595219c87582c45d35c14fa4b7cf052fc1abda2704dd49551da6
|
|
| MD5 |
e5a5126eec5c7459459b71b89702cd22
|
|
| BLAKE2b-256 |
c93b7e73272d8ef6a9417573a5415a255426fdde337e8558ba231fa9b65f99cc
|
File details
Details for the file flappy_rl-0.2.3-py3-none-any.whl.
File metadata
- Download URL: flappy_rl-0.2.3-py3-none-any.whl
- Upload date:
- Size: 16.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d75482cb973e895d16de7933b14d2407bd20dca785780edc05da2fd3ce27dc11
|
|
| MD5 |
5428481c2cd80d6ea524f33211c2cad9
|
|
| BLAKE2b-256 |
ee7eda3c61bfd52177fd88776daf20461209b09db388cf893255a69573fe2082
|