-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathenemy.py
356 lines (297 loc) · 13.6 KB
/
enemy.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import pygame
import random
from bomb import Bomb
from node import Node
from enums.algorithm import Algorithm
class Enemy:
dire = [[1, 0, 1], [0, 1, 0], [-1, 0, 3], [0, -1, 2]]
TILE_SIZE = 4
def __init__(self, x, y, alg):
self.life = True
self.path = []
self.movement_path = []
self.pos_x = x * Enemy.TILE_SIZE
self.pos_y = y * Enemy.TILE_SIZE
self.direction = 0
self.frame = 0
self.animation = []
self.range = 3
self.bomb_limit = 1
self.plant = False
self.algorithm = alg
def move(self, map, bombs, explosions, enemy):
if self.direction == 0:
self.pos_y += 1
elif self.direction == 1:
self.pos_x += 1
elif self.direction == 2:
self.pos_y -= 1
elif self.direction == 3:
self.pos_x -= 1
if self.pos_x % Enemy.TILE_SIZE == 0 and self.pos_y % Enemy.TILE_SIZE == 0:
self.movement_path.pop(0)
self.path.pop(0)
if len(self.path) > 1:
grid = self.create_grid(map, bombs, explosions, enemy)
next = self.path[1]
if grid[next[0]][next[1]] > 1:
self.movement_path.clear()
self.path.clear()
if self.frame == 2:
self.frame = 0
else:
self.frame += 1
def make_move(self, map, bombs, explosions, enemy):
if not self.life:
return
if len(self.movement_path) == 0:
if self.plant:
bombs.append(self.plant_bomb(map))
self.plant = False
map[int(self.pos_x / Enemy.TILE_SIZE)][int(self.pos_y / Enemy.TILE_SIZE)] = 3
if self.algorithm is Algorithm.DFS:
self.dfs(self.create_grid(map, bombs, explosions, enemy))
else:
self.dijkstra(self.create_grid_dijkstra(map, bombs, explosions, enemy))
else:
self.direction = self.movement_path[0]
self.move(map, bombs, explosions, enemy)
def plant_bomb(self, map):
b = Bomb(self.range, round(self.pos_x / Enemy.TILE_SIZE), round(self.pos_y / Enemy.TILE_SIZE), map, self)
self.bomb_limit -= 1
return b
def check_death(self, exp):
for e in exp:
for s in e.sectors:
if int(self.pos_x / Enemy.TILE_SIZE) == s[0] and int(self.pos_y / Enemy.TILE_SIZE) == s[1]:
self.life = False
return
def dfs(self, grid):
new_path = [[int(self.pos_x / Enemy.TILE_SIZE), int(self.pos_y / Enemy.TILE_SIZE)]]
depth = 0
if self.bomb_limit == 0:
self.dfs_rec(grid, 0, new_path, depth)
else:
self.dfs_rec(grid, 2, new_path, depth)
self.path = new_path
def dfs_rec(self, grid, end, path, depth):
last = path[-1]
if depth > 200:
return
if grid[last[0]][last[1]] == 0 and end == 0:
return
elif end == 2:
if grid[last[0] + 1][last[1]] == end or grid[last[0] - 1][last[1]] == end \
or grid[last[0]][last[1] + 1] == end \
or grid[last[0]][last[1] - 1] == end:
if len(path) == 1 and end == 2:
self.plant = True
return
grid[last[0]][last[1]] = 9
random.shuffle(self.dire)
# safe
if grid[last[0] + self.dire[0][0]][last[1] + self.dire[0][1]] == 0:
path.append([last[0] + self.dire[0][0], last[1] + self.dire[0][1]])
self.movement_path.append(self.dire[0][2])
elif grid[last[0] + self.dire[1][0]][last[1] + self.dire[1][1]] == 0:
path.append([last[0] + self.dire[1][0], last[1] + self.dire[1][1]])
self.movement_path.append(self.dire[1][2])
elif grid[last[0] + self.dire[2][0]][last[1] + self.dire[2][1]] == 0:
path.append([last[0] + self.dire[2][0], last[1] + self.dire[2][1]])
self.movement_path.append(self.dire[2][2])
elif grid[last[0] + self.dire[3][0]][last[1] + self.dire[3][1]] == 0:
path.append([last[0] + self.dire[3][0], last[1] + self.dire[3][1]])
self.movement_path.append(self.dire[3][2])
# unsafe
elif grid[last[0] + self.dire[0][0]][last[1] + self.dire[0][1]] == 1:
path.append([last[0] + self.dire[0][0], last[1] + self.dire[0][1]])
self.movement_path.append(self.dire[0][2])
elif grid[last[0] + self.dire[1][0]][last[1] + self.dire[1][1]] == 1:
path.append([last[0] + self.dire[1][0], last[1] + self.dire[1][1]])
self.movement_path.append(self.dire[1][2])
elif grid[last[0] + self.dire[2][0]][last[1] + self.dire[2][1]] == 1:
path.append([last[0] + self.dire[2][0], last[1] + self.dire[2][1]])
self.movement_path.append(self.dire[2][2])
elif grid[last[0] + self.dire[3][0]][last[1] + self.dire[3][1]] == 1:
path.append([last[0] + self.dire[3][0], last[1] + self.dire[3][1]])
self.movement_path.append(self.dire[3][2])
else:
if len(self.movement_path) > 0:
path.pop(0)
self.movement_path.pop(0)
depth += 1
self.dfs_rec(grid, end, path, depth)
def dijkstra(self, grid):
end = 1
if self.bomb_limit == 0:
end = 0
visited = []
open_list = []
current = grid[int(self.pos_x / Enemy.TILE_SIZE)][int(self.pos_y / Enemy.TILE_SIZE)]
current.weight = current.base_weight
new_path = []
while True:
visited.append(current)
random.shuffle(self.dire)
if (current.value == end and end == 0) or\
(end == 1 and (grid[current.x+1][current.y].value == 1 or grid[current.x-1][current.y].value == 1 or
grid[current.x][current.y+1].value == 1 or grid[current.x][current.y-1].value == 1)):
new_path.append([current.x, current.y])
while True:
if current.parent is None:
break
current = current.parent
new_path.append([current.x, current.y])
new_path.reverse()
for xd in range(len(new_path)):
if new_path[xd] is not new_path[-1]:
if new_path[xd][0] - new_path[xd+1][0] == -1:
self.movement_path.append(1)
elif new_path[xd][0] - new_path[xd + 1][0] == 1:
self.movement_path.append(3)
elif new_path[xd][1] - new_path[xd + 1][1] == -1:
self.movement_path.append(0)
elif new_path[xd][1] - new_path[xd + 1][1] == 1:
self.movement_path.append(2)
if len(new_path) == 1 and end == 1:
self.plant = True
self.path = new_path
return
for i in range(len(self.dire)):
if current.x + self.dire[i][0] < len(grid) and current.y + self.dire[i][1] < len(grid):
if grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].reach \
and grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]] not in visited:
if grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]] in open_list:
if grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].weight >\
grid[current.x][current.y].weight \
+ grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].base_weight:
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].parent = current
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].weight = current.weight + grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].base_weight
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].direction = self.dire[i][2]
else:
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].parent = current
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].weight =\
current.weight + grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].base_weight
grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]].direction = self.dire[i][2]
open_list.append(grid[current.x + self.dire[i][0]][current.y + self.dire[i][1]])
if len(open_list) == 0:
self.path = [[int(self.pos_x / Enemy.TILE_SIZE), int(self.pos_y / Enemy.TILE_SIZE)]]
return
next_node = open_list[0]
for n in open_list:
if n.weight < next_node.weight:
next_node = n
open_list.remove(next_node)
current = next_node
def create_grid(self, map, bombs, explosions, enemys):
grid = [[0] * len(map) for r in range(len(map))]
# 0 - safe
# 1 - unsafe
# 2 - destryable
# 3 - unreachable
for b in bombs:
b.get_range(map)
for x in b.sectors:
grid[x[0]][x[1]] = 1
grid[b.pos_x][b.pos_y] = 3
for e in explosions:
for s in e.sectors:
grid[s[0]][s[1]] = 3
for i in range(len(map)):
for j in range(len(map[i])):
if map[i][j] == 1:
grid[i][j] = 3
elif map[i][j] == 2:
grid[i][j] = 2
for x in enemys:
if x == self:
continue
elif not x.life:
continue
else:
grid[int(x.pos_x / Enemy.TILE_SIZE)][int(x.pos_y / Enemy.TILE_SIZE)] = 2
return grid
def create_grid_dijkstra(self, map, bombs, explosions, enemys):
grid = [[None] * len(map) for r in range(len(map))]
# 0 - safe
# 1 - destroyable
# 2 - unreachable
# 3 - unsafe
for i in range(len(map)):
for j in range(len(map)):
if map[i][j] == 0:
grid[i][j] = Node(i, j, True, 1, 0)
elif map[i][j] == 2:
grid[i][j] = Node(i, j, False, 999, 1)
elif map[i][j] == 1:
grid[i][j] = Node(i, j, False, 999, 2)
elif map[i][j] == 3:
grid[i][j] = Node(i, j, False, 999, 2)
for b in bombs:
b.get_range(map)
for x in b.sectors:
grid[x[0]][x[1]].weight = 5
grid[x[0]][x[1]].value = 3
grid[b.pos_x][b.pos_y].reach = False
for e in explosions:
for s in e.sectors:
grid[s[0]][s[1]].reach = False
for x in enemys:
if x == self:
continue
elif not x.life:
continue
else:
grid[int(x.pos_x / Enemy.TILE_SIZE)][int(x.pos_y / Enemy.TILE_SIZE)].reach = False
grid[int(x.pos_x / Enemy.TILE_SIZE)][int(x.pos_y / Enemy.TILE_SIZE)].value = 1
return grid
def load_animations(self, en, scale):
front = []
back = []
left = []
right = []
resize_width = scale
resize_height = scale
image_path = 'images/enemy/e'
if en == '':
image_path = 'images/hero/p'
f1 = pygame.image.load(image_path + en + 'f0.png')
f2 = pygame.image.load(image_path + en + 'f1.png')
f3 = pygame.image.load(image_path + en + 'f2.png')
f1 = pygame.transform.scale(f1, (resize_width, resize_height))
f2 = pygame.transform.scale(f2, (resize_width, resize_height))
f3 = pygame.transform.scale(f3, (resize_width, resize_height))
front.append(f1)
front.append(f2)
front.append(f3)
r1 = pygame.image.load(image_path + en + 'r0.png')
r2 = pygame.image.load(image_path + en + 'r1.png')
r3 = pygame.image.load(image_path + en + 'r2.png')
r1 = pygame.transform.scale(r1, (resize_width, resize_height))
r2 = pygame.transform.scale(r2, (resize_width, resize_height))
r3 = pygame.transform.scale(r3, (resize_width, resize_height))
right.append(r1)
right.append(r2)
right.append(r3)
b1 = pygame.image.load(image_path + en + 'b0.png')
b2 = pygame.image.load(image_path + en + 'b1.png')
b3 = pygame.image.load(image_path + en + 'b2.png')
b1 = pygame.transform.scale(b1, (resize_width, resize_height))
b2 = pygame.transform.scale(b2, (resize_width, resize_height))
b3 = pygame.transform.scale(b3, (resize_width, resize_height))
back.append(b1)
back.append(b2)
back.append(b3)
l1 = pygame.image.load(image_path + en + 'l0.png')
l2 = pygame.image.load(image_path + en + 'l1.png')
l3 = pygame.image.load(image_path + en + 'l2.png')
l1 = pygame.transform.scale(l1, (resize_width, resize_height))
l2 = pygame.transform.scale(l2, (resize_width, resize_height))
l3 = pygame.transform.scale(l3, (resize_width, resize_height))
left.append(l1)
left.append(l2)
left.append(l3)
self.animation.append(front)
self.animation.append(right)
self.animation.append(back)
self.animation.append(left)