Skip to content

Commit 79604d6

Browse files
committed
Added Dungeon Escape game in Python
1 parent c61e8c6 commit 79604d6

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

INDEX.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,6 @@ By now we have 2 numbers (variables), you and computer
112112

113113
### 🎯 [Guess the number](./Python/Guess_the_number/)
114114
- Language: Python
115+
116+
### 🎯 [Dungeon Escape](./Python/Dungeon_escape/)
117+
- Language: Python

Python/Dungeon_escape/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Dungeon Escape
2+
3+
## Description
4+
A console-based maze adventure!
5+
Navigate a 5×5 dungeon using **N/S/E/W** commands to find the exit (`E`) while avoiding traps (`X`).
6+
You start with 3 lives — lose them all, and the dungeon wins!
7+
8+
---
9+
10+
## How to Run
11+
```bash
12+
python dungeon_escape.py
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import random
2+
3+
# Dungeon size
4+
ROWS, COLS = 5, 5
5+
6+
# Symbols
7+
EMPTY, PLAYER, EXIT, TRAP = '.', 'P', 'E', 'X'
8+
9+
def create_dungeon():
10+
dungeon = [[EMPTY for _ in range(COLS)] for _ in range(ROWS)]
11+
exit_row, exit_col = random.randint(0, ROWS-1), random.randint(0, COLS-1)
12+
dungeon[exit_row][exit_col] = EXIT
13+
14+
# Random traps
15+
for _ in range(5):
16+
r, c = random.randint(0, ROWS-1), random.randint(0, COLS-1)
17+
if dungeon[r][c] == EMPTY:
18+
dungeon[r][c] = TRAP
19+
20+
# Place player
21+
while True:
22+
pr, pc = random.randint(0, ROWS-1), random.randint(0, COLS-1)
23+
if dungeon[pr][pc] == EMPTY:
24+
dungeon[pr][pc] = PLAYER
25+
break
26+
return dungeon, pr, pc
27+
28+
def display_dungeon(dungeon):
29+
for row in dungeon:
30+
print(' '.join(row))
31+
print()
32+
33+
def move_player(dungeon, pr, pc, direction):
34+
dungeon[pr][pc] = EMPTY
35+
if direction == 'N': pr -= 1
36+
elif direction == 'S': pr += 1
37+
elif direction == 'E': pc += 1
38+
elif direction == 'W': pc -= 1
39+
pr, pc = max(0, min(ROWS-1, pr)), max(0, min(COLS-1, pc))
40+
cell = dungeon[pr][pc]
41+
dungeon[pr][pc] = PLAYER
42+
return pr, pc, cell
43+
44+
def play():
45+
dungeon, pr, pc = create_dungeon()
46+
lives = 3
47+
48+
print("🏰 Welcome to Dungeon Escape!")
49+
print("Find the exit (E) and avoid traps (X). Move with N/S/E/W.\n")
50+
51+
while True:
52+
display_dungeon(dungeon)
53+
move = input("Move (N/S/E/W): ").upper()
54+
if move not in ['N', 'S', 'E', 'W']:
55+
print("Invalid move. Try again.")
56+
continue
57+
58+
pr, pc, cell = move_player(dungeon, pr, pc, move)
59+
60+
if cell == EXIT:
61+
display_dungeon(dungeon)
62+
print("🎉 You escaped the dungeon! You win!")
63+
break
64+
elif cell == TRAP:
65+
lives -= 1
66+
print(f"💀 You hit a trap! Lives left: {lives}")
67+
if lives == 0:
68+
print("Game Over! You couldn’t escape.")
69+
break
70+
71+
if __name__ == "__main__":
72+
play()

0 commit comments

Comments
 (0)