Untitled

 avatar
unknown
python
5 months ago
3.4 kB
2
Indexable
import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 400, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird - Easy")

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

# Fonts
font = pygame.font.Font(None, 40)

# Game variables
clock = pygame.time.Clock()
gravity = 0.25
bird_movement = 0
score = 0

# Load assets
background = pygame.image.load("nature_background.jpg")  # Replace with a simple nature background image
background = pygame.transform.scale(background, (SCREEN_WIDTH, SCREEN_HEIGHT))

bird = pygame.Surface((30, 30))
bird.fill(GOLDEN)

pipe_surface = pygame.Surface((50, SCREEN_HEIGHT))
pipe_surface.fill(WHITE)

# Positions
bird_rect = bird.get_rect(center=(100, SCREEN_HEIGHT // 2))
pipe_list = []
SPAWN_PIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWN_PIPE, 1200)  # Spawn pipes every 1.2 seconds

# Functions
def draw_pipes(pipes):
    for pipe in pipes:
        pygame.draw.rect(screen, BLACK, pipe)

def move_pipes(pipes):
    for pipe in pipes:
        pipe.centerx -= 2  # Slow movement for easy difficulty
    return [pipe for pipe in pipes if pipe.right > 0]

def check_collision(pipes):
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            return False
    if bird_rect.top <= 0 or bird_rect.bottom >= SCREEN_HEIGHT:
        return False
    return True

def display_score():
    score_surface = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_surface, (10, 10))

def draw_watermark():
    watermark = font.render("Rapid", True, BLACK)
    screen.blit(watermark, (SCREEN_WIDTH - 90, SCREEN_HEIGHT - 40))

# Main game loop
running = True
game_active = True

while running:
    screen.blit(background, (0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and game_active:
                bird_movement = -6  # Flap up
            if event.key == pygame.K_SPACE and not game_active:
                game_active = True
                pipe_list.clear()
                bird_rect.center = (100, SCREEN_HEIGHT // 2)
                bird_movement = 0
                score = 0

        if event.type == SPAWN_PIPE:
            pipe_height = random.randint(200, 400)
            top_pipe = pipe_surface.get_rect(midbottom=(SCREEN_WIDTH + 100, pipe_height - 150))
            bottom_pipe = pipe_surface.get_rect(midtop=(SCREEN_WIDTH + 100, pipe_height))
            pipe_list.extend([top_pipe, bottom_pipe])

    if game_active:
        # Bird
        bird_movement += gravity
        bird_rect.centery += bird_movement
        screen.blit(bird, bird_rect)

        # Pipes
        pipe_list = move_pipes(pipe_list)
        draw_pipes(pipe_list)

        # Collision
        game_active = check_collision(pipe_list)

        # Score
        score += 0.01  # Slow score increment for easy mode
        display_score()

    else:
        game_over_surface = font.render("Game Over!", True, WHITE)
        screen.blit(game_over_surface, (SCREEN_WIDTH // 2 - 80, SCREEN_HEIGHT // 2))

    # Watermark
    draw_watermark()

    # Update the screen
    pygame.display.update()
    clock.tick(60)
Editor is loading...
Leave a Comment