forked from pedralmeida22/IA-Bomberman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
348 lines (299 loc) · 11.2 KB
/
game.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
import asyncio
import json
import logging
import math
import os
import requests
from characters import Balloom, Bomberman, Character, Doll, Minvo, Oneal, Kondoria, Ovapi, Pass
from consts import Powerups
from mapa import Map, Tiles
logger = logging.getLogger("Game")
logger.setLevel(logging.DEBUG)
LIVES = 3
INITIAL_SCORE = 0
TIMEOUT = 3000
GAME_SPEED = 20
MIN_BOMB_RADIUS = 3
MAP_SIZE = (51, 31)
LEVEL_ENEMIES = {
1: [Balloom] * 6,
2: [Balloom] * 3 + [Oneal] * 3,
3: [Balloom] * 2 + [Oneal] * 2 + [Doll] * 2,
4: [Balloom] + [Oneal] + [Doll] * 2 + [Minvo] * 2,
5: [Oneal] * 4 + [Doll] * 3,
6: [Oneal] * 2 + [Doll] * 3 + [Minvo] * 2,
7: [Oneal] * 2 + [Doll] * 3 + [Kondoria] * 2,
8: [Oneal] * 1 + [Doll] * 2 + [Minvo] * 4,
9: [Oneal] * 1 + [Doll] * 1 + [Minvo] * 4 + [Kondoria] * 1,
10: [Oneal] * 1 + [Doll] * 1 + [Minvo] * 1 + [Kondoria] * 3 + [Ovapi] * 1,
11: [Oneal] * 1 + [Doll] * 2 + [Minvo] * 3 + [Kondoria] * 1 + [Ovapi] * 1,
12: [Oneal] * 1 + [Doll] * 1 + [Minvo] * 1 + [Kondoria] * 4 + [Ovapi] * 1,
13: [Doll] * 3 + [Minvo] * 3 + [Kondoria] * 3,
14: [Ovapi] * 7 + [Pass] * 1,
15: [Doll] * 1 + [Minvo] * 3 + [Kondoria] * 3 + [Pass] * 1,
}
LEVEL_POWERUPS = {
1: Powerups.Flames,
2: Powerups.Bombs,
3: Powerups.Detonator,
4: Powerups.Speed,
5: Powerups.Bombs,
6: Powerups.Bombs,
7: Powerups.Flames,
8: Powerups.Detonator,
9: Powerups.Bombpass,
10: Powerups.Wallpass,
11: Powerups.Bombs,
12: Powerups.Bombs,
13: Powerups.Detonator,
14: Powerups.Bombpass,
15: Powerups.Flames,
}
class Bomb:
def __init__(self, pos, mapa, radius, detonator=False):
self._pos = pos
self._timeout = radius + 1 # TODO fine tune
self._radius = radius
self._detonator = detonator
self._map = mapa
def detonate(self):
if self._detonator:
self._timeout = 0
@property
def pos(self):
return self._pos
@property
def timeout(self):
return self._timeout
@property
def radius(self):
return self._radius
def update(self):
if not self._detonator:
self._timeout -= 1 / 2
def exploded(self):
return not self._timeout > 0
def in_range(self, character):
bx, by = self._pos
if isinstance(character, Character):
cx, cy = character.pos
else:
cx, cy = character
if by == cy:
for r in range(self._radius + 1):
if self._map.is_stone((bx + r, by)):
break # protected by stone to the right
if (cx, cy) == (bx + r, by):
return True
for r in range(self._radius + 1):
if self._map.is_stone((bx - r, by)):
break # protected by stone to the left
if (cx, cy) == (bx - r, by):
return True
if bx == cx:
for r in range(self._radius + 1):
if self._map.is_stone((bx, by + r)):
break # protected by stone in the bottom
if (cx, cy) == (bx, by + r):
return True
for r in range(self._radius + 1):
if self._map.is_stone((bx, by - r)):
break # protected by stone in the top
if (cx, cy) == (bx, by - r):
return True
return False
def __repr__(self):
return self._pos
class Game:
def __init__(self, level=1, lives=LIVES, timeout=TIMEOUT, size=MAP_SIZE):
logger.info(f"Game(level={level}, lives={lives})")
self.initial_level = level
self._running = False
self._timeout = timeout
self._score = 0
self._step = 0
self._total_steps = 0
self._state = {}
self._initial_lives = lives
self.map = Map(size=size, empty=True)
self._enemies = []
def info(self):
return {
"size": self.map.size,
"map": self.map.map,
"fps": GAME_SPEED,
"timeout": TIMEOUT,
"lives": LIVES,
"score": self.score,
}
@property
def running(self):
return self._running
@property
def score(self):
return self._score
@property
def total_steps(self):
return self._total_steps
def start(self, player_name):
logger.debug("Reset world")
self._player_name = player_name
self._running = True
self._total_steps = 0
self._score = INITIAL_SCORE
self._bomberman = Bomberman(self.map.bomberman_spawn, self._initial_lives)
for powerup in range(1, self.initial_level):
self._bomberman.powerup(LEVEL_POWERUPS[powerup])
logger.debug("Bomberman Powerups: %s", self._bomberman.powers)
self.next_level(self.initial_level)
def stop(self):
logger.info("GAME OVER")
self._total_steps += self._step
self._running = False
def next_level(self, level):
if level > len(LEVEL_ENEMIES):
logger.info("You WIN!")
self.stop()
return
logger.info("NEXT LEVEL")
self.map = Map(level=level, size=self.map.size, enemies=len(LEVEL_ENEMIES[level]))
self._bomberman.respawn()
self._total_steps += self._step
self._step = 0
self._bombs = []
self._powerups = []
self._bonus = []
self._exit = []
self._lastkeypress = ""
self._enemies = [
t(p) for t, p in zip(LEVEL_ENEMIES[level], self.map.enemies_spawn)
]
logger.debug("Enemies: %s", [(e._name, e.pos) for e in self._enemies])
logger.debug("Walls: %s", self.map.walls)
def quit(self):
logger.debug("Quit")
self._running = False
def keypress(self, key):
self._lastkeypress = key
def update_bomberman(self):
try:
if self._lastkeypress.isupper():
# Parse action
if self._lastkeypress == "A" and len(self._bombs) > 0:
self._bombs[0].detonate() # always detonate the oldest bomb
elif (
self._lastkeypress == "B"
and len(self._bombs)
< self._bomberman.powers.count(Powerups.Bombs) + 1
and not self.map.is_blocked(self._bomberman.pos)
):
self._bombs.append(
Bomb(
self._bomberman.pos,
self.map,
MIN_BOMB_RADIUS + self._bomberman.flames(),
detonator=Powerups.Detonator in self._bomberman.powers,
)
) # must be dependent of powerup
else:
# Update position
new_pos = self.map.calc_pos(
self._bomberman.pos, self._lastkeypress, self._bomberman.wallpass
) # don't bump into stones/walls
if self._bomberman.bombpass or new_pos not in [b.pos for b in self._bombs]: # don't pass over bombs
self._bomberman.pos = new_pos
for pos, _type in self._powerups: # consume powerups
if new_pos == pos:
self._bomberman.powerup(_type)
self._powerups.remove((pos, _type))
except AssertionError:
logger.error(
"Invalid key <%s> pressed. Valid keys: w,a,s,d A B", self._lastkeypress
)
finally:
self._lastkeypress = "" # remove inertia
if len(self._enemies) == 0 and self._bomberman.pos == self._exit:
logger.info(f"Level {self.map.level} completed")
#self._score += self._timeout - self._step
self.next_level(self.map.level + 1)
def kill_bomberman(self):
logger.info(f"bomberman has died on step: {self._step}")
self._bomberman.kill()
logger.debug(f"bomberman has now {self._bomberman.lives} lives")
if self._bomberman.lives > 0:
logger.debug("RESPAWN")
self._bomberman.respawn()
self._bombs = []
else:
self.stop()
def collision(self):
for e in self._enemies:
if e.pos == self._bomberman.pos:
self.kill_bomberman()
e.respawn()
def explode_bomb(self):
for bomb in self._bombs[:]:
bomb.update()
if bomb.exploded():
logger.debug("BOOM")
if bomb.in_range(self._bomberman) and not self._bomberman.flamepass:
self.kill_bomberman()
for wall in self.map.walls[:]:
if bomb.in_range(wall):
logger.debug(f"Destroying wall @{wall}")
self.map.remove_wall(wall)
if self.map.exit_door == wall:
self._exit = wall
if self.map.powerup == wall:
self._powerups.append(
(wall, LEVEL_POWERUPS[self.map.level])
)
for enemy in self._enemies[:]:
if bomb.in_range(enemy):
logger.debug(f"killed enemy @{enemy}")
self._score += enemy.points()
self._enemies.remove(enemy)
if bomb in self._bombs:
self._bombs.remove(bomb)
async def next_frame(self):
await asyncio.sleep(1.0 / GAME_SPEED)
if not self._running:
logger.info("Waiting for player 1")
return
self._step += 1
if self._step == self._timeout:
self.stop()
if self._step % 100 == 0:
logger.debug(
f"[{self._step}] SCORE {self._score} - LIVES {self._bomberman.lives} - Total Steps {self.total_steps}"
)
self.explode_bomb()
self.update_bomberman()
self.collision()
if (
self._step % (self._bomberman.powers.count(Powerups.Speed) + 1) == 0
): # increase speed of bomberman by moving enemies less often
for enemy in self._enemies:
enemy.move(self.map, self._bomberman, self._bombs, self._enemies)
self.collision()
#sanity check
assert all([ep not in self.map.walls for ep in [e.pos for e in self._enemies if not e._wallpass]])
self._state = {
"level": self.map.level,
"step": self._step,
"timeout": self._timeout,
"player": self._player_name,
"score": self._score,
"lives": self._bomberman.lives,
"bomberman": self._bomberman.pos,
"bombs": [(b.pos, b.timeout, b.radius) for b in self._bombs],
"enemies": [{"name": str(e), "id": str(e.id), "pos": e.pos} for e in self._enemies],
"walls": self.map.walls,
"powerups": [(p, Powerups(n).name) for p, n in self._powerups],
"bonus": self._bonus,
"exit": self._exit,
}
@property
def state(self):
# logger.debug(self._state)
return json.dumps(self._state)