/
world.py
232 lines (198 loc) · 6.94 KB
/
world.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
import enemies
import npc
import random
from colorama import init, Fore, Back, Style
init()
class MapTile:
def __init__(self, x, y):
self.x = x
self.y = y
self.name = None
def intro_text(self):
raise NotImplementedError("Create a subclass instead!")
def modify_player(self, player):
pass
class StartTile(MapTile):
def intro_text(self):
return """
You wake up in the middle of the woods. It's pretty dark out
and you are soaking wet! You seem lost in direction and
have no idea where you are
"""
class ForestTrailTile(MapTile):
def intro_text(self):
return """
The tree branches are swaying, and the crickets are playing
a song in the background. The trail is barly visable with the
full moon shining your path.
"""
class VictoryTile(MapTile):
def modify_player(self, player):
player.victory = True
def intro_text(self):
return """
You escape the quarantine zone!
"""
class EnemyTile(MapTile):
def __init__(self, x, y):
self.enemy_killed = False
r = random.random()
if r < 0.50:
self.enemy = enemies.Zombie()
self.alive_text = "\nA zombie sets its sights on you and runs at you! \n"
self.dead_text = "\nThe zombies drops to the ground. \n"
elif r < 0.80:
self.enemy = enemies.ZombieDog()
self.alive_text = "\nA zombie dog growls and lunges at you! \n"
self.dead_text = "\nThe zombies drops to the ground. \n"
elif r < 0.95:
self.enemy = enemies.ZombieBear()
self.alive_text = """\n
A zombie bear is sitting down eating a human
a human corpse. It turns his head around and makes eye contact.
The zombie bear lunges at you. \n
"""
self.dead_text = "\nThe zombies bear drops to the ground shaking everything. \n"
else:
self.enemy = enemies.MutantCreature()
self.alive_text = """\n
A mutant zombie that is times the size of a regular human
slams onto the ground in front of you. \n
"""
self.dead_text = """\n
The mutant starts to grow 6 times the size and explodes.
blood and flesh fly everywhere. \n
"""
super().__init__(x, y)
def intro_text(self):
if self.enemy_killed:
text = ""
return text
elif not self.enemy_killed:
text = self.alive_text if self.enemy.is_alive() else self.dead_text
return text
def modify_player(self, player):
if self.enemy.is_alive():
player.hp = player.hp - self.enemy.damage
print(Fore.RED + "Enemy does {} damage. You have {} HP remaining.".
format(self.enemy.damage, player.hp))
print(Style.RESET_ALL)
elif not self.enemy.is_alive():
self.enemy_killed = True
print("Nothing of interest here. Just an enemy you vanquished.\n")
class TraderTile(MapTile):
def __init__(self, x, y):
self.trader = npc.Trader()
super().__init__(x, y)
def trade(self, buyer, seller):
for i, item in enumerate(seller.inventory, 1):
print("{}. {} - {} Gold".format(i, item.name, item.value))
while True:
user_input = input("Choose an item or press Q to exit: ")
if user_input in ["Q", "q"]:
return
else:
try:
choice = int(user_input)
to_swap = seller.inventory[choice - 1]
self.swap(seller, buyer, to_swap)
except ValueError:
print("Invalid choice!")
def swap(self, seller, buyer, item):
if item.value > buyer.gold:
print("That's to exspensive")
return
seller.inventory.remove(item)
buyer.inventory.append(item)
seller.gold = seller.gold + item.value
buyer.gold = buyer.gold - item.value
print("Trade complete!")
def check_if_trade(self, player):
while True:
print("Would you like to (B)uy, (S)ell, or (Q)uit?")
user_input = input()
if user_input in ["Q", "q"]:
return
elif user_input in ["B", "b"]:
print("Here's whats available to buy: ")
self.trade(buyer=player, seller=self.trader)
elif user_input in ["S", "s"]:
print("Here's whats available to sell: ")
self.trade(buyer=self.trader, seller=player)
else:
print("Invalid choice!")
def intro_text(self):
return """
A creepy old man hidden in an alley hollers at you to come
see what he has for sale.
"""
class FindGoldTile(MapTile):
def __init__(self, x, y):
self.gold = random.randint(1, 50)
self.gold_claimed = False
super().__init__(x, y)
def modify_player(self, player):
if not self.gold_claimed:
self.gold_claimed = True
player.gold = player.gold + self.gold
print("+{} gold added.".format(self.gold))
def intro_text(self):
if self.gold_claimed:
return """
Nothing here is of any interest to you.
"""
else:
return """
Someone left some gold laying around! You pick it up.
"""
world_dsl = """
|EN|EN|VT|EN|EN|
|EN| | | |EN|
|EN|FG|EN| |TT|
|TT| |ST|FG|EN|
|FG| |EN| |FG|
"""
world_map = []
def is_dsl_valid(dsl):
if dsl.count("|ST|") != 1:
return False
if dsl.count("|VT|") == 0:
return False
lines = dsl.splitlines()
lines = [l for l in lines if l]
pipe_counts = [line.count("|") for line in lines]
for count in pipe_counts:
if count != pipe_counts[0]:
return False
return True
tile_type_dict = {"VT": VictoryTile,
"EN": EnemyTile,
"ST": StartTile,
"FG": FindGoldTile,
"TT": TraderTile,
" ": None}
world_map = []
start_tile_location = None
def parse_world_dsl():
if not is_dsl_valid(world_dsl):
raise SyntaxError("DSL is invalid!")
dsl_lines = world_dsl.splitlines()
dsl_lines = [x for x in dsl_lines if x]
for y, dsl_row in enumerate(dsl_lines):
row = []
dsl_cells = dsl_row.split("|")
dsl_cells = [c for c in dsl_cells if c]
for x, dsl_cell in enumerate(dsl_cells):
tile_type = tile_type_dict[dsl_cell]
if tile_type == StartTile:
global start_tile_location
start_tile_location = x, y
row.append(tile_type(x, y) if tile_type else None)
world_map.append(row)
def tile_at(x, y):
if x < 0 or y < 0:
return None
try:
return world_map[y][x]
except IndexError:
return None