import pygame import random import sys # Initialize pygame.init() WIDTH, HEIGHT = 800, 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) BROWN = (139, 69, 19) GREEN = (0, 200, 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 # Ground ground_height = 40 ground_y = HEIGHT - ground_height # World elements scroll_speed = 3 platforms = [] platform_timer = 0 last_platform_x = WIDTH # Coin elements coins = [] coin_timer = 0 coin_count = 0 # Track number of coins collected coin_image_raw = pygame.image.load('coins_for_essay-removebg-preview.png') coin_image = pygame.transform.scale(coin_image_raw, (20, 20)) # Distance mechanic distance = 0 font = pygame.font.SysFont('Arial', 30) 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) coins.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 coins[:]: coin.x -= scroll_speed if player.colliderect(coin): coins.remove(coin) distance += 50 coin_count += 1 # Increase coin counter # Clean up platforms = [p for p in platforms if p.right > 0] coins = [c for c in coins 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 # Draw ground pygame.draw.rect(win, BROWN, (0, ground_y, WIDTH, ground_height)) # Draw platforms for plat in platforms: pygame.draw.rect(win, GREEN, plat) # Draw coins for coin in coins: win.blit(coin_image, coin.topleft) # Draw player (replace 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"Coins: {coin_count}", True, WHITE) win.blit(coin_text, (10, 40)) pygame.display.update() pygame.quit()