🚀 Create Flappy Bird AI Game in Python – Complete Code with AI Automation!

Demo :


Click Video 👇👇👇














📄 Description:
Learn how to create a Flappy Bird AI Game using Python and Pygame! 🎮 This project includes AI automation that lets the bird play automatically. Perfect for beginners & game developers. 💻🔥

🔹 Features:
✅ Flappy Bird with AI Mode 🤖
✅ Pipe Obstacles & Gravity Effect 🎯
✅ Automatic Scoring System 🏆
✅ Full Python Code Included! 🐍


Code :

 

import pygame

import random


# Initialize pygame

pygame.init()


# Game Constants  

WIDTH, HEIGHT = 400, 600

BIRD_X = 50

GRAVITY = 0.5

JUMP_STRENGTH = -8

PIPE_GAP = 150

PIPE_WIDTH = 60

PIPE_SPEED = 4

AI_MODE = False  # Change to False for manual play


# Colors

WHITE = (255, 255, 255)

GREEN = (0, 200, 0)

BLUE = (0, 150, 255)

RED = (255, 0, 0)

BLACK = (0, 0, 0)


# Load Assets

bird_img = pygame.image.load("bird.png")

bird_img = pygame.transform.scale(bird_img, (40, 30))


# Initialize Display

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Flappy Bird AI")


# Fonts

font = pygame.font.Font(None, 36)


# Score Variables

score = 0

high_score = 0


# Bird Class

class Bird:

    def __init__(self):

        self.y = HEIGHT // 2

        self.velocity = 0

        self.alive = True


    def update(self):

        self.velocity += GRAVITY

        self.y += self.velocity


    def jump(self):

        self.velocity = JUMP_STRENGTH


    def draw(self):

        screen.blit(bird_img, (BIRD_X, self.y))


# Pipe Class

class Pipe:

    def __init__(self, x):

        self.x = x

        self.height = random.randint(100, HEIGHT - PIPE_GAP - 100)

        self.passed = False  # Track if bird has passed the pipe


    def move(self):

        self.x -= PIPE_SPEED

        if self.x < -PIPE_WIDTH:

            self.x = WIDTH

            self.height = random.randint(100, HEIGHT - PIPE_GAP - 100)

            self.passed = False  # Reset for new pipe


    def draw(self):

        pygame.draw.rect(screen, GREEN, (self.x, 0, PIPE_WIDTH, self.height))

        pygame.draw.rect(screen, GREEN, (self.x, self.height + PIPE_GAP, PIPE_WIDTH, HEIGHT))


# AI Decision Function

def ai_jump(bird, pipes):

    if pipes[0].x < BIRD_X + 50 and bird.y > pipes[0].height + 30:

        return True

    return False


# Game Over Screen

def game_over_screen():

    global high_score, score

    if score > high_score:

        high_score = score  # Update high score

    

    screen.fill(BLUE)

    text = font.render(f"Game Over! Score: {score}", True, RED)

    restart_text = font.render("Press ENTER to Restart", True, WHITE)

    screen.blit(text, (WIDTH // 2 - 100, HEIGHT // 2 - 50))

    screen.blit(restart_text, (WIDTH // 2 - 130, HEIGHT // 2))

    pygame.display.update()

    

    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                pygame.quit()

                exit()

            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:

                return


# Main Game Loop

def game_loop():

    global score

    while True:

        bird = Bird()

        pipes = [Pipe(WIDTH + i * 200) for i in range(2)]

        running = True

        clock = pygame.time.Clock()

        score = 0


        while running:

            screen.fill(BLUE)

            bird.update()

            for pipe in pipes:

                pipe.move()

                pipe.draw()


            bird.draw()

            

            # Score Display

            score_text = font.render(f"Score: {score}", True, WHITE)

            high_score_text = font.render(f"High Score: {high_score}", True, WHITE)

            screen.blit(score_text, (10, 10))

            screen.blit(high_score_text, (10, 40))

            

            # Collision Detection

            if bird.y < 0 or bird.y > HEIGHT:

                running = False


            for pipe in pipes:

                if BIRD_X < pipe.x + PIPE_WIDTH and BIRD_X + 40 > pipe.x:

                    if bird.y < pipe.height or bird.y > pipe.height + PIPE_GAP:

                        running = False

                

                # Score Update

                if pipe.x + PIPE_WIDTH < BIRD_X and not pipe.passed:

                    score += 1

                    pipe.passed = True


            # AI Auto Play

            if AI_MODE and ai_jump(bird, pipes):

                bird.jump()


            # Manual Play

            for event in pygame.event.get():

                if event.type == pygame.QUIT:

                    pygame.quit()

                    exit()

                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not AI_MODE:

                    bird.jump()


            pygame.display.update()

            clock.tick(30)


        # Game Over screen with restart option

        game_over_screen()


# Run Game

game_loop()




Comments

Popular posts from this blog

🚀 Simple Login & Registration System in Python Tkinter 📱

🚀 Create a Python Screen Recorder with Audio (Complete Code)

📡 Fuzzu Packet Sniffer – Python GUI for Real-Time IP Monitoring | Tkinter + Scapy