Untitled
unknown
plain_text
a year ago
21 kB
15
Indexable
# Simple Maze Game with Questions
# Maze legend:
# S = Start
# E = Exit
# # = Wall
# . = Open path
# Q = Question checkpoint
maze = [
['S', '.', '#', 'Q', '.', '.', '#', '.', 'E'],
['#', '.', '#', '.', '#', '.', '#', '.', '#'],
['#', '.', '.', '.', '#', '.', '.', '.', '#'],
['#', '#', '#', '.', '#', '#', '#', '.', '#'],
['#', 'Q', '.', '.', '.', '.', '#', '.', '#'],
['#', '#', '#', '#', '#', '.', '#', '.', '#'],
['#', '.', '.', 'Q', '.', '.', '.', 'Q', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', '#']
]
# Questions for checkpoints (coordinates as keys)
questions = {
(0, 3): {
'question': "What is 5 + 7?",
'answer': '12'
},
(4, 1): {
'question': "What is the capital of France?",
'answer': 'paris'
},
(6, 3): {
'question': "What color do you get when you mix red and white?",
'answer': 'pink'
},
(6, 7): {
'question': "What is 10 / 2?",
'answer': '5'
}
}
player_pos = [0, 0] # Starting position at 'S'
def print_maze():
for r, row in enumerate(maze):
row_str = ''
for c, cell in enumerate(row):
if [r, c] == player_pos:
row_str += 'P ' # Player position
else:
row_str += cell + ' '
print(row_str)
print()
def move_player(direction):
row, col = player_pos
if direction == 'w':
row -= 1
elif direction == 's':
row += 1
elif direction == 'a':
col -= 1
elif direction == 'd':
col += 1
else:
print("Invalid input! Use W/A/S/D to move.")
return False
# Check boundaries and walls
if 0 <= row < len(maze) and 0 <= col < len(maze[0]):
if maze[row][col] != '#':
player_pos[0], player_pos[1] = row, col
return True
else:
print("You hit a wall! Can't move there.")
return False
else:
print("Out of bounds!")
return False
def ask_question():
pos_tuple = tuple(player_pos)
if pos_tuple in questions:
q = questions[pos_tuple]['question']
ans = questions[pos_tuple]['answer']
print(f"You've reached a checkpoint! Answer the question to proceed:")
print(q)
player_answer = input("Your answer: ").strip().lower()
if player_answer == ans:
print("Correct! You may proceed.\n")
# Remove the checkpoint so player won't be asked again
maze[pos_tuple[0]][pos_tuple[1]] = '.'
del questions[pos_tuple]
return True
else:
print("Incorrect! Try again or move away and come back.\n")
return False
return True
def main():
print("Welcome to the Maze Game!")
print("Move with W (up), A (left), S (down), D (right). Reach 'E' to win.")
print("At checkpoints (Q), answer questions to pass.\n")
while True:
print_maze()
if maze[player_pos[0]][player_pos[1]] == 'E':
print("Congratulations! You've reached the exit and won the game!")
break
if not ask_question():
# If answer is wrong, do not allow moving from checkpoint cell until correct or moved away.
continue
move = input("Move (W/A/S/D): ").strip().lower()
move_player(move)
if __name__ == "__main__":
main()
}
}
}
}
}
]Editor is loading...
Leave a Comment