-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice06.py
More file actions
105 lines (86 loc) · 6.03 KB
/
practice06.py
File metadata and controls
105 lines (86 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Adventure Game Text
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
# creating a class for story blocks
class StoryBlock:
def __init__(self, storyText = "", Button1Text = "", Button2Text = "", Button1Block = None, Button2Block = None):
self.storyText = storyText
self.Button1Text = Button1Text
self.Button2Text = Button2Text
self.Button1Block = Button1Block
self.Button2Block = Button2Block
# welcome function to introduce the game to the player
def welcome():
clear_screen()
print("=== THE DUNGEON ESCAPE ===")
print()
print("You find yourself trapped. Every choice matters.")
print("Think carefully — some paths lead to freedom, others to darkness.")
print()
input("Press Enter to begin...")
# functions to handle button clicks
def button1_click(currentBlock):
if currentBlock.Button1Block is not None:
return currentBlock.Button1Block
else:
print("Button 1 is not available.")
return currentBlock
def button2_click(currentBlock):
if currentBlock.Button2Block is not None:
return currentBlock.Button2Block
else:
return None # no next block -> exits the game loop
# main function to run the game
def main():
# welcome the player
welcome()
# create story blocks
block1 = StoryBlock("You wake up with a throbbing headache on a cold stone floor. The air smells of damp and decay. Dim torchlight flickers through a small barred window above you. The last thing you remember is walking home through the forest — now you're locked in a dungeon cell with no idea how long you've been here.", "Call for help", "Examine the door")
block2 = StoryBlock("You pound on the walls and shout at the top of your lungs. Your voice echoes through the corridors, but only silence answers back. Whoever put you here is either long gone — or simply doesn't care. Screaming won't save you.", "Sit down and think", "Examine the door")
block3 = StoryBlock("You approach the heavy iron door. It's old but solid — hinges rusted, frame firmly set in stone. A small keyhole sits just below the handle, clogged with grime. Without a key, this door isn't moving an inch.", "Do nothing, wait for rescue", "Search the room for a key")
block4 = StoryBlock("You take a deep breath and sit against the cold wall. Panicking won't help. You need to be smart about this. The cell is small but you haven't thoroughly searched it yet. There has to be something useful here — a key, a tool, anything.", "Inspect the floor carefully", "Check your pockets")
block5 = StoryBlock("You pat down every pocket — nothing. Whoever took you was thorough. Your phone, wallet, even your house keys are gone. Your pockets are completely empty.", "Inspect the floor carefully", "Check pockets again")
block6 = StoryBlock("You get on your hands and knees and examine the floor inch by inch. Near the far corner, wedged deep in a crack between two stones, you spot something metal. You pry it out — a small iron key, old and coated in rust. Your heart races. This might be your way out.", "Try the key on the door", "Keep searching for something better")
block7 = StoryBlock("You slide the key into the lock with trembling hands. It fits the keyhole — but when you turn it, the key snaps clean in half. Too brittle from years of rust. The broken piece falls to the floor with a faint clink. Dead end.", "Keep searching", "Sit down and rethink")
block8 = StoryBlock("Hours crawl by. Then what feels like days. No one comes. The torchlight outside your window eventually dies out, leaving you in total darkness. Hunger and thirst gnaw at you until you can barely move. You close your eyes one last time...\n\n --- GAME OVER ---", "Restart", "Exit Game")
# linking the story blocks
# block | option 1 (Button1Block) | option 2 (Button2Block)
# --------|---------------------------|-------------------------
block1.Button1Block = block2 # "Call for help" -> block2
block1.Button2Block = block3 # "Examine the door" -> block3
block2.Button1Block = block4 # "Sit down and think" -> block4
block2.Button2Block = block3 # "Examine the door" -> block3
block3.Button1Block = block8 # "Do nothing, wait for rescue" -> block8 (GAME OVER)
block3.Button2Block = block6 # "Search the room for a key" -> block6
block4.Button1Block = block6 # "Inspect the floor carefully" -> block6
block4.Button2Block = block5 # "Check your pockets" -> block5
block5.Button1Block = block6 # "Inspect the floor carefully" -> block6
block5.Button2Block = block5 # "Check pockets again" -> block5 (loops back)
block6.Button1Block = block7 # "Try the key on the door" -> block7
block6.Button2Block = block6 # "Keep searching for something better" -> block6 (loops back)
block7.Button1Block = block6 # "Keep searching" -> block6
block7.Button2Block = block4 # "Sit down and rethink" -> block4
block8.Button1Block = block1 # "Restart" -> block1 (back to start)
block8.Button2Block = None # "Exit Game" -> None (exits the loop)
# start the game
currentBlock = block1
while currentBlock is not None:
clear_screen()
# display the current story block
print(currentBlock.storyText)
# display the options
print(f"1. {currentBlock.Button1Text}")
print(f"2. {currentBlock.Button2Text}")
# get the player's choice
choice = input("Enter your choice (1 or 2): ")
if choice == "1":
currentBlock = button1_click(currentBlock)
elif choice == "2":
currentBlock = button2_click(currentBlock)
else:
print("Invalid choice. Please try again.")
input("Press Enter to continue...")
# run the game
if __name__ == "__main__":
main() # calling the main function to start the game