Skip to content

Hasnain006-nain/Flappy-Bird

Repository files navigation

🐦 Flappy Bird — Python Game

Python Pygame Game Status


A fully functional Flappy Bird clone built with Python & Pygame
featuring collision detection, score tracking, sound effects, and randomized pipes.


🚀 Getting Started🎮 Gameplay📁 Structure⚙️ Features👤 Author



📌 Table of Contents



🔍 Overview

This is a fully functional Flappy Bird clone built from scratch using Python and the Pygame library. The player controls a bird navigating through randomly generated pipes, with real-time collision detection, live score display, and full sound effects. The game runs at 32 FPS with smooth sprite rendering and physics-based bird movement.

✨ Key Highlights

🏆 Feature 📋 Detail
🎮 Engine Pygame
🖥️ Resolution 289 × 511 pixels
🎯 FPS 32
🔊 Sound Effects Wing, Hit, Point, Die, Swoosh
🌀 Pipes Randomly generated each run


🎮 Gameplay

┌──────────────────────────────┐
│   🐦  →  Navigate the bird   │
│   🟩  →  Avoid the pipes     │
│   ⭐  →  Score per pipe pass │
│   💥  →  Game over on hit    │
└──────────────────────────────┘
  • Tap SPACE or ↑ UP to flap and gain altitude
  • Gravity pulls the bird down continuously
  • Pass through pipe gaps to score points
  • Hit a pipe or the ground — Game Over!


📁 Project Structure

📦 flappy-bird/
│
├── 🐍 flappy_bird.py          ← Main game script
│
├── 🖼️  Images/
│   ├── bird.png               ← Player sprite
│   ├── background.png         ← Background image
│   ├── pipe.png               ← Pipe sprite
│   ├── base.png               ← Ground sprite
│   ├── message.jpg            ← Cover/welcome screen
│   ├── message1.png           ← Welcome message overlay
│   └── 0.png - 9.png          ← Score digit sprites
│
├── 🔊 Sounds/
│   ├── wing.wav               ← Flap sound
│   ├── hit.wav                ← Collision sound
│   ├── point.wav              ← Score sound
│   ├── die.wav                ← Death sound
│   └── swoosh.wav             ← Swoosh sound
│
└── 📝 README.md               ← Project documentation


⚙️ Features

  • Welcome Screen — Cover page shown before the game starts
  • Physics Engine — Gravity, flap velocity, and acceleration
  • Randomized Pipes — Different gap positions every game
  • Collision Detection — Ground, upper and lower pipe checks
  • Live Score Display — Digit sprites rendered in real time
  • Sound Effects — Wing, point, hit, die, and swoosh
  • Smooth Animation — 32 FPS game loop with Pygame clock


🚀 Getting Started

1. Install Pygame

pip install pygame

2. Clone the Repository

git clone https://github.com/Hasnain006-nain/flappy-bird.git
cd flappy-bird

3. Add Assets

Place all image and sound files in the same directory as flappy_bird.py

4. Run the Game

python flappy_bird.py


🕹️ Controls

Key Action
SPACE Flap / Jump
↑ UP Arrow Flap / Jump
ESC Quit Game
✕ Close Button Exit


🔬 How It Works

┌──────────────────────────────────────────────────────────────────┐
│                       GAME LOOP OVERVIEW                         │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│  1. 🟢 Initialize      →  Pygame init, load sprites & sounds     │
│                                                                  │
│  2. 🖼️  Welcome Screen  →  Show cover until SPACE is pressed     │
│                                                                  │
│  3. 🎮 Main Game Loop   →  Handle events every frame             │
│                                                                  │
│  4. 🌀 Generate Pipes   →  Random gap positions via getRandomPipe│
│                                                                  │
│  5. 🐦 Bird Physics     →  Gravity + flap velocity applied       │
│                                                                  │
│  6. 💥 Collision Check  →  Ground, upper pipe, lower pipe        │
│                                                                  │
│  7. ⭐ Score Update     →  Increment when pipe midpoint passed   │
│                                                                  │
│  8. 🖥️  Render Frame    →  Blit background, pipes, bird, score   │
│                                                                  │
│  9. 🔁 Repeat / Reset   →  Return to welcome on crash            │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘

🌀 Pipe Generation Logic

offset = SCREENHEIGHT / 3
y2 = offset + random.randrange(0, int(SCREENHEIGHT - base_height - 1.2 * offset))
y1 = pipeHeight - y2 + offset
# Returns upper pipe (inverted) + lower pipe positions

🐦 Bird Physics

playerVelY += playerAccY        # Gravity pulls bird down
playerVelY = playerFlapAccv     # Flap gives upward velocity
playery += min(playerVelY, GROUNDY - playery - playerHeight)


📦 Assets Required

File Type Description
bird.png Image Bird player sprite
background.png Image Game background
pipe.png Image Pipe obstacle sprite
base.png Image Ground/floor sprite
message.jpg Image Welcome cover screen
message1.png Image Welcome overlay text
0.png – 9.png Images Score digit sprites
wing.wav Sound Flap sound effect
hit.wav Sound Collision sound effect
point.wav Sound Score sound effect
die.wav Sound Death sound effect
swoosh.wav Sound Swoosh sound effect

💡 All assets must be in the same folder as the main Python script.



⚠️ Known Issues & Notes

⚠️ Indentation Bug

if playerFlapped:
playerFlapped = False   # ← Missing indentation

Fix:

if playerFlapped:
    playerFlapped = False

⚠️ Missing Assets Error

If any .png, .jpg, or .wav file is missing, Pygame will raise a FileNotFoundError. Ensure all assets are present before running.



🔮 Future Improvements

  • 🏆 Add High Score saving system
  • 🎨 Add multiple bird skin options
  • 🌙 Add day/night background switching
  • 📱 Port to mobile using Kivy or similar
  • 🤖 Add an AI agent to play automatically using NEAT or Q-Learning
  • 🔊 Add background music loop


👤 Author


╔════════════════════════════════════╗
║                                    ║
║         Hasnain Haider             ║
║                                    ║
║   Python Developer & ML Engineer   ║
║   Games | Data Science | AI        ║
║                                    ║
╚════════════════════════════════════╝

LinkedIn



© 2024 Hasnain Haider — Built for educational purposes in Python Game Development

If you found this project helpful, please give it a star!

About

A Flappy Bird clone built with Python & Pygame — featuring physics-based bird movement, randomized pipes, collision detection, live score display, and full sound effects.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages