-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
229 lines (216 loc) · 9.37 KB
/
Copy pathmain.py
File metadata and controls
229 lines (216 loc) · 9.37 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import time
import re
import random
import winsound
import anim
from quest_SPOILERS import quest, ignore_re
def sound(s: str) -> None:
"""
Play a sound. If you are not on Windows, this is the perfect place to change the function
:param s: path to sound file (wav)
:return: None
"""
winsound.PlaySound(s, winsound.SND_ASYNC)
inventory: set[str] = set()
def showroom(room: str, prevroomid: str) -> str:
"""
Show a room
:param room: room id to show
:param prevroomid: previous room id
:return: next room id
"""
if not handlemissing(room):
return "start", prevroomid
if quest[room]["image"] != "":
image: str = quest[room]["image"]
anim_ended = False
else:
image: str = ""
anim_ended = True
frame: int = 0
if quest[room]["sound"] != "":
sound(quest[room]["sound"])
if image != "" and not anim_ended:
im, anim_ended = anim.getanim(image, frame)
print("\n".join(im)+"\n", end="")
parts: list[str] = quest[room]["text"].split("&")
for i in range(len(parts)):
if i % 2 == 1:
if parts[i][0].lower() == "s": # play sound
sound(parts[i][1:])
elif parts[i][0].lower() == "t": # wait
t = time.time()
while time.time() - t < float(parts[i][1:]):
if image != "" and not anim_ended:
im, anim_ended = anim.getanim(image, frame)
pr = []
for j in range(i):
if j % 2 == 0:
pr.append(parts[j])
print("\n".join(im)+"\n"+"".join(pr), end="")
frame += 1
time.sleep(0.1)
elif parts[i][0].lower() == "i": # show image
image = parts[i][1:]
anim_ended = False
frame = 0
if image != "" and not anim_ended:
im, anim_ended = anim.getanim(image, frame)
else:
im = []
pr = []
for j in range(i):
if j % 2 == 0:
pr.append(parts[j])
print("\n".join(im)+"\n"+"".join(pr), end="")
frame += 1
time.sleep(0.1)
elif parts[i][0].lower() == "w": # wait for animation to end
while not anim_ended:
im, anim_ended = anim.getanim(image, frame)
pr = []
for j in range(i):
if j % 2 == 0:
pr.append(parts[j])
print("\n".join(im)+"\n"+"".join(pr), end="")
frame += 1
time.sleep(0.1)
elif parts[i][0].lower() == "r": # Generate new random value in random inventory
if random.randint(0, 1) == 1:
inventory.add("random")
elif "random" in inventory:
inventory.remove("random")
elif parts[i][0] == "+": # add item to inventory
inventory.add(parts[i][1:])
elif parts[i][0] == "-": # delete item from inventory
if parts[i][1:] in inventory:
inventory.remove(parts[i][1:])
elif parts[i][0] in ("=", "!"): # print the text in triangular brackets<> only if item exists / doesn't
if (parts[i][1:] in inventory and parts[i][0] == "=") or (parts[i][1:] not in inventory and
parts[i][0] == "!"):
if ">" in parts[i + 1]:
idx: int = parts[i + 1].index(">")
parts[i + 1] = parts[i + 1][1:idx] + parts[i + 1][idx + 1:]
else:
parts[i + 1] = parts[i + 1][1:]
brackets: int = 1
b: bool = False
for j in range(i + 1, len(parts)):
if j % 2 == 0:
for k in range(len(parts[j])):
if "<" == parts[j][k]:
brackets += 1
if ">" == parts[j][k]:
brackets -= 1
if brackets == 0:
parts[j] = parts[j][:k] + parts[j][k + 1:]
b = True
break
if b:
break
else:
if ">" in parts[i + 1]:
parts[i + 1] = parts[i + 1][parts[i + 1].index(">") + 1:]
else:
parts[i + 1] = ""
brackets = 1
b: bool = False
for j in range(i + 1, len(parts)):
if j % 2 == 0:
for k in range(len(parts[j])):
if "<" == parts[j][k]:
brackets += 1
if ">" == parts[j][k]:
brackets -= 1
if brackets == 0:
parts[j] = parts[j][k + 1:]
b = True
break
if b:
break
parts[j] = ""
else:
parts[j] = "#"
else:
print(parts[i], end="")
print("\n", end="")
while not anim_ended:
im, anim_ended = anim.getanim(image, frame)
pr = []
for i in range(len(parts)):
if i % 2 == 0:
pr.append(parts[i])
print("\n".join(im) + "\n" + "".join(pr)+"\n", end="")
frame += 1
time.sleep(0.1)
options: dict[str: str] = getoptions(room)
chosen: str = re.sub(ignore_re, "", input()).strip()
while chosen not in options:
print("Sorry, I didn't understand.\n", end="")
chosen: str = re.sub(ignore_re, "", input()).strip()
if options[chosen] == "return":
options = getoptions(prevroomid)
chosen: str = re.sub(ignore_re, "", input()).strip()
while chosen not in options:
print("Sorry, I didn't understand.\n", end="")
chosen: str = re.sub(ignore_re, "", input()).strip()
return options[chosen], prevroomid
return options[chosen], room
def handlemissing(room: str) -> bool:
"""
Handle missing parametrs
:param room: room id
:return: should the program continue
"""
if room not in quest:
print(f"The room \"{room}\" doesn't exist. Sending back to start.")
return False
if "text" not in quest[room]:
quest[room]["text"] = ""
if "options" not in quest[room]:
quest[room]["options"] = [(f"The room \"{room}\" doesn't have any options. Go to start.", "s", "start")]
if "hiddenoptions" not in quest[room]:
quest[room]["hiddenoptions"] = {}
if "image" not in quest[room]:
quest[room]["image"] = ""
if "sound" not in quest[room]:
quest[room]["sound"] = ""
return True
def getoptions(room: str) -> dict[str: str]:
"""
Gets the options for a room
:param room: room id
:return: options
"""
options: dict[tuple[str, ...]: str] = {}
for i, j in quest[room]["hiddenoptions"].items():
if isinstance(i, str):
options[i] = j
else:
for k in i:
options[k] = j
toprint: list[str] = []
for i in range(len(quest[room]["options"])):
if isinstance(quest[room]["options"][i][1], str):
quest[room]["options"][i] = (quest[room]["options"][i][0], (quest[room]["options"][i][1],)
) + quest[room]["options"][i][2:]
if len(quest[room]["options"][i]) > 3:
for j in range(3, len(quest[room]["options"][i])):
if (quest[room]["options"][i][j][0] == "=" and quest[room]["options"][i][j][1:] not in inventory) or (
quest[room]["options"][i][j][0] == "!" and quest[room]["options"][i][j][1:] in inventory):
break
else:
for j in quest[room]["options"][i][1]:
options[j] = quest[room]["options"][i][2]
toprint.append(f"{quest[room]['options'][i][1][0]}: {quest[room]['options'][i][0]}")
else:
for j in quest[room]["options"][i][1]:
options[j] = quest[room]["options"][i][2]
toprint.append(f"{quest[room]['options'][i][1][0]}: {quest[room]['options'][i][0]}")
print("\n".join(toprint)+"\n", end="")
return options
nextroom: str
prevroom: str
nextroom, prevroom = showroom("start", "start")
while nextroom != "end":
nextroom, prevroom = showroom(nextroom, prevroom)