-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
316 lines (298 loc) · 9.79 KB
/
main.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import time
import random
player_name = "player"
stat = {
"inventory_stick": 0,
"inventory_empty": 1,
"monster_alive": 1,
"monster_beaten": 0,
"candy_eaten": 0,
"candy_cave": 1 ,
"": 1
}
situation = ""
score = 0
situations = {
# This dictionary contains the possible situations the player may face.
# Each situation is a dictionary that contains the number of cases,
# the functions to be executed in the situation, the description of
# the situation, the prompt, and the resulting situation of each choice.
"initial":
{
"cases": 1,
"func": [],
"desc": [["You're lost in the mountains.",
"In front of you are two caves.",
"There seems to be a fire lit inside the left cave.",
"The right cave is so dark you can't see anything.",
"Which cave will you enter?"]],
"prompt": [["Left Cave.", "Left Cave.",
"Right Cave.", "Right Cave."]],
"results": [["cave1", "cave1_walk", "cave2", "cave2_empty"]],
"availability": [["monster_alive", "monster_beaten",
"inventory_empty","inventory_stick"]]
},
"cave1":
{
"cases": 1,
"func": [],
"desc": [["You step inside the cave.",
"You hear distant footsteps from inside. What to do?"]],
"prompt": [["Shout \"Hello\".", "Continue Walking.", "Go back."]],
"results": [["cave1_hello", "cave1_walk", "initial"]],
"availability": [["", "", ""]]
},
"cave2":
{
"cases": 1,
"func": [],
"desc": [["You step inside the cave. You're scared.",
"You step on an object. What to do?"]],
"prompt": [["Pick it up.", "Continue walking.",
"Continue walking.", "Go back."]],
"results": [["cave2_obj", "cave2_walk",
"cave2_walk_empty", "initial"]],
"availability": [["", "candy_cave",
"candy_eaten", ""]]
},
"cave2_empty":
{
"cases": 1,
"func": [],
"desc": [["You're inside the cave.",
"It's dark. What to do?"]],
"prompt": [["Continue walking.", "Continue walking.", "Go back."]],
"results": [["cave2_walk", "cave2_walk_empty", "initial"]],
"availability": [["candy_cave", "candy_eaten", ""]]
},
"cave2_obj":
{
"cases": 1,
"func": ["pickup_stick","score+1"],
"desc": [["You now have a stick.",
"Fantastick! (+1 point ^ω^)"]],
"prompt": [["Continue."]],
"results": [["cave2_empty"]],
"availability": [[""]]
},
"cave2_walk":
{
"cases": 1,
"func": ["score+1", "pickup_candy"],
"desc": [["You walk further inside the cave.",
"You find some candy to take with you.",
"Nice. (+1 point ^ω^)"]],
"prompt": [["Continue.", "Continue."]],
"results": [["cave2_empty", "cave2"]],
"availability": [["inventory_stick", "inventory_empty"]]
},
"cave2_walk_empty":
{
"cases": 1,
"func": [""],
"desc": [["You walk further inside the cave.",
"There's nothing here."]],
"prompt": [["Go back.","Go back."]],
"results": [["cave2","cave2_empty"]],
"availability": [["inventory_empty","inventory_stick"]]
},
"cave1_hello":
{
"cases": 2,
"func": [],
"desc": [["The footsteps get louder. ",
"You see a monster running towards you.",
"What to do?."],
["Your voice echoes through the cave. ",
"The footsteps become quieter.",
"What to do?."]],
"prompt": [["Run.", "Fight it with your fist.",
"Beat it with your stick."],
["Continue walking inside.", "Go back."]],
"results": [["escape_monster", "fight_monster", "beat_monster"],
["cave1_walk", "initial"]],
"availability": [["", "", "inventory_stick"], ["", ""]]
},
"cave1_walk":
{
"cases": 1,
"func": ["show_score"],
"desc": [["You walk deep inside the left cave.", "You find a map.",
"Now you can find your way home!", "Yᴏᴜ Wɪɴ."]],
"prompt": [["Main menu."]],
"results": [["menu"]],
"availability": [[""]]
},
"escape_monster":
{
"cases": 1,
"func": [],
"desc": [["You are now outside.",
"The monster is still after you. What to do?"]],
"prompt": [["Go into the other cave.",
"Fight it with your fist.",
"Beat it with your stick.", "Give up."]],
"results": [["escape_cave2", "fight_monster",
"beat_monster", "give_up"]],
"availability": [["", "", "inventory_stick", ""]]
},
"fight_monster":
{
"cases": 1,
"func": ["score-1"],
"desc": [["You try to fight the monster with your fist.",
"It's too strong for you.","You are hurt (-1 point T_T)."]],
"prompt": [["Beat it with your stick.","Give up."]],
"results": [["beat_monster","give_up"]],
"availability": [["inventory_stick",""]]
},
"beat_monster":
{
"cases": 1,
"func": ["monster","score+1"],
"desc": [["You beat the monster with your stick.",
"It falls to the ground.","(+2 points ^ω^)"]],
"prompt": [["Continue."]],
"results": [["relax"]],
"availability": [[""]]
},
"escape_cave2":
{
"cases": 2,
"func": ["show_score"],
"desc": [["Dead End. The monster eats you.", "Gᴀᴍᴇ Oᴠᴇʀ."],
["The monster loses its sight of you.",
"It wanders into the forest.",
"You escaped the monster. (+1 points ^ω^)"]],
"prompt": [["Main menu."], ["Continue."]],
"results": [["menu"],["relax"]],
"availability": [[""],[""]]
},
"relax":
{
"cases": 1,
"func": ["score+1"],
"desc": [["You sit outside for a while to take your breath.",
"You're proud of yourself and ready to get up agian.",]],
"prompt": [["Continue."]],
"results": [["initial"]],
"availability": [[""]]
},
"give_up":
{
"cases": 1,
"func": ["show_score"],
"desc": [["You are hopeless.", "You let the monster eat you.",
"Gᴀᴍᴇ Oᴠᴇʀ."]],
"prompt": [["Main menu."]],
"results": ["menu"],
"availability": [[""]]
}
}
def print_pause(s, t=1):
'''
Function to print a string "s" and wait for "t" seconds
'''
print(s)
time.sleep(t)
def prompt(options,available):
'''
Input function to keep prompting users with prompts "options"
until a valid option number is chosen.
'''
option_numbers = []
last_number = 1
for i in available:
if(stat[i]):
option_numbers.append(last_number)
last_number+=1
else:
option_numbers.append(-1)
while True:
for i in range(len(options)):
if option_numbers[i] == -1:
continue
print("("+str(option_numbers[i])+") "+options[i])
try:
choice = int(input("Input choice number: "))
if choice < last_number:
return option_numbers.index(choice)
else:
print("Please choose a valid option")
except:
print("Please choose a valid option")
def init():
'''
Game Initialization
'''
global player_name
print_pause("Welcome to the game.", 3)
player_name = str(input("Please enter your name: "))
def func(f):
'''
Execute functions demanded by the game situation.
'''
global score
for s in f:
if(s == "show_score"):
print("Your score is: " + str(score) + " points.")
if(s == "pickup_stick"):
stat["inventory_empty"] = 0
stat["inventory_stick"] = 1
if(s == "pickup_candy"):
stat["candy_eaten"] = 1
stat["candy_cave"] = 0
if(s == "monster"):
stat["monster_alive"] = 0
stat["monster_beaten"] = 1
if(s[:5] == "score"):
if(s[5] == "+"):
score+=int(s[6:])
else:
score-=int(s[6:])
def reset_stats():
'''
Reset game stats.
'''
global score
score = 0
stat["inventory_empty"] = 1
stat["inventory_stick"] = 0
stat["candy_eaten"] = 0
stat["candy_cave"] = 1
stat["monster_alive"] = 1
stat["monster_beaten"] = 0
def gameplay():
'''
Process Gameplay.
'''
situation = "initial"
while situation != "menu":
situation_case = random.randrange(0,situations[situation]["cases"])
for i in situations[situation]["desc"][situation_case]:
print_pause(i,1 if len(i) < 35 else 2)
func(situations[situation]["func"])
choice = prompt(situations[situation]["prompt"][situation_case],
situations[situation]["availability"][situation_case])
situation = situations[situation]["results"][situation_case][choice]
def menu():
'''
Menu Screen
'''
global player_name
reset_stats()
print_pause("Hi, " + player_name +
"! Please choose one of the options below"+
" by typing its number:")
menu_choice = int(prompt(["Start New Game","Exit"], ["", ""]))
if menu_choice == 0:
print_pause("Starting game...", 1)
print_pause("\n", 2)
gameplay()
return
elif menu_choice == 1:
quit()
if player_name == "player":
init()
while True:
menu()