I'll add several functional features to enhance this game while maintaining the theme. Here's the improved version:
# --- Constants for our tiles ---
WALL = "⬛"
EMPTY = "⬜"
PLAYER = "🟦"
GOAL = "🟩"
BOX = "🟫"
BOX_ON_GOAL = "🟧"
KEY = "🟨"
DOOR = "🟥"
TELEPORTER = "🟪"
# --- Game state variables ---
has_key = False
moves_count = 0
boxes_pushed = 0
# --- The Map Data (A list of lists) ---
map_data = [
[WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, PLAYER, EMPTY, EMPTY, WALL, EMPTY, GOAL, EMPTY, WALL],
[WALL, EMPTY, BOX, EMPTY, WALL, EMPTY, WALL, WALL, WALL],
[WALL, WALL, WALL, EMPTY, DOOR, EMPTY, EMPTY, EMPTY, WALL],
[WALL, KEY, EMPTY, EMPTY, WALL, WALL, WALL, EMPTY, WALL],
[WALL, EMPTY, WALL, EMPTY, EMPTY, TELEPORTER, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, BOX, EMPTY, WALL, WALL, EMPTY, WALL],
[WALL, TELEPORTER, WALL, WALL, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL],
]
# Store original goal positions for box puzzles
goal_positions = []
for r in range(len(map_data)):
for c in range(len(map_data[r])):
if map_data[r][c] == GOAL:
goal_positions.append((r, c))
def print_map(current_map):
"""Takes a list of lists and prints it as a clean grid."""
print("\n" * 35)
for row in current_map:
print("".join(row))
print(f"\nMoves: {moves_count} | Boxes Pushed: {boxes_pushed}")
if has_key:
print("🔑 You have a key!")
print("\n")
def find_player(current_map):
"""Searches the map to find the row and column of the PLAYER tile."""
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)
return (None, None)
def find_teleporters(current_map):
"""Find all teleporter positions on the map."""
teleporters = []
for row_index, row_content in enumerate(current_map):
for col_index, tile in enumerate(row_content):
if tile == TELEPORTER:
teleporters.append((row_index, col_index))
return teleporters
def check_win_condition(current_map):
"""Check if all boxes are on goal positions."""
boxes_on_goals = 0
total_boxes = 0
for row in current_map:
for tile in row:
if tile == BOX:
total_boxes += 1
elif tile == BOX_ON_GOAL:
boxes_on_goals += 1
total_boxes += 1
# Win if all boxes are on goals (or if there are no boxes and player reached a goal)
if total_boxes == 0:
return GOAL not in [tile for row in current_map for tile in row]
return boxes_on_goals == total_boxes and boxes_on_goals > 0
# --- The Main Game Loop ---
while True:
# 1. Display the current state of the game
print_map(map_data)
# 2. Find the player's current position
player_row, player_col = find_player(map_data)
# Check win condition
if check_win_condition(map_data):
print(f"🎉 Victory! You completed the puzzle in {moves_count} moves!")
print(f"Total boxes pushed: {boxes_pushed}")
break
# 3. Get user input
move = input("Enter a move (W/A/S/D) or R to restart: ").upper().strip()
# Handle restart
if move == "R":
print("Restarting level...")
# Reset map to initial state (you'd need to store the original map)
moves_count = 0
boxes_pushed = 0
has_key = False
input("Press Enter to continue...")
continue
# 4. Determine the coordinates of the target destination
next_row, next_col = player_row, player_col
if move == "W":
next_row -= 1
elif move == "S":
next_row += 1
elif move == "A":
next_col -= 1
elif move == "D":
next_col += 1
else:
print("Invalid input! Please use W, A, S, or D.")
input("Press Enter to continue...")
continue
# Bounds checking
if next_row < 0 or next_row >= len(map_data) or next_col < 0 or next_col >= len(map_data[0]):
print("You can't move outside the map!")
input("Press Enter to continue...")
continue
# 5. Check the destination tile
destination_tile = map_data[next_row][next_col]
if destination_tile == WALL:
print("You can't move into a wall!")
input("Press Enter to continue...")
continue
elif destination_tile == DOOR:
if has_key:
print("You unlock the door with your key!")
map_data[next_row][next_col] = PLAYER
map_data[player_row][player_col] = EMPTY
has_key = False
moves_count += 1
else:
print("This door is locked! You need a key 🔑")
input("Press Enter to continue...")
continue
elif destination_tile == KEY:
print("You found a key! 🔑")
has_key = True
map_data[next_row][next_col] = PLAYER
map_data[player_row][player_col] = EMPTY
moves_count += 1
input("Press Enter to continue...")
elif destination_tile == BOX or destination_tile == BOX_ON_GOAL:
# Calculate where the box would go
box_next_row = next_row + (next_row - player_row)
box_next_col = next_col + (next_col - player_col)
# Check bounds for box
if (box_next_row < 0 or box_next_row >= len(map_data) or
box_next_col < 0 or box_next_col >= len(map_data[0])):
print("Can't push the box off the map!")
input("Press Enter to continue...")
continue
box_destination = map_data[box_next_row][box_next_col]
# Can only push box to empty space or goal
if box_destination == EMPTY or box_destination == GOAL:
# Move the box
if box_destination == GOAL:
map_data[box_next_row][box_next_col] = BOX_ON_GOAL
else:
map_data[box_next_row][box_next_col] = BOX
# Move player to box's old position
map_data[next_row][next_col] = PLAYER
# Update player's old position
if (player_row, player_col) in goal_positions:
map_data[player_row][player_col] = GOAL
else:
map_data[player_row][player_col] = EMPTY
boxes_pushed += 1
moves_count += 1
else:
print("Can't push the box there!")
input("Press Enter to continue...")
continue
elif destination_tile == TELEPORTER:
teleporters = find_teleporters(map_data)
if len(teleporters) == 2:
# Find the other teleporter
other_teleporter = None
for tp in teleporters:
if tp != (next_row, next_col):
other_teleporter = tp
break
if other_teleporter:
print("Whoosh! You've been teleported! 🌀")
# Move player to the other teleporter
map_data[other_teleporter[0]][other_teleporter[1]] = PLAYER
map_data[player_row][player_col] = EMPTY
moves_count += 1
input("Press Enter to continue...")
else:
# Just move normally if teleporter is broken
map_data[next_row][next_col] = PLAYER
map_data[player_row][player_col] = EMPTY
moves_count += 1
elif destination_tile == GOAL:
# Move to goal
map_data[next_row][next_col] = PLAYER
map_data[player_row][player_col] = EMPTY
moves_count += 1
else: # EMPTY tile
# Move the player to the new spot
map_data[next_row][next_col] = PLAYER
# Check if player was on a goal
if (player_row, player_col) in goal_positions:
map_data[player_row][player_col] = GOAL
else:
map_data[player_row][player_col] = EMPTY
moves_count += 1