Skip to main content

FlappyBird game with Reinforcement Learning AI

Project description

๐Ÿ† FlappyAlpha: Reinforcement Learning vs Human

PyPI version License: MIT

๐ŸŽฎ 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

Demo video

๐Ÿ“‚ 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

cmd to start the game


๐Ÿ› ๏ธ 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.py
    

    Training progress and results will be saved as .png plots in the results/ 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.

  • 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 episodes training, 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

flappy_rl-0.2.2.tar.gz (16.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flappy_rl-0.2.2-py3-none-any.whl (16.0 MB view details)

Uploaded Python 3

File details

Details for the file flappy_rl-0.2.2.tar.gz.

File metadata

  • Download URL: flappy_rl-0.2.2.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

Hashes for flappy_rl-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e87bf824365ff30088dbb5ac6a12cd8d74fd6391037967682bfee8960d63402a
MD5 3765a2ece5dcabbe24063afaac36e6bb
BLAKE2b-256 3ec59a6bc6929ed9443f66f48acdae82733c5ca9ca8d766657933e33386d9490

See more details on using hashes here.

File details

Details for the file flappy_rl-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: flappy_rl-0.2.2-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

Hashes for flappy_rl-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 19e08939e40ef41d2540a028174ae79b5c4237b01baab4ea3254b75986600f93
MD5 67b4162d8153be02166da71a610d0832
BLAKE2b-256 91df2f5b020b439114c4ed343417912f415800bad32e68a607c9cb2bfdf7f473

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page