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 BLACK = (0, 0, 0) WHITE = (255, 255, 255) # Load and scale background image background_img_raw = pygame.image.load('retro-sci-fi-futuristic-background-.jpg') background_img = pygame.transform.scale(background_img_raw, (WIDTH, HEIGHT)) # Player setup player = pygame.Rect(100, 300, 30, 30) player_dy = 0 gravity = 0.6 jump_power = -13 on_ground = False # Load animation frames for the player player_images = [ pygame.transform.scale(pygame.image.load('pacmansprite1.png'), (30, 30)), pygame.transform.scale(pygame.image.load('pacmansprite2.png'), (30, 30)), pygame.transform.scale(pygame.image.load('pacmansprite3.png'), (30, 30)) ] current_frame = 0 animation_timer = 0 frame_delay = 100 # milliseconds # Load other images platform_image_raw = pygame.image.load('platform3.jpg') ground_img_raw = pygame.image.load('ground1.jpg') ground_img = pygame.transform.scale(ground_img_raw, (100, 40)) coin_image_raw = pygame.image.load('Egg_angry_birds.webp') coin_image = pygame.transform.scale(coin_image_raw, (20, 20)) enemy_image_raw = pygame.image.load('enemy1.png') # Replace with your enemy image enemy_image = pygame.transform.scale(enemy_image_raw, (30, 30)) # World elements ground_height = 40 ground_y = HEIGHT - ground_height scroll_speed = 3 platforms = [] platform_timer = 0 last_platform_x = WIDTH eggs = [] coin_timer = 0 coin_count = 0 enemies = [] enemy_timer = 0 distance = 0 font = pygame.font.SysFont('Arial', 30) game_over = False num_ground_images = 15 ground_x = [i * ground_img.get_width() for i in range(num_ground_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) def spawn_enemy(): enemy_x = WIDTH + random.randint(0, 200) enemy_y = ground_y - 30 enemy_rect = pygame.Rect(enemy_x, enemy_y, 30, 30) enemies.append(enemy_rect) # Game loop running = True while running: dt = clock.tick(60) # delta time in milliseconds win.blit(background_img, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if not game_over: 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 # Enemy movement and collision for enemy in enemies[:]: enemy.x -= scroll_speed if player.colliderect(enemy): game_over = True # Clean up platforms = [p for p in platforms if p.right > 0] eggs = [c for c in eggs if c.right > 0] enemies = [e for e in enemies if e.right > 0] # Spawning platform_timer += 1 if platform_timer > 100: spawn_platform() platform_timer = 0 coin_timer += 1 if coin_timer > 150: spawn_coin() coin_timer = 0 enemy_timer += 1 if enemy_timer > 180: spawn_enemy() enemy_timer = 0 # Distance update distance += scroll_speed # Move ground for i in range(num_ground_images): ground_x[i] -= scroll_speed if ground_x[i] <= -ground_img.get_width(): ground_x[i] = ground_x[(i - 1) % num_ground_images] + ground_img.get_width() # Update animation frame animation_timer += dt if animation_timer >= frame_delay: current_frame = (current_frame + 1) % len(player_images) animation_timer = 0 # Draw ground for x in ground_x: win.blit(ground_img, (x, ground_y)) # Draw platforms for plat in platforms: scaled_platform = pygame.transform.scale(platform_image_raw, (plat.width, plat.height)) win.blit(scaled_platform, plat.topleft) # Draw coins for coin in eggs: win.blit(coin_image, coin.topleft) # Draw enemies for enemy in enemies: win.blit(enemy_image, enemy.topleft) # Draw player animation win.blit(player_images[current_frame], player.topleft) # 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)) else: # Game Over Screen over_text = font.render("GAME OVER", True, BLACK) win.blit(over_text, (WIDTH // 2 - over_text.get_width() // 2, HEIGHT // 2 - 20)) pygame.display.update() pygame.quit()