-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_adventure.py
More file actions
229 lines (201 loc) · 7.37 KB
/
text_adventure.py
File metadata and controls
229 lines (201 loc) · 7.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
'''
This is an example text-based adventure! Play it multiple times to get all the endings and fight randomized bosses
'''
from random import randint, choice
from time import sleep
import os
# from colorama import Fore, Style
def runTextAdventure():
# You can change these constant lists depending on the story you create
yes_no = ["yes", "no"]
directions = ["left", "right"]
moves = ["fight", "run", "dodge", "inspect"]
classes = ["knight", "hunter"]
bosses = ["dragon", "orc"]
def make_decision(prompt, choices):
"""
Returns any valid decision the user makes
"""
while True:
user_input = input(prompt).lower()
if user_input in choices:
return user_input
else:
print("Invalid choice, try again!")
def generate_player():
"""
Returns randomly generated stats depending on the chosen class
"""
player_class = make_decision("> Are you a knight or a hunter?: ", classes)
sleep(1)
if player_class == 'knight':
print("> You are a knight! You are endowed with strength and courage, but you are extremely stubborn!")
return {
"hp": randint(100, 120),
"atk": randint(5, 10),
"spd": randint(10, 55)
}
else:
print("> You are a Hunter! You are endowed with agility and stealth, but you lack vitality!")
return {
"hp": randint(70, 100),
"atk": randint(7, 10),
"spd": randint(35, 60)
}
def generate_boss():
"""
Returns a randomly generated boss with stats
"""
boss_type = choice(bosses)
if boss_type == "dragon":
return {
"type": "dragon",
"hp": randint(100, 130),
"atk": randint(4, 6),
"spd": randint(50, 70)
}
else:
return {
"type": "orc",
"hp": randint(90, 110),
"atk": randint(7, 10),
"spd": randint(20, 40)
}
return None
def print_stats(name):
"""
Prints out the HP stat of the boss and player. Does not return anything.
"""
print("\nHEALTH------------------------------")
print(name.upper() + ":", player["hp"], "HP")
print(boss["type"].upper() + ":", boss["hp"], "HP")
print("------------------------------------\n")
def inspect(name):
"""
Prints out the ATK and SPD stats of the boss and player. Does not return anything.
"""
print("\nSTATS-------------------------------")
print(name.upper()+":", player["atk"], "ATK |", player["spd"], "SPD")
print(boss["type"].upper()+":", boss["atk"], "ATK |", boss["spd"], "SPD")
print("------------------------------------")
def boss_fight(player, name, boss):
"""
Executes the fight with the randomly generated boss. Returns True if the user beats the boss, False if they die, None if they run away.
"""
while True:
print_stats(name)
move = make_decision("> Do you FIGHT, RUN, INSPECT or DODGE?: ", moves)
if move == "fight":
print("\n> You attack!")
boss["hp"] -= randint(player["atk"]-4, player["atk"]+2)
elif move == "dodge":
sleep(1)
# Theoretically, you can regain more health than you intially started with
if randint(0, player["spd"]) > randint(0, boss["spd"]):
print("\n> You dodged! You regained health.")
player["hp"] += randint(5, 10)
continue
else:
print("\n> You failed to dodge!")
elif move == "inspect":
print("\n> You inspected the", boss["type"] + "!")
inspect(name)
else:
if player["spd"] > boss["spd"]:
return None
else:
print("\n> You attempt to run, but you're too slow!")
if boss["hp"] <= 0:
return True
sleep(1)
print("\n>", boss["type"].upper(), "attacks!")
sleep(1)
if randint(0, 9) >= 1:
player["hp"] -= randint(boss["atk"]-2, boss["atk"]+2)
else:
print("> But it misses!")
if player["hp"] <= 0:
return False
# Main game loop
while True:
name = input("> What is your name, adventurer?: ")
player = generate_player()
boss = generate_boss()
# Actual gameplay loop
while True:
# Choice 1
sleep(3)
print("\n> You find yourself lost in a vast forest...")
print("> Your head hurts...")
print("> You look around to get a bearing of your surroundings. It seems you have 2 choices.")
print("> Go LEFT past a nearby mysterious monument")
print("> Go RIGHT past a little stream\n")
sleep(1)
# This is here for the aesthetic, cause your choice doesn't matter in this case
decision = make_decision("> What do you choose, " + name + "?: ", directions)
sleep(2)
# Randomly generate the correct path
if randint(0, 1) == 1:
print("\n> You choose to go", decision + ", but it seems you've gone even deeper into the forest.")
else:
print("\n> You choose to go", decision + ". As you make your way down the path, you are ambushed. You cannot make out who or what attacked you before everything goes black.")
sleep(2)
print("> You died!")
break
# Choice 2
print("> You start to feel uneasy...")
print("> Do you want to turn around?\n")
sleep(1)
# This choice does matter
decision = make_decision("> YES or NO, " + name +"?: ", yes_no)
sleep(2)
if decision == "no":
print("\n> You choose to continue" + ", but you have gotten even more lost!")
else:
print("\n> You choose to go back" + ". While walking, you start to feel extremely nauseous...")
print("> You fall over and pass out!")
sleep(2)
print("> You died!")
break
# Choice 3
print("> Your heart starts pounding...")
print("> You see a path that splits into two directions...\n")
sleep(1)
# This choice doesn't matter in escaping
decision = make_decision("> LEFT or RIGHT, " + name +"?: ", directions)
sleep(3)
# Randomly chooses if you fight or escape
if randint(0, 1) == 1:
print("\n> You choose to go", decision + "...")
else:
print("\n> You choose to go", decision + ". The forest become less dense as you walk down the path.")
sleep(2)
print("> After what seems like an hour, you finally make it out of the forest.")
sleep(2)
print("> You escaped unscathed!")
break
sleep(1)
print("> All of a sudden, the ground shakes!")
print("> A huge", boss["type"], "emerges from the trees!")
win = boss_fight(player, name, boss)
# Checks if the user won, lost or ran away
if win == None:
print("\n> You cowardly run away from the", boss["type"] + "!")
sleep(2)
print("> After what seems like an hour of running, you finally make it out of the forest.")
break
elif win:
print("\n> You defeated the", boss["type"] + "! You claim the loot of your fallen foe and make your way out of the forest!")
break
else:
print("\n> The injuries you sustained from fighting make you collapse... You died!")
break
# Let's the user play again
playAgain = make_decision("\nPlay again (YES/NO)?: ", yes_no)
if playAgain == "no":
break
sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
print("\nThanks for playing!")
if __name__ == "__main__":
runTextAdventure()