Loading ...

Flappy Bird clone using Pygame

Let’s build a simple Flappy Bird clone using Pygame, a popular library for game development in Python. Here’s a basic structure for the game:

Step 1: Install Pygame

Make sure you have Pygame installed. You can install it using pip:

pip install pygame

Step 2: Game Code

import pygame
import random

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird Clone")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Game variables
gravity = 0.5
jump_strength = -10
pipe_speed = 3
pipe_gap = 150

# Load assets
font = pygame.font.SysFont("Arial", 32)

# Bird class
class Bird:
    def __init__(self):
        self.x = 50
        self.y = HEIGHT // 2
        self.velocity = 0
        self.radius = 20

    def jump(self):
        self.velocity = jump_strength

    def move(self):
        self.velocity += gravity
        self.y += self.velocity

    def draw(self):
        pygame.draw.circle(screen, BLACK, (self.x, int(self.y)), self.radius)

# Pipe class
class Pipe:
    def __init__(self):
        self.x = WIDTH
        self.height = random.randint(50, HEIGHT - pipe_gap - 50)
        self.passed = False

    def move(self):
        self.x -= pipe_speed

    def draw(self):
        pygame.draw.rect(screen, BLACK, (self.x, 0, 50, self.height))
        pygame.draw.rect(screen, BLACK, (self.x, self.height + pipe_gap, 50, HEIGHT - self.height - pipe_gap))

# Game loop
def main():
    clock = pygame.time.Clock()
    bird = Bird()
    pipes = [Pipe()]
    score = 0
    running = True

    while running:
        clock.tick(60)
        screen.fill(WHITE)

        # Event handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                bird.jump()

        # Bird movement
        bird.move()
        bird.draw()

        # Pipe movement
        for pipe in pipes:
            pipe.move()
            pipe.draw()

            if pipe.x + 50 < 0:
                pipes.remove(pipe)
                pipes.append(Pipe())

            if not pipe.passed and pipe.x < bird.x:
                pipe.passed = True
                score += 1

        # Display score
        score_text = font.render(f"Score: {score}", True, BLACK)
        screen.blit(score_text, (10, 10))

        # Collision detection
        for pipe in pipes:
            if (bird.y - bird.radius < pipe.height or bird.y + bird.radius > pipe.height + pipe_gap) and (pipe.x < bird.x < pipe.x + 50):
                running = False  # End game on collision

        if bird.y > HEIGHT or bird.y < 0:
            running = False

        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()

Step 3: Run the Game

Save the code in a Python file (e.g., flappy_bird.py) and run:

python flappy_bird.py

This code provides a basic Flappy Bird clone. You can customize it further by adding sound effects, improving graphics, or tweaking the difficulty levels.

The error indicates that Pygame isn’t installed in your Python environment. Here’s how you can fix it:

1. Verify Python Version

First, make sure you’re using the correct Python version where you want to install Pygame:

C:/Users/Hp/AppData/Local/Programs/Python/Python313/python.exe --version

2. Install Pygame in the Correct Environment

Run this command to install Pygame specifically for Python 3.13:

C:/Users/Hp/AppData/Local/Programs/Python/Python313/python.exe -m pip install pygame

3. Verify Pygame Installation

After installation, verify if Pygame is installed successfully:

C:/Users/Hp/AppData/Local/Programs/Python/Python313/python.exe -m pip show pygame

4. Run Your Game Again

Once installed, run your game again:

C:/Users/Hp/AppData/Local/Programs/Python/Python313/python.exe d:/python-projects/games/flappy_bird.py

Let me know if you run into any other issues! 🚀

Related Posts

🐍 How to Use Tkinter Listbox to Add and Print Selected Items in Python

🐍 How to Use Tkinter Listbox to Add and Print Selected Items in Python If you’re learning Tkinter, Python’s standard GUI library, and want to build a fun and interactive…

Read more

Building a Modern CRUD Menu Application with Python and ttkbootstrap

Title: Building a Modern CRUD Menu Application with Python and ttkbootstrap Introduction Welcome to my latest tech adventure! In this blog post, I’ll walk you through the development of a…

Read more

Building Car Racing Elite: A High-Graphics Pygame without any images

Want to create a thrilling car racing game with stunning visuals using Python? In this step-by-step Pygame tutorial, I’ll guide you through building “Car Racing Elite”—a modern racing game with…

Read more

Building a Modern Menu Viewer with Python and Tkinter: A Step-by-Step Guide

Morden Menu Viewer with Python Looking to build a modern desktop application with Python? In this detailed guide, I’ll walk you through creating a sleek menu viewer using Python’s Tkinter…

Read more

🎯 Title: Build a Modern Calculator Using Python Tkinter

Here’s a complete blog post explaining how to build a modern calculator using Python Tkinter from scratch. This will cover everything from setting up Tkinter to customizing the UI for…

Read more

Jumping Jack Game with Python and Pygame

Let’s build a simple Jumping Jack game using Pygame, a popular library for game development in Python. This game will feature basic mechanics like jumping, gravity, and obstacle dodging, with…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *