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.1.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.1-py3-none-any.whl (16.0 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flappy_rl-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e57cdbb27f9829a09f23259c76bff3f9f080763538e4cee78a5836191fcbd02b
MD5 c1b5e333257c7289b52b12fac90e8047
BLAKE2b-256 d2ca73c1d84b6f8c7b13444ec16efd4830dac6cef96aae00310394e200e88dd6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flappy_rl-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0bbb6bc61b2596308bf2075074238a8ff50c408a370241eef70ae5f828d13b50
MD5 2e8a68950aa45bed825584ec765fd192
BLAKE2b-256 98a7d4931599492f2313a85efbc68a85f08bdb660db04f649f5be10af73330ea

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