-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_map.py
More file actions
333 lines (257 loc) · 10.2 KB
/
Copy pathinitialize_map.py
File metadata and controls
333 lines (257 loc) · 10.2 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os # pause
import numpy # math
import random # random numbers
import time #sleep
import config
class Tile:
def __init__(self, name, front, passable):
self.name = name
self.front = front
self.passable = passable
class Player:
def __init__(self, name, front, x, y):
self.name = name
self.front = front
self.x = x
self.y = y
class Building:
def __init__(self, name, front, size_x, size_y):
self.name = name
self.front = front
self.size_x = size_x
self.size_y = size_y
self.tiles = self.randomize_building(size_x, size_y)
def randomize_building(self, building_size_x, building_size_y):
building_array = self.prepare_floor(building_size_x, building_size_y)
building_array = self.construct_walls(building_array, building_size_x, building_size_y)
building_array = self.add_corners(building_array, building_size_x, building_size_y)
building_array = self.add_doors(building_array, building_size_x, building_size_y)
return building_array
@staticmethod
def prepare_floor(building_size_x, building_size_y):
empty_floor = Tile("floor", ".", True)
return [[empty_floor for col in range(building_size_x)] for row in range(building_size_y)]
@staticmethod
def construct_walls(building_array, building_size_x, building_size_y):
wall_horizontal = Tile("wall", "═", False)
wall_vertical = Tile("wall", "║", False)
building_array[0] = [wall_horizontal] * building_size_x
building_array[building_size_y - 1] = [wall_horizontal] * building_size_x
for y in range(building_size_y - 2 ):
building_array[y + 1][0] = wall_vertical
building_array[y + 1][building_size_x - 1] = wall_vertical
return building_array
@staticmethod
def add_corners(building_array, building_size_x, building_size_y):
corner_left_top = Tile("wall", "╔", False)
corner_left_bottom = Tile("wall", "╚", False)
corner_right_top = Tile("wall", "╗", False)
corner_right_bottom = Tile("wall", "╝", False)
building_array[0][0] = corner_left_top
building_array[0][building_size_x - 1] = corner_right_top
building_array[building_size_y - 1][0] = corner_left_bottom
building_array[building_size_y - 1][building_size_x - 1] = corner_right_bottom
return building_array
@staticmethod
def add_doors(building_array, building_size_x, building_size_y):
door = Tile("door", "D", True)
doorable_walls = [[random.randint(1, building_size_x - 2),0],[random.randint(1, building_size_x - 2),building_size_y - 1],[0, random.randint(1, building_size_y - 2)],[building_size_x - 1, random.randint(1, building_size_y - 2)]]
number_of_doors = random.randint(1,3)
for d in range(number_of_doors):
wall_with_door = random.choice(doorable_walls)
building_array[wall_with_door[1]][wall_with_door[0]] = door
return building_array
def prepare_map():
add_map_boundaries()
spill_lakes()
seed_bushes()
plant_trees()
construct_buildings()
return map_array
def initialize_map():
print("....Creating map.....")
time.sleep(0.5)
empty_tile = Tile("grass", " ", True)
return [[empty_tile for col in range(config.SCREEN_SIZE_X)] for row in range(config.SCREEN_SIZE_Y)]
def add_map_boundaries():
print("....Adding walls.....")
time.sleep(0.1)
wall_horizontal = Tile("wall", "═", False)
wall_vertical = Tile("wall", "║", False)
corner_left_top = Tile("wall", "╔", False)
corner_left_bottom = Tile("wall", "╚", False)
corner_right_top = Tile("wall", "╗", False)
corner_right_bottom = Tile("wall", "╝", False)
map_array[0] = [wall_horizontal] * config.SCREEN_SIZE_X
map_array[config.SCREEN_SIZE_Y - 1] = [wall_horizontal] * config.SCREEN_SIZE_X
for y in range(config.SCREEN_SIZE_Y - 2 ):
map_array[y + 1][0] = wall_vertical
map_array[y + 1][config.SCREEN_SIZE_X - 1] = wall_vertical
map_array[0][0] = corner_left_top
map_array[0][config.SCREEN_SIZE_X - 1] = corner_right_top
map_array[config.SCREEN_SIZE_Y - 1][0] = corner_left_bottom
map_array[config.SCREEN_SIZE_Y - 1][config.SCREEN_SIZE_X - 1] = corner_right_bottom
def spill_lakes():
print("....Spilling lakes...")
time.sleep(0.1)
lake_tile = Tile("water", "≈", False)
for lake in range(config.NUMBER_OF_LAKES):
lake_x = random.randint(2, (config.SCREEN_SIZE_X - config.LAKE_SIZE) - 2)
lake_y = random.randint(2, (config.SCREEN_SIZE_Y - config.LAKE_SIZE) - 2)
for tile in range(config.LAKE_DENSITY):
x = int(random.gauss(config.LAKE_SIZE, config.LAKE_DENSITY/100)) + lake_x - int(config.LAKE_SIZE/2)
y = int(random.gauss(config.LAKE_SIZE, config.LAKE_DENSITY/100)) + lake_y - int(config.LAKE_SIZE/2)
map_array[y][x] = lake_tile
def seed_bushes():
print("....Seeding bushes...")
time.sleep(0.1)
bush_tile = Tile("bush", "#", False)
bush_spread = [-1,0,0,0,0,1]
for bush_source in range(config.NUMBER_OF_BUSHES):
bush_x = random.randint(2, (config.SCREEN_SIZE_X - 3))
bush_y = random.randint(2, (config.SCREEN_SIZE_Y - 3))
horizontal = bool(random.getrandbits(1))
if (horizontal == True):
for bush in range(config.BUSH_LENGTH):
x = int(random.gauss(config.BUSH_LENGTH, 1) + bush_x - config.BUSH_LENGTH)
y = bush_y + random.choice(bush_spread)
if map_array[y][x].passable == True:
map_array[y][x] = bush_tile
else:
for bush in range(config.BUSH_LENGTH):
y = int(random.gauss(config.BUSH_LENGTH, 1) + bush_y - config.BUSH_LENGTH)
x = bush_x + random.choice(bush_spread)
if map_array[y][x].passable == True:
map_array[y][x] = bush_tile
def plant_trees():
print("....Planting trees...")
time.sleep(0.1)
tree_tile = Tile("tree", "♣", False)
for tree in range(config.NUMBER_OF_TREES):
x = random.randint(2, config.SCREEN_SIZE_X - 2)
y = random.randint(2, config.SCREEN_SIZE_Y - 2)
if map_array[y][x].passable == True:
map_array[y][x] = tree_tile
def construct_buildings():
print("....Making buildings.....")
time.sleep(0.1)
for construction in range(config.NUMBER_OF_BUILDINGS):
building_width = random.randint(config.MIN_BUILDING_SIZE,config.MIN_BUILDING_SIZE)
building_height = random.randint(config.MIN_BUILDING_SIZE,config.MIN_BUILDING_SIZE)
building_x = random.randint(2, config.SCREEN_SIZE_X - building_width - 1)
building_y = random.randint(2, config.SCREEN_SIZE_Y - building_height - 1)
building = Building("Home","H",building_width,building_height).tiles
for y in range(building_height):
for x in range(building_width):
map_array[building_y + y][building_x + x] = building[y][x]
def show_generated_map():
final_map = ""
for y in range(config.SCREEN_SIZE_Y):
front_tiles = []
for node in map_array[y]:
front_tiles.append(node.front)
row = "".join(front_tiles)
final_map = final_map + row + "\n"
print(final_map)
def show_map_statistics():
counted_objects = count_objects()
print("Generated objects:")
for object_key, object_value in counted_objects.items():
print(object_key, " ",object_value)
programPause = input("Press the any key to start")
def spawn_player():
while True:
player_x = random.randint(2, config.SCREEN_SIZE_X - 2)
player_y = random.randint(2, config.SCREEN_SIZE_Y - 2)
if map_array[player_y][player_x].passable == True:
break
return Player("vinne","V", player_x, player_y)
def update_map():
temp_map[player.y][player.x] = player
final_map = ""
for y in range(config.SCREEN_SIZE_Y):
front_tiles = []
for node in temp_map[y]:
front_tiles.append(node.front)
row = "".join(front_tiles)
final_map = final_map + row + "\n"
os.system('cls')
print(final_map)
def count_objects():
# objects [grass, tree, bush, water, wall, floor, door]
objects = {
"grass": 0,
"trees": 0,
"bushes": 0,
"water": 0,
"walls": 0,
"floor": 0,
"doors": 0
}
for row in map_array:
for cell in row:
name = cell.name
if name == "grass":
objects["grass"] += 1
elif name == "tree":
objects["trees"] += 1
elif name == "bush":
objects["bushes"] += 1
elif name == "water":
objects["water"] += 1
elif name == "wall":
objects["walls"] += 1
elif name == "floor":
objects["floor"] += 1
elif name == "door":
objects["doors"] += 1
return objects
def copy_matrix(matrix):
return [row[:] for row in matrix]
def move_player(player):
direction = ""
direction = input("move 'w' 's' 'a' 'd', get info by adding .info or quit: ")
if direction == "w":
if map_array[player.y - 1][player.x].passable == True:
player.y = player.y - 1
else:
player.y = player.y
elif direction == "s":
if map_array[player.y + 1][player.x].passable == True:
player.y = player.y + 1
else:
player.y = player.y
elif direction == "a":
if map_array[player.y][player.x - 1].passable == True:
player.x = player.x - 1
else:
player.x = player.x
elif direction == "d":
if map_array[player.y][player.x + 1].passable == True:
player.x = player.x + 1
else:
player.x = player.x
elif direction == "w.info":
information = "You are looking at " + str(map_array[player.y - 1][player.x].name)
input(information)
elif direction == "s.info":
information = "You are looking at " + str(map_array[player.y + 1][player.x].name)
input(information)
elif direction == "a.info":
information = "You are looking at " + str(map_array[player.y][player.x - 1].name)
input(information)
elif direction == "d.info":
information = "You are looking at " + str(map_array[player.y][player.x + 1].name)
input(information)
## ======================= main program start =========================
## ========================== generate map ============================
map_array = initialize_map()
prepare_map()
show_generated_map()
show_map_statistics()
temp_map = copy_matrix(map_array)
player = spawn_player()
while True:
temp_map = copy_matrix(map_array)
update_map()
move_player(player)