AI Technology world 🌍

Ai Technology world
By -
0

Building a Simple Bird Game with Code and Blueprint

Bird games, inspired by classics like Flappy Bird, are fun, challenging, and an excellent way to learn game development. In this blog, we will create a simple 2D bird game using Python (Pygame) and Unreal Engine (Blueprints), so you can choose the approach that suits you best.


1. Building the Bird Game in Python (Pygame)

Pygame is a beginner-friendly library for making games in Python. Below is a simple Flappy Bird-style game where the bird jumps and avoids obstacles.

Installation

First, install Pygame if you haven’t already:

pip install pygame

Game Code

import pygame
import random

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 400, 600
GRAVITY = 0.5
JUMP = -10
PIPE_GAP = 150
PIPE_SPEED = 3
BIRD_X = 50

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

# Setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 40)

# Load Assets
bird_img = pygame.image.load("bird.png")
bird_img = pygame.transform.scale(bird_img, (40, 30))

# Game Variables
bird_y = HEIGHT // 2
bird_velocity = 0
pipes = []
score = 0

# Function to create pipes
def create_pipes():
    pipe_height = random.randint(100, 400)
    pipes.append({"x": WIDTH, "top": pipe_height, "bottom": pipe_height + PIPE_GAP})

# Main Game Loop
running = True
while running:
    screen.fill(WHITE)
    
    # Event Handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_velocity = JUMP

    # Update Bird
    bird_velocity += GRAVITY
    bird_y += bird_velocity

    # Draw Bird
    screen.blit(bird_img, (BIRD_X, bird_y))

    # Move and Draw Pipes
    for pipe in pipes:
        pipe["x"] -= PIPE_SPEED
        pygame.draw.rect(screen, GREEN, (pipe["x"], 0, 50, pipe["top"]))
        pygame.draw.rect(screen, GREEN, (pipe["x"], pipe["bottom"], 50, HEIGHT - pipe["bottom"]))

    # Generate new pipes
    if not pipes or pipes[-1]["x"] < WIDTH - 200:
        create_pipes()

    # Remove off-screen pipes
    if pipes[0]["x"] < -50:
        pipes.pop(0)
        score += 1

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

    # Collision Detection
    for pipe in pipes:
        if BIRD_X + 40 > pipe["x"] and BIRD_X < pipe["x"] + 50:
            if bird_y < pipe["top"] or bird_y + 30 > pipe["bottom"]:
                running = False  # Game Over

    if bird_y > HEIGHT or bird_y < 0:
        running = False  # Game Over

    pygame.display.update()
    clock.tick(30)

pygame.quit()

How It Works

  • The bird jumps when you press space.
  • Pipes move from right to left.
  • The game ends if the bird collides with a pipe or falls off the screen.

2. Building the Bird Game in Unreal Engine (Blueprints)

Step 1: Setting Up the Project

  1. Open Unreal Engine and create a 2D Side Scroller project.
  2. Add a Sprite for the bird and name it BP_Bird.
  3. Add a Pipe Blueprint (BP_Pipe) for obstacles.

Step 2: Creating the Bird Behavior

In BP_Bird:

  1. Add an Event Tick → Apply Gravity (Add Movement Input downward).
  2. Add a Jump Input (Spacebar) → Apply Upward Impulse (Launch Character).

Step 3: Generating Pipes

  1. Create BP_PipeSpawner Blueprint.
  2. Use Timer by Event to spawn pipes every few seconds.
  3. Move pipes leftward with Add Actor World Offset.
  4. Destroy pipes when off-screen.

Step 4: Collision & Score System

  1. In BP_Bird, add OnComponentHit event:
    • If bird hits a pipe, trigger Game Over.
  2. Add a Score Trigger Box after each pipe.
    • When the bird overlaps, increase the score variable.

Step 5: Displaying UI

  1. Add a Widget Blueprint (BP_HUD).
  2. Bind text to Score Variable.
  3. Show “Game Over” when the bird crashes.

Conclusion

We created a 2D bird game in both Python (Pygame) and Unreal Engine (Blueprints). Python is great for quick prototypes, while Unreal Engine provides a visual approach for more advanced games.

Would you like a downloadable project file for either version? Let me know!

Tags:

Post a Comment

0 Comments

Post a Comment (0)
3/related/default