import pygame import random import sys # Initialize pygame.init() WIDTH, HEIGHT = 800, 400 win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("PacMania") clock = pygame.time.Clock() # Colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) TRAIL_COLORS = { 'White': WHITE, 'Red': (255, 0, 0), 'Green': (0, 255, 0), 'Blue': (0, 0, 255), 'Yellow': (255, 255, 0), 'Purple': (128, 0, 128), 'Cyan': (0, 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)) # Fonts font = pygame.font.SysFont('Arial', 30) menu_font = pygame.font.SysFont('Arial', 50) # Player setup player = pygame.Rect(100, 300, 30, 30) player_dy = 0 gravity = 0.6 jump_power = -13 on_ground = False # Animation 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 # 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 = pygame.transform.scale(pygame.image.load('cherry pacman.png'), (40, 40)) star_image = pygame.transform.scale(pygame.image.load('coins_for_essay-removebg-preview.png'), (20, 20)) enemy_image = pygame.transform.scale(pygame.image.load('enemy1.png'), (30, 30)) # Game state ground_height = 40 ground_y = HEIGHT - ground_height base_speed = 3 scroll_speed = base_speed speed_level = 1 start_time = pygame.time.get_ticks() speed_increase_interval = 8000 platforms, cherriess, coins, enemies = [], [], [], [] coin_timer = star_timer = enemy_timer = platform_timer = 0 last_platform_x = WIDTH distance = coin_count = star_count = total_coins = 0 game_over = False num_ground_images = 15 ground_x = [i * ground_img.get_width() for i in range(num_ground_images)] trail_color = WHITE trail_history = [] trail_shop = {name: False for name in TRAIL_COLORS if name != 'White'} # Reset function def reset_game(): global player, player_dy, on_ground, scroll_speed, speed_level, start_time global platforms, cherriess, coins, enemies global coin_count, star_count, distance, game_over global platform_timer, coin_timer, star_timer, enemy_timer, last_platform_x, total_coins, trail_history total_coins += star_count player = pygame.Rect(100, 300, 30, 30) player_dy = 0 on_ground = False scroll_speed = base_speed speed_level = 1 start_time = pygame.time.get_ticks() platforms.clear() cherriess.clear() coins.clear() enemies.clear() coin_count = star_count = distance = 0 game_over = False platform_timer = coin_timer = star_timer = enemy_timer = 0 last_platform_x = WIDTH trail_history.clear() # Spawners 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 platforms.append(pygame.Rect(x, y, platform_width, 15)) def spawn_coin(): cherriess.append(pygame.Rect(random.randint(WIDTH, WIDTH + 200), random.randint(150, 250), 20, 20)) def spawn_star(): coins.append(pygame.Rect(random.randint(WIDTH, WIDTH + 200), random.randint(100, 220), 20, 20)) def spawn_enemy(): enemies.append(pygame.Rect(WIDTH + random.randint(0, 200), ground_y - 30, 30, 30)) # Shop menu def shop_menu(): global total_coins, trail_color while True: win.blit(background_img, (0, 0)) title_text = menu_font.render("Shop", True, WHITE) win.blit(title_text, (WIDTH // 2 - title_text.get_width() // 2, 20)) y_offset = 100 for i, (color_name, color_value) in enumerate(TRAIL_COLORS.items()): if color_name == 'White': continue button_rect = pygame.Rect(WIDTH // 2 - 100, y_offset + i * 60, 200, 50) pygame.draw.rect(win, color_value, button_rect) label = f"{color_name} Trail" if not trail_shop[color_name]: label += " (5 coins)" elif trail_color == color_value: label += " ✓" text = font.render(label, True, BLACK) win.blit(text, (button_rect.centerx - text.get_width() // 2, button_rect.centery - text.get_height() // 2)) if pygame.mouse.get_pressed()[0] and button_rect.collidepoint(pygame.mouse.get_pos()): if trail_shop[color_name]: trail_color = color_value elif total_coins >= 5: total_coins -= 5 trail_shop[color_name] = True trail_color = color_value pygame.time.delay(200) # Back button back_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT - 70, 200, 50) pygame.draw.rect(win, WHITE, back_button) back_text = font.render("Back", True, BLACK) win.blit(back_text, (back_button.centerx - back_text.get_width() // 2, back_button.centery - back_text.get_height() // 2)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if back_button.collidepoint(event.pos): return # Main menu def main_menu(): while True: win.blit(background_img, (0, 0)) title_text = menu_font.render("PacMania", True, WHITE) win.blit(title_text, (WIDTH // 2 - title_text.get_width() // 2, 50)) # Buttons start_rect = pygame.Rect(WIDTH // 2 - 100, 150, 200, 50) shop_rect = pygame.Rect(WIDTH // 2 - 100, 220, 200, 50) pygame.draw.rect(win, WHITE, start_rect) pygame.draw.rect(win, WHITE, shop_rect) start_text = font.render("Start", True, BLACK) shop_text = font.render("Shop", True, BLACK) win.blit(start_text, (start_rect.centerx - start_text.get_width() // 2, start_rect.centery - start_text.get_height() // 2)) win.blit(shop_text, (shop_rect.centerx - shop_text.get_width() // 2, shop_rect.centery - shop_text.get_height() // 2)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if start_rect.collidepoint(event.pos): return if shop_rect.collidepoint(event.pos): shop_menu() # Run menu main_menu() # Game loop running = True while running: dt = clock.tick(60) win.blit(background_img, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if not game_over: if pygame.key.get_pressed()[pygame.K_SPACE] and on_ground: player_dy = jump_power on_ground = False player_dy += gravity player.y += player_dy on_ground = player.bottom >= ground_y if on_ground: player.bottom = ground_y player_dy = 0 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 for coin in cherriess[:]: coin.x -= scroll_speed if player.colliderect(coin): cherriess.remove(coin) distance += 50 coin_count += 1 for star in coins[:]: star.x -= scroll_speed if player.colliderect(star): coins.remove(star) distance += 25 star_count += 1 for enemy in enemies[:]: enemy.x -= scroll_speed if player.colliderect(enemy): game_over = True platform_timer += 1 if platform_timer > 100: spawn_platform() platform_timer = 0 if coin_timer > 150: spawn_coin() coin_timer = 0 if star_timer > 75: spawn_star() star_timer = 0 if enemy_timer > 180: spawn_enemy() enemy_timer = 0 coin_timer += 1 star_timer += 1 enemy_timer += 1 distance += scroll_speed 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() animation_timer += dt if animation_timer >= frame_delay: current_frame = (current_frame + 1) % len(player_images) animation_timer = 0 # Draw world for x in ground_x: win.blit(ground_img, (x, ground_y)) for plat in platforms: win.blit(pygame.transform.scale(platform_image_raw, (plat.width, plat.height)), plat.topleft) for coin in cherriess: win.blit(coin_image, coin.topleft) for star in coins: win.blit(star_image, star.topleft) for enemy in enemies: win.blit(enemy_image, enemy.topleft) trail_history.insert(0, player.center) trail_history = trail_history[:10] for i, pos in enumerate(trail_history): pygame.draw.circle(win, trail_color, pos, max(1, 5 - i // 2)) win.blit(player_images[current_frame], player.topleft) # HUD win.blit(font.render(f"Distance: {distance // 10}", True, WHITE), (10, 10)) win.blit(font.render(f"Cherries: {coin_count}", True, WHITE), (10, 40)) win.blit(font.render(f"Coins: {star_count}", True, WHITE), (10, 70)) win.blit(font.render(f"Speed Lv: {speed_level}", True, WHITE), (10, 100)) win.blit(font.render(f"Total Coins: {total_coins + star_count}", True, WHITE), (WIDTH - 250, 10)) else: over_text = font.render("GAME OVER", True, BLACK) win.blit(over_text, (WIDTH // 2 - over_text.get_width() // 2, HEIGHT // 2 - 60)) button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2, 200, 50) pygame.draw.rect(win, WHITE, button_rect) restart_text = font.render("Restart", True, BLACK) win.blit(restart_text, (button_rect.centerx - restart_text.get_width() // 2, button_rect.centery - restart_text.get_height() // 2)) if pygame.mouse.get_pressed()[0]: if button_rect.collidepoint(pygame.mouse.get_pos()): pygame.time.delay(200) reset_game() main_menu() pygame.display.update() pygame.quit()