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) # 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 # 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('cherry pacman.png') coin_image = pygame.transform.scale(coin_image_raw, (40, 40)) star_image_raw = pygame.image.load('coins_for_essay-removebg-preview.png') star_image = pygame.transform.scale(star_image_raw, (20, 20)) enemy_image_raw = pygame.image.load('enemy1.png') enemy_image = pygame.transform.scale(enemy_image_raw, (30, 30)) # World elements 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 = [] platform_timer = 0 last_platform_x = WIDTH cherriess = [] coin_timer = 0 coin_count = 0 coins = [] star_timer = 0 star_count = 0 enemies = [] enemy_timer = 0 distance = 0 game_over = False total_coins = 0 # Tracks coins across game sessions num_ground_images = 15 ground_x = [i * ground_img.get_width() for i in range(num_ground_images)] 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 total_coins += star_count # Transfer coins to total before resetting 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 = 0 star_count = 0 distance = 0 game_over = False platform_timer = 0 coin_timer = 0 star_timer = 0 enemy_timer = 0 last_platform_x = WIDTH 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) cherriess.append(coin_rect) def spawn_star(): star_x = random.randint(WIDTH, WIDTH + 200) star_y = random.randint(100, 220) star_rect = pygame.Rect(star_x, star_y, 20, 20) coins.append(star_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) def shop_menu(): while True: win.blit(background_img, (0, 0)) title_text = menu_font.render("Shop", True, WHITE) back_button_text = font.render("Back", True, BLACK) title_x = WIDTH // 2 - title_text.get_width() // 2 title_y = HEIGHT // 2 - 100 win.blit(title_text, (title_x, title_y)) button_width, button_height = 200, 50 button_x = WIDTH // 2 - button_width // 2 button_y = HEIGHT // 2 back_button_rect = pygame.Rect(button_x, button_y, button_width, button_height) pygame.draw.rect(win, WHITE, back_button_rect) win.blit(back_button_text, (button_x + button_width // 2 - back_button_text.get_width() // 2, button_y + button_height // 2 - back_button_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_rect.collidepoint(event.pos): return # Back to main menu def main_menu(): while True: win.blit(background_img, (0, 0)) title_text = menu_font.render("PacMania", True, WHITE) start_button_text = font.render("Start", True, BLACK) shop_button_text = font.render("Shop", True, BLACK) title_x = WIDTH // 2 - title_text.get_width() // 2 title_y = HEIGHT // 2 - 120 win.blit(title_text, (title_x, title_y)) # Start Button button_width, button_height = 200, 50 start_button_x = WIDTH // 2 - button_width // 2 start_button_y = HEIGHT // 2 - 20 start_button_rect = pygame.Rect(start_button_x, start_button_y, button_width, button_height) pygame.draw.rect(win, WHITE, start_button_rect) win.blit(start_button_text, (start_button_x + button_width // 2 - start_button_text.get_width() // 2, start_button_y + button_height // 2 - start_button_text.get_height() // 2)) # Shop Button shop_button_x = WIDTH // 2 - button_width // 2 shop_button_y = HEIGHT // 2 + 50 shop_button_rect = pygame.Rect(shop_button_x, shop_button_y, button_width, button_height) pygame.draw.rect(win, WHITE, shop_button_rect) win.blit(shop_button_text, (shop_button_x + button_width // 2 - shop_button_text.get_width() // 2, shop_button_y + button_height // 2 - shop_button_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_button_rect.collidepoint(event.pos): return if shop_button_rect.collidepoint(event.pos): shop_menu() # Start 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: elapsed_time = pygame.time.get_ticks() - start_time new_level = elapsed_time // speed_increase_interval + 1 if new_level > speed_level: speed_level = new_level scroll_speed = base_speed + speed_level - 1 keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] and on_ground: player_dy = jump_power on_ground = False player_dy += gravity player.y += player_dy on_ground = False if player.bottom >= ground_y: player.bottom = ground_y player_dy = 0 on_ground = True 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 platforms = [p for p in platforms if p.right > 0] cherriess = [c for c in cherriess if c.right > 0] coins = [s for s in coins if s.right > 0] enemies = [e for e in enemies if e.right > 0] platform_timer += 1 if platform_timer > 100: spawn_platform() platform_timer = 0 coin_timer += 1 if coin_timer > 150: spawn_coin() coin_timer = 0 star_timer += 1 if star_timer > 75: spawn_star() star_timer = 0 enemy_timer += 1 if enemy_timer > 180: spawn_enemy() enemy_timer = 0 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 for x in ground_x: win.blit(ground_img, (x, ground_y)) for plat in platforms: scaled_platform = pygame.transform.scale(platform_image_raw, (plat.width, plat.height)) win.blit(scaled_platform, 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) win.blit(player_images[current_frame], player.topleft) distance_text = font.render(f"Distance: {distance // 10}", True, WHITE) win.blit(distance_text, (10, 10)) coin_text = font.render(f"Cherries: {coin_count}", True, WHITE) win.blit(coin_text, (10, 40)) star_text = font.render(f"Coins: {star_count}", True, WHITE) win.blit(star_text, (10, 70)) speed_text = font.render(f"Speed Lv: {speed_level}", True, WHITE) win.blit(speed_text, (10, 100)) total_text = font.render(f"Total Coins: {total_coins + star_count}", True, WHITE) win.blit(total_text, (WIDTH - total_text.get_width() - 10, 10)) # TOP RIGHT else: over_text = font.render("GAME OVER", True, BLACK) win.blit(over_text, (WIDTH // 2 - over_text.get_width() // 2, HEIGHT // 2 - 60)) button_width, button_height = 200, 50 button_x = WIDTH // 2 - button_width // 2 button_y = HEIGHT // 2 button_rect = pygame.Rect(button_x, button_y, button_width, button_height) pygame.draw.rect(win, WHITE, button_rect) restart_text = font.render("Restart", True, BLACK) win.blit(restart_text, (button_x + button_width // 2 - restart_text.get_width() // 2, button_y + button_height // 2 - restart_text.get_height() // 2)) if pygame.mouse.get_pressed()[0]: mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): pygame.time.delay(200) reset_game() main_menu() pygame.display.update() pygame.quit()