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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flappy_rl-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c89d86915803a3548fbcac1b5fc0f19413e0be3f81c7a6565559e2d96178f11e
MD5 ea77de4281eacc1e9b514c66ddb7d4dc
BLAKE2b-256 629b69facd7094074ebbd9b1b91ee6f1b6f0fab6aa5a9e6859d1f0928390283f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flappy_rl-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f7f57a880474c1a79fc0ccf3c3fe72a8c69e6820502c6a30d477cccaed06250
MD5 1555a917dffefbde61033fe644f43b0c
BLAKE2b-256 b3275b8b684585430c21fa4760e995f9b36db20874da902b66e2419dbcf72769

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