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.1.1.tar.gz (16.1 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.1.1-py3-none-any.whl (16.2 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flappy_rl-0.1.1.tar.gz
  • Upload date:
  • Size: 16.1 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.1.1.tar.gz
Algorithm Hash digest
SHA256 398074451d25018f6173a4c9c2e5229066def95cbebebf8cb93c6fc250819413
MD5 7297215646088b3adb9d8c5442a8ba02
BLAKE2b-256 131c989dcc0da2b8f3da26d606fe8a630b09882cf0d281e0cf28c0053762881c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flappy_rl-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.2 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bdba63be55cf2810ea8d39c41eaf344abf79b05aab804696d778453e813e9c51
MD5 fc9b191de216b6ad18e76cf9d1e8991f
BLAKE2b-256 a27b3b0b8f87cdeab8c380fd3ef80ec3475d7cf71858b39df56a1510f2089b2b

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