From 79604d68b99188d1b4cb693e4ac3b0e76ff6c569 Mon Sep 17 00:00:00 2001 From: Isha Date: Fri, 31 Oct 2025 11:53:26 +0530 Subject: [PATCH] Added Dungeon Escape game in Python --- INDEX.md | 3 ++ Python/Dungeon_escape/README.md | 12 +++++ Python/Dungeon_escape/dungeon_escape.py | 72 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 Python/Dungeon_escape/README.md create mode 100644 Python/Dungeon_escape/dungeon_escape.py diff --git a/INDEX.md b/INDEX.md index bac4408..48d2eb7 100644 --- a/INDEX.md +++ b/INDEX.md @@ -112,3 +112,6 @@ By now we have 2 numbers (variables), you and computer ### 🎯 [Guess the number](./Python/Guess_the_number/) - Language: Python + +### 🎯 [Dungeon Escape](./Python/Dungeon_escape/) +- Language: Python diff --git a/Python/Dungeon_escape/README.md b/Python/Dungeon_escape/README.md new file mode 100644 index 0000000..b966e3a --- /dev/null +++ b/Python/Dungeon_escape/README.md @@ -0,0 +1,12 @@ +# Dungeon Escape + +## Description +A console-based maze adventure! +Navigate a 5×5 dungeon using **N/S/E/W** commands to find the exit (`E`) while avoiding traps (`X`). +You start with 3 lives — lose them all, and the dungeon wins! + +--- + +## How to Run +```bash +python dungeon_escape.py diff --git a/Python/Dungeon_escape/dungeon_escape.py b/Python/Dungeon_escape/dungeon_escape.py new file mode 100644 index 0000000..000cf03 --- /dev/null +++ b/Python/Dungeon_escape/dungeon_escape.py @@ -0,0 +1,72 @@ +import random + +# Dungeon size +ROWS, COLS = 5, 5 + +# Symbols +EMPTY, PLAYER, EXIT, TRAP = '.', 'P', 'E', 'X' + +def create_dungeon(): + dungeon = [[EMPTY for _ in range(COLS)] for _ in range(ROWS)] + exit_row, exit_col = random.randint(0, ROWS-1), random.randint(0, COLS-1) + dungeon[exit_row][exit_col] = EXIT + + # Random traps + for _ in range(5): + r, c = random.randint(0, ROWS-1), random.randint(0, COLS-1) + if dungeon[r][c] == EMPTY: + dungeon[r][c] = TRAP + + # Place player + while True: + pr, pc = random.randint(0, ROWS-1), random.randint(0, COLS-1) + if dungeon[pr][pc] == EMPTY: + dungeon[pr][pc] = PLAYER + break + return dungeon, pr, pc + +def display_dungeon(dungeon): + for row in dungeon: + print(' '.join(row)) + print() + +def move_player(dungeon, pr, pc, direction): + dungeon[pr][pc] = EMPTY + if direction == 'N': pr -= 1 + elif direction == 'S': pr += 1 + elif direction == 'E': pc += 1 + elif direction == 'W': pc -= 1 + pr, pc = max(0, min(ROWS-1, pr)), max(0, min(COLS-1, pc)) + cell = dungeon[pr][pc] + dungeon[pr][pc] = PLAYER + return pr, pc, cell + +def play(): + dungeon, pr, pc = create_dungeon() + lives = 3 + + print("🏰 Welcome to Dungeon Escape!") + print("Find the exit (E) and avoid traps (X). Move with N/S/E/W.\n") + + while True: + display_dungeon(dungeon) + move = input("Move (N/S/E/W): ").upper() + if move not in ['N', 'S', 'E', 'W']: + print("Invalid move. Try again.") + continue + + pr, pc, cell = move_player(dungeon, pr, pc, move) + + if cell == EXIT: + display_dungeon(dungeon) + print("🎉 You escaped the dungeon! You win!") + break + elif cell == TRAP: + lives -= 1 + print(f"💀 You hit a trap! Lives left: {lives}") + if lives == 0: + print("Game Over! You couldn’t escape.") + break + +if __name__ == "__main__": + play()