🟥 This section introduces new constants for enhanced gameplay features, including a level start marker and a collectible item.
# --- Constants for our tiles ---
WALL = "⬛"
EMPTY = "⬜"
PLAYER = "🟦"
GOAL = "🟩"
# --- New Constants for features ---
KEY = "🔑" # A collectible item required to finish some levels
LEVEL_START = "▶️" # Marks the starting position for the player in a new level
🟧 Here, we define multiple levels for the game. Each level is represented as a list of lists (the map grid). This structure allows for game progression.
# --- The Map Data (A list of lists) ---
# Level 1: Simple maze
level_1_map = [
[WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, LEVEL_START, WALL, EMPTY, GOAL, WALL], # Player starts here
[WALL, EMPTY, WALL, EMPTY, WALL, WALL],
[WALL, EMPTY, WALL, EMPTY, EMPTY, WALL],
[WALL, EMPTY, WALL, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL],
]
# Level 2: Introduces a key
level_2_map = [
[WALL, WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, LEVEL_START, EMPTY, WALL, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, EMPTY, WALL, WALL],
[WALL, EMPTY, KEY, EMPTY, EMPTY, WALL, WALL], # Key required for goal
[WALL, WALL, WALL, WALL, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, GOAL, EMPTY, WALL], # Goal
[WALL, WALL, WALL, WALL, WALL, WALL, WALL],
]
# Level 3: More complex, multiple paths
level_3_map = [
[WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, LEVEL_START, EMPTY, WALL, EMPTY, EMPTY, EMPTY, WALL],
[WALL, EMPTY, WALL, WALL, EMPTY, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, WALL, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, EMPTY, WALL, WALL, WALL, WALL, KEY, WALL], # Key
[WALL, EMPTY, GOAL, EMPTY, EMPTY, EMPTY, EMPTY, WALL], # Goal
[WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL],
]
# A list to hold all our levels
all_levels = [
(level_1_map, "The Introduction"),
(level_2_map, "The Key Quest"),
(level_3_map, "The Grand Maze"),
]
# Game state variables
current_level_index = 0
moves_taken_this_level = 0
total_moves_game = 0
has_key = False # New state variable for the collectible
🟨 The print_map function is updated to display the current level name, total moves taken, and whether the player possesses the key.
def print_map(current_map, level_name, moves, key_status):
"""Takes a list of lists and prints it as a clean grid,
now including game information."""
print("\n" * 35) # A simple way to "clear" the screen
print(f"--- Level: {level_name} ---")
print(f"Moves: {moves} | Key: {'YES' if key_status else 'NO'}")
print("-" * (len(level_name) + 15)) # Simple separator
for row in current_map:
print("".join(row))
print("\n")
def find_player(current_map):
"""Searches the map to find the row and column of the PLAYER tile.
If PLAYER is not found (e.g., at the start of a new level), it looks for LEVEL_START."""
for row_index, row_content in enumerate(current_map):
for col_index, tile in enumerate(row_content):
if tile == PLAYER:
return (row_index, col_index)
# If PLAYER not found, it must be a new level, so look for LEVEL_START
for row_index, row_content in enumerate(current_map):
for col_index, tile in enumerate(row_content):
if tile == LEVEL_START:
# Place the player at the start position for this new level
current_map[row_index][col_index] = PLAYER
return (row_index, col_index)
return (None, None) # Should not happen if levels are set up correctly