-
Notifications
You must be signed in to change notification settings - Fork 0
/
Area.py
193 lines (154 loc) · 5.79 KB
/
Area.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
from utils import text_wrap, print_loading_anim
from random import randint
from time import sleep
from ascii_text import get_ascii
from Combat import Enemy, Combat
import os
class Area():
def __init__(self, name, description="", n=None, e=None, s=None, w=None):
self.name = name
self.description = description
self.items = []
self.npcs = []
self.north = n
self.east = e
self.south = s
self.west = w
def search(self):
print(" Searching area. ", end="\r")
sleep(0.7)
print(" Searching area.. ", end="\r")
sleep(0.7)
print(" Searching area... ")
sleep(0.7)
if len(self.items) == 0:
print("You didnt find anything")
else:
print("\n You found:")
for obj in self.items:
print(" " + str(obj))
def add_npcs(self, npcs):
for npc in npcs:
if npc not in self.npcs:
self.npcs.append(npc)
def search_NPCs(self, search):
simpleSearch = search.lower().replace(" ", "")
best_option = None
best_score = float("inf") # Lower is better
for npc in self.npcs:
simple_npc_name = npc.name.lower().replace(" ", "")
if simpleSearch in simple_npc_name:
score = len(simple_npc_name.replace(simpleSearch, ""))
if score < best_score:
best_option = npc
best_score = score
return best_option
def set_link(self, direction, area):
if direction == "n":
self.north = area
area.south = self
if direction == "e":
self.east = area
area.west = self
if direction == "s":
self.south = area
area.north = self
if direction == "w":
self.west = area
area.east = self
def get_map(self):
top_line = ""
mid_line = ""
bottom_line = ""
if self.west:
if self.west.west != None: mid_line += "... "
mid_line += self.west.name + " --- "
else:
mid_line += " "
mid_line += "[ {} ]".format(self.name)
if self.east:
mid_line += " --- " + self.east.name
if self.east.east != None: mid_line += " ..."
else:
mid_line += " "
middle_padding = round(len(mid_line)/2) * " "
if self.north:
north_name = self.north.name
if self.north.west != None: north_name = "... " + north_name
if self.north.east != None: north_name += " ..."
padding = (round((len(mid_line) - len(north_name)) / 2)+1) * " "
top_line = padding + north_name + "\n" + middle_padding + "|"
if self.south:
south_name = self.south.name
if self.south.west != None: south_name = "... " + south_name
if self.south.east != None: south_name += " ..."
padding = round((len(mid_line) - len(south_name)) / 2) * " "
bottom_line = middle_padding + "|" + "\n" + padding + south_name
_ret = "{0}======== Area Map ========{0}\n\n".format((round(len(mid_line)/2) - 14) * " ")
if top_line != "":
_ret += top_line + "\n"
else:
_ret += "\n\n"
_ret += mid_line
if bottom_line != "": _ret += "\n" + bottom_line
return "\n\n" + _ret
def __str__(self):
name = get_ascii(self.name).split("\n")
log = name
log.append("")
log.append("\n".join(text_wrap(self.description)) + "\n")
for npc in self.npcs:
log.append(str(npc))
log.append("")
# if self.north != None: log.append("North is " + self.north.name)
# if self.east != None: log.append("East is " + self.east.name)
# if self.south != None: log.append("South is " + self.south.name)
# if self.west != None: log.append("West is " + self.west.name)
log.append(self.get_map())
log.append("")
return "\n".join(log) + "\n"
class World():
def __init__(self, start, player):
self.start = start
self.locations = [start]
self.current_area = start
self.player = player
def __str__(self):
return str(self.start)
def move(self, direction):
nextArea = None
if direction == "n": nextArea = self.current_area.north
if direction == "e": nextArea = self.current_area.east
if direction == "s": nextArea = self.current_area.south
if direction == "w": nextArea = self.current_area.west
direction_names = {
"n": "North",
"e": "East",
"s": "South",
"w": "West"
}
if nextArea != None:
distance = 3
header = " Travelling " + direction_names[direction] + " to " + nextArea.name
print()
safe = None
for i in range(0, distance):
print(header + ("." * i), end="\r")
sleep(0.4)
if randint(0,20) == 1:
rand_encounter = Combat(self.player)
safe = rand_encounter.combat_loop()
break
if safe == None:
print(header + "... You made it safely.", end ="\r")
self.current_area = nextArea
sleep(1)
elif safe == True:
print()
print_loading_anim("You made it to " + nextArea.name + " safely.", 3)
self.current_area = nextArea
else:
print_loading_anim("You stumble back to " + self.current_area.name + ", defeated", 3)
return True
else:
return False