-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.py
More file actions
127 lines (112 loc) · 3.8 KB
/
map.py
File metadata and controls
127 lines (112 loc) · 3.8 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
from utils import randbool
from utils import randcell
from utils import randcell2
# 0 - pole
# 1 - derevo
# 2 - reka
# 3 - hospital
# 4 - upgrade-shop
# 5 - fire
CELL_TYPES = "🟩🌲🌊🏥🏦🔥"
TREE_BONUS = 100
UPGRADE_COST = 5000
LIFE_COST = 10000
class Map:
def __init__(self, w, h):
self.w = w
self.h = h
self.cells = [[0 for i in range(w)] for j in range(h)]
self.generate_forest(5,10)
self.generate_river(10)
self.generate_river(10)
self.generate_upgrade_shop()
self.generate_hospital()
def check_bounds(self, x, y):
if (x < 0 or y < 0 or x >= self.h or y >= self.w):
return False
return True
def print_map(self, helico, clouds):
print('⬛' * (self.w + 2))
for ri in range(self.h):
print('⬛', end='')
for ci in range(self.w):
cell = self.cells[ri][ci]
if (clouds.cells[ri][ci]==1):
print('⬜', end='')
elif (clouds.cells[ri][ci]==2):
print('🟥', end='')
elif (helico.x == ri and helico.y == ci):
print('🚁', end='')
elif (cell>= 0 and cell < len(CELL_TYPES)):
print(CELL_TYPES[cell], end='')
print('⬛')
print('⬛' * (self.w + 2))
def generate_river(self, l):
rc = randcell(self.w, self.h)
rx, ry = rc[0], rc[1]
self.cells[rx][ry] = 2
while l > 0:
rc2 = randcell2(rx, ry)
rx2, ry2 = rc2[0], rc2[1]
if (self.check_bounds(rx2, ry2)):
self.cells[rx2][ry2] = 2
rx, ry = rx2, ry2
l -= 1
def generate_forest(self, r, mxr):
for ri in range(self.h):
for ci in range(self.w):
if randbool(r, mxr):
self.cells[ri][ci] = 1
def generate_tree(self):
c = randcell(self.w, self.h)
cx, cy = c[0], c[1]
if(self.cells[cx][cy] == 0):
self.cells[cx][cy] = 1
def generate_upgrade_shop(self):
c = randcell(self.w, self.h)
cx, cy = c[0], c[1]
self.cells[cx][cy] = 4
def generate_hospital(self):
c = randcell(self.w, self.h)
cx, cy = c[0], c[1]
if self.cells[cx][cy] != 4:
self.cells[cx][cy] = 3
else:
self.generate_hospital()
def add_fire(self):
c = randcell(self.w, self.h)
cx, cy = c[0], c[1]
if self.cells[cx][cy] == 1:
self.cells[cx][cy] = 5
def update_fires(self, helico):
for ri in range(self.h):
for ci in range(self.w):
cell = self.cells[ri][ci]
if cell == 5:
self.cells[ri][ci] = 0
helico.score -= 20
for i in range(10):
self.add_fire()
def process_helicopter(self, helico, clouds):
c = self.cells[helico.x][helico.y]
d = clouds.cells[helico.x][helico.y]
if (c == 2):
helico.tank = helico.mxtank
if (c == 5 and helico.tank > 0):
helico.tank -= 1
helico.score += TREE_BONUS
self.cells[helico.x][helico.y] = 1
if (c==4 and helico.score >= UPGRADE_COST):
helico.mxtank += 1
helico.score -= UPGRADE_COST
if (c==3 and helico.score >= LIFE_COST):
helico.lives += 10
helico.score -= LIFE_COST
if (d==2):
helico.lives -= 1
if (helico.lives == 0):
helico.game_over()
def export_date(self):
return {'cells': self.cells}
def import_data(self, data):
self.cells = data['cells'] or [[0 for i in range(self.w)] for j in range(self.h)]