Untitled
Anonymous
plain_text
02/24/2026 10:30 PM
2.6 KB
11
Indexable
import random
import time
# Maze size
WIDTH = 5
HEIGHT = 5
# Initialize player, key, and enemy positions
player_pos = [0, 0]
key_pos = [random.randint(0, WIDTH-1), random.randint(0, HEIGHT-1)]
enemy_pos = [WIDTH-1, HEIGHT-1]
print("š® Welcome to Key of the Hollow!")
print("Find the key and escape to [0,0]. Avoid the entity chasing you!")
# Automatically generate random moves
directions = ['w','a','s','d']
has_key = False
step = 0
MAX_STEPS = 50 # prevent infinite loops
while step < MAX_STEPS:
step += 1
print(f"\nStep {step}: You are at {player_pos}")
if has_key:
print("You have the key! Find your way back to [0,0] to escape.")
else:
print("The key is somewhere in the maze.")
# Check if player finds key
if player_pos == key_pos:
has_key = True
print("š You found the key!")
# Check if player escapes
if has_key and player_pos == [0,0]:
print("šŖ You escaped! Congratulations!")
break
# Pick a random move
move = random.choice(directions)
print(f"Move chosen: {move}")
# Apply move
if move == 'w' and player_pos[1] > 0:
player_pos[1] -= 1
elif move == 's' and player_pos[1] < HEIGHT-1:
player_pos[1] += 1
elif move == 'a' and player_pos[0] > 0:
player_pos[0] -= 1
elif move == 'd' and player_pos[0] < WIDTH-1:
player_pos[0] += 1
else:
print("You can't move that way!")
# Enemy moves toward player
if enemy_pos[0] < player_pos[0]:
enemy_pos[0] += 1
elif enemy_pos[0] > player_pos[0]:
enemy_pos[0] -= 1
if enemy_pos[1] < player_pos[1]:
enemy_pos[1] += 1
elif enemy_pos[1] > player_pos[1]:
enemy_pos[1] -= 1
# Check if enemy catches player
if enemy_pos == player_pos:
print("š» An entity caught you... Game over!")
break
print(f"You hear something moving at {enemy_pos}")
time.sleep(0.5)
else:
print("\nā ļø Maximum steps reached. Game over.")Editor is loading...
Leave a Comment