import pygame
import random
import sys

# Initialize
pygame.init()
WIDTH, HEIGHT = 1000, 400
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Auto-Scrolling Platformer")
clock = pygame.time.Clock()

# Colors
SKY = (135, 206, 235)
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Player setup
player = pygame.Rect(100, 300, 30, 30)
player_dy = 0
gravity = 0.6
jump_power = -13
on_ground = False

# Load Angry Bird image
angry_bird_img_raw = pygame.image.load('angry bird red.webp')  # Replace with your filename
angry_bird_img = pygame.transform.scale(angry_bird_img_raw, (30, 30))  # Match player size

# Load Platform Image
platform_image_raw = pygame.image.load('set-wooden-plank-frame-game-ui-asset-textured-vector-47358680-removebg-preview.png')  # Replace with your filename

# Load Ground Image (Multiple Images for Smooth Scroll)
ground_height = 40
ground_y = HEIGHT - ground_height
ground_img_raw = pygame.image.load('JJ-stomper.jpg')  # Replace with your filename
ground_img = pygame.transform.scale(ground_img_raw, (100, ground_height))  # Scale to desired height

# World elements
scroll_speed = 3
platforms = []
platform_timer = 0
last_platform_x = WIDTH

# Coin elements
eggs = []
coin_timer = 0
coin_count = 0  # Track number of eggs collected
coin_image_raw = pygame.image.load('Egg_angry_birds.webp')
coin_image = pygame.transform.scale(coin_image_raw, (20, 20))

# Distance mechanic
distance = 0
font = pygame.font.SysFont('Arial', 30)

# Ground movement variables (15 images)
num_ground_images = 15  # Now we have 15 images
ground_x = [i * ground_img.get_width() for i in range(num_ground_images)]  # 15 images

def spawn_platform():
    global last_platform_x
    y = random.randint(250, 270)
    platform_width = random.randint(80, 120)
    x = last_platform_x + random.randint(5, 20)
    last_platform_x = x
    plat = pygame.Rect(x, y, platform_width, 15)
    platforms.append(plat)

def spawn_coin():
    coin_x = random.randint(WIDTH, WIDTH + 200)
    coin_y = random.randint(150, 250)
    coin_rect = pygame.Rect(coin_x, coin_y, 20, 20)
    eggs.append(coin_rect)

# Game loop
running = True
while running:
    clock.tick(60)
    win.fill(SKY)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Input
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] and on_ground:
        player_dy = jump_power
        on_ground = False

    # Gravity
    player_dy += gravity
    player.y += player_dy
    on_ground = False

    # Ground collision
    if player.bottom >= ground_y:
        player.bottom = ground_y
        player_dy = 0
        on_ground = True

    # Platform collisions
    for plat in platforms:
        plat.x -= scroll_speed
        if (
            player.right > plat.left + 5 and
            player.left < plat.right - 5 and
            player.bottom <= plat.top + 5 and
            player.bottom + player_dy >= plat.top
        ):
            player.bottom = plat.top
            player_dy = 0
            on_ground = True

    # Coin collision and cleanup
    for coin in eggs[:]:
        coin.x -= scroll_speed
        if player.colliderect(coin):
            eggs.remove(coin)
            distance += 50
            coin_count += 1

    # Clean up
    platforms = [p for p in platforms if p.right > 0]
    eggs = [c for c in eggs if c.right > 0]

    # Spawn elements
    platform_timer += 1
    if platform_timer > 100:
        spawn_platform()
        platform_timer = 0

    coin_timer += 1
    if coin_timer > 150:
        spawn_coin()
        coin_timer = 0

    # Distance update
    distance += scroll_speed

    # Move the ground images (scrolling effect)
    for i in range(num_ground_images):
        ground_x[i] -= scroll_speed

    # Reposition ground images when they move off-screen
    for i in range(num_ground_images):
        if ground_x[i] <= -ground_img.get_width():
            # Move the image to the end of the row
            ground_x[i] = ground_x[(i - 1) % num_ground_images] + ground_img.get_width()

    # Draw ground images (scrolling effect)
    for x in ground_x:
        win.blit(ground_img, (x, ground_y))

    # Draw platforms with image
    for plat in platforms:
        scaled_platform = pygame.transform.scale(platform_image_raw, (plat.width, plat.height))
        win.blit(scaled_platform, plat.topleft)

    # Draw eggs
    for coin in eggs:
        win.blit(coin_image, coin.topleft)

    # Draw player (with Angry Bird image)
    win.blit(angry_bird_img, player.topleft)

    # Draw UI
    distance_text = font.render(f"Distance: {distance // 10}", True, WHITE)
    win.blit(distance_text, (10, 10))

    coin_text = font.render(f"eggs: {coin_count}", True, WHITE)
    win.blit(coin_text, (10, 40))

    pygame.display.update()

pygame.quit()