Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions Python/Dungeon_escape/README.md
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions Python/Dungeon_escape/dungeon_escape.py
Original file line number Diff line number Diff line change
@@ -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()
Loading