import pygame import sys import random # Initialize Pygame pygame.init() # Set up the screen screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Mario Run") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) # Player properties player_width = 50 player_height = 50 player_x = 50 player_y = screen_height - player_height - 50 player_speed = 5 player_jump = 10 player_jump_height = 150 player_jump_count = 0 is_jumping = False # Platform properties platform_width = screen_width platform_height = 50 platform_x = 0 platform_y = screen_height - platform_height # Coin properties coin_width = 20 coin_height = 20 coins = [] num_coins = 5 # Monster properties monster_width = 50 monster_height = 50 monsters = [] num_monsters = 3 monster_speed = 3 # Currency (coins) and score coins_collected = 0 font = pygame.font.Font(None, 36) # Obstacle properties obstacle_width = 50 obstacle_height = 50 obstacles = [] num_obstacles = 3 # Level properties levels = [ { "num_coins": 5, "num_monsters": 3, "num_obstacles": 3, }, { "num_coins": 7, "num_monsters": 5, "num_obstacles": 5, } ] current_level = 0 # Function to generate random coins def generate_coins(num_coins): coins.clear() for _ in range(num_coins): x = random.randint(coin_width, screen_width - coin_width) y = random.randint(coin_height, screen_height - coin_height) coins.append((x, y)) # Function to generate random monsters def generate_monsters(num_monsters): monsters.clear() for _ in range(num_monsters): x = random.randint(monster_width, screen_width - monster_width) y = random.randint(monster_height, screen_height - monster_height) monsters.append((x, y)) # Function to generate random obstacles def generate_obstacles(num_obstacles): obstacles.clear() for _ in range(num_obstacles): x = random.randint(obstacle_width, screen_width - obstacle_width) y = random.randint(obstacle_height, screen_height - obstacle_height) obstacles.append((x, y)) # Function to handle level change def change_level(): global current_level, coins_collected current_level += 1 coins_collected = 0 if current_level < len(levels): generate_coins(levels[current_level]["num_coins"]) generate_monsters(levels[current_level]["num_monsters"]) generate_obstacles(levels[current_level]["num_obstacles"]) # Main game loop generate_coins(levels[current_level]["num_coins"]) generate_monsters(levels[current_level]["num_monsters"]) generate_obstacles(levels[current_level]["num_obstacles"]) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed # Player jump if keys[pygame.K_SPACE] and not is_jumping: is_jumping = True if is_jumping: player_y -= player_jump player_jump_count += 1 if player_jump_count >= player_jump_height: player_jump_count = 0 is_jumping = False # Boundaries player_x = max(0, min(player_x, screen_width - player_width)) player_y = max(0, min(player_y, screen_height - player_height)) # Player collision with coins for coin in coins: coin_rect = pygame.Rect(coin[0], coin[1], coin_width, coin_height) player_rect = pygame.Rect(player_x, player_y, player_width, player_height) if coin_rect.colliderect(player_rect): coins.remove(coin) coins_collected += 1 # Player collision with monsters for monster in monsters: monster_rect = pygame.Rect(monster[0], monster[1], monster_width, monster_height) player_rect = pygame.Rect(player_x, player_y, player_width, player_height) if monster_rect.colliderect(player_rect): print("You were caught by a monster! Game Over.") running = False # Player collision with obstacles for obstacle in obstacles: obstacle_rect = pygame.Rect(obstacle[0], obstacle[1], obstacle_width, obstacle_height) player_rect = pygame.Rect(player_x, player_y, player_width, player_height) if obstacle_rect.colliderect(player_rect): print("You hit an obstacle! Game Over.") running = False # Clear the screen screen.fill(WHITE) # Draw player pygame.draw.rect(screen, RED, (player_x, player_y, player_width, player_height)) # Draw platform pygame.draw.rect(screen, BLUE, (platform_x, platform_y, platform_width, platform_height)) # Draw coins for coin in coins: pygame.draw.ellipse(screen, YELLOW, (coin[0], coin[1], coin_width, coin_height)) # Draw monsters for monster in monsters: pygame.draw.rect(screen, BLACK, (monster[0], monster[1], monster_width, monster_height)) # Draw obstacles for obstacle in obstacles: pygame.draw.rect(screen, GREEN, (obstacle[0], obstacle[1], obstacle_width, obstacle_height)) # Draw currency (coins) and score score_text = font.render("Coins: " + str(coins_collected), True, BLACK) screen.blit(score_text, (10, 10)) # Check if all coins are collected to advance to the next level if coins_collected == levels[current_level]["num_coins"]: change_level() pygame.display.flip() # Cap the frame rate pygame.time.Clock().tick(60) # Quit Pygame pygame.quit() sys.exit()