Skip to content

Commit aa3ddd1

Browse files
authored
Merge pull request #78 from ishaa05/memory_flip
Memory flip game
2 parents c61e8c6 + 7e1b8fa commit aa3ddd1

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

INDEX.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ 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
118+
119+
### 🎯 [Memory Flip](./Python/Memory_Flip/)
120+
- 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()

Python/Memory_Flip/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Memory Flip Game (Python, Tkinter)
2+
3+
## Description
4+
A simple GUI memory game built with **Tkinter**.
5+
Flip two cards at a time to find matching pairs — match all to win!
6+
7+
8+
---
9+
10+
## How to Run
11+
```bash
12+
python memory_flip.py

Python/Memory_Flip/memory_flip.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import tkinter as tk
2+
import random
3+
from functools import partial
4+
5+
class MemoryGame:
6+
def __init__(self, root):
7+
self.root = root
8+
self.root.title("🧠 Memory Flip Game")
9+
self.cards = list('AABBCCDDEEFF')
10+
random.shuffle(self.cards)
11+
self.buttons = []
12+
self.flipped = []
13+
self.create_board()
14+
self.matched = 0
15+
16+
def create_board(self):
17+
for i in range(4):
18+
row = []
19+
for j in range(3):
20+
btn = tk.Button(self.root, text='❓', width=8, height=4,
21+
command=partial(self.flip_card, i, j))
22+
btn.grid(row=i, column=j, padx=5, pady=5)
23+
row.append(btn)
24+
self.buttons.append(row)
25+
26+
def flip_card(self, i, j):
27+
idx = i * 3 + j
28+
btn = self.buttons[i][j]
29+
if btn['text'] == '❓' and len(self.flipped) < 2:
30+
btn['text'] = self.cards[idx]
31+
self.flipped.append((i, j))
32+
if len(self.flipped) == 2:
33+
self.root.after(800, self.check_match)
34+
35+
def check_match(self):
36+
(i1, j1), (i2, j2) = self.flipped
37+
b1, b2 = self.buttons[i1][j1], self.buttons[i2][j2]
38+
if b1['text'] == b2['text']:
39+
b1['state'] = b2['state'] = 'disabled'
40+
self.matched += 2
41+
if self.matched == len(self.cards):
42+
tk.Label(self.root, text="🎉 You won!", font=('Arial', 14)).grid(row=5, column=0, columnspan=3)
43+
else:
44+
b1['text'] = b2['text'] = '❓'
45+
self.flipped.clear()
46+
47+
if __name__ == "__main__":
48+
root = tk.Tk()
49+
game = MemoryGame(root)
50+
root.mainloop()

0 commit comments

Comments
 (0)