Skip to main content

FlappyBird game with Q-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

๐ŸŽฎ Tech Stack

  • Game & UI: Python, Pygame
  • Reinforcement Learning: Q-Learning (NumPy)
  • Data Analysis & Result Visualization: NumPy, Matplotlib

๐ŸŽฎ Simple Demo

Demo video

๐ŸŽฎ Project Structure

rl_flappyBird/
โ”‚
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ .gitignore
โ”‚
โ”œโ”€โ”€ src/                        # Core game environment
โ”‚   โ””โ”€โ”€ flappy_bird.py
โ”‚
โ”œโ”€โ”€ train/                      # Training scripts
โ”‚   โ”œโ”€โ”€ train_qLearningBEGINNER.py
โ”‚   โ””โ”€โ”€ train_qLearningHARD.py
โ”‚
โ”œโ”€โ”€ models/                     # Trained models
โ”‚   โ”œโ”€โ”€ best_q_table_beginner.npy
โ”‚   โ””โ”€โ”€ best_q_table_hard.npy
โ”‚
โ”œโ”€โ”€ results/                    # Result plots and analysis
โ”‚   โ”œโ”€โ”€ beginner_mode_scores.png
โ”‚   โ”œโ”€โ”€ hard_mode_scores.png
โ”‚   โ”œโ”€โ”€ user_vs_agent_scores.png
โ”‚   โ””โ”€โ”€ user_vs_agent_scoresHARD.png
โ”‚
โ”œโ”€โ”€ playground/                 # Human vs agent
โ”‚   โ”œโ”€โ”€ playground.py
โ”‚   โ””โ”€โ”€ playgroundHARD.py
โ”‚
โ””โ”€โ”€ assets/                     # Assets

๐ŸŽฎ How to Use

U can use virtual environment (venv), but since the dependencies are just common and less, so depend on yourself.

1. Install Dependencies

pip install -r requirements.txt

2. Train the RL Agent

Note: Pre-trained Q-tables are provided in the models/ folder. You can retrain if you wish.

  • Train Beginner mode agent:
    python train/train_qLearningBEGINNER.py
    
  • Train Hard mode agent:
    python train/train_qLearningHARD.py
    

3. Human vs. Agent Battle

  • Play against Beginner agent:
    python playground/playground.py
    
  • Play against Hard agent:
    python playground/playgroundHARD.py
    

4. View Results

  • Training and battle results are auto saved as .png plots in the results/ folder.

๐ŸŽฎ 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.0.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.0-py3-none-any.whl (16.0 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flappy_rl-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 784c01e0ee8874e5ba0d3372879fdb5da764711bf1a84727f51a0c8b6f72dc31
MD5 7e18ff21cb57e5dd19f44352f20b3146
BLAKE2b-256 71c96b18f2230b0a2fc3146c688e31790e4f2057775a5a8a15ddd7c03b8bb3d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flappy_rl-0.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15dddb2209443308b3f5048831494da1e8f94652bc7328ca0f096818ccce16bc
MD5 fb499c99d40cce63d0108e514fd7020d
BLAKE2b-256 eeaba9bd2307ba2dc91e055296e866f383b4a1b8eefb82daad7519a3f872ea55

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