-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
464 lines (394 loc) · 18.6 KB
/
map.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from PyQt5.QtWidgets import (QMainWindow, QLabel, QDesktopWidget, QFrame, QPushButton)
from PyQt5.QtGui import (QPainter, QPixmap, QIcon, QMovie, QTextDocument)
from PyQt5.QtCore import Qt, QThreadPool, pyqtSlot, QCoreApplication
import pos
import player
import enemy
import bubble
from time import sleep
LIVES_ROW_POS = 15
P2_LIFE_1_COLUMN = 13
class Map(QFrame):
def __init__(self):
super().__init__()
# i = vertical, kolona
# j = horizonal, vrsta
self.block_w = 75
self.block_h = 60
# List of free position objects
self.freeCoordinates = []
# List of all position
self.allPositions = []
# Number of lives for players. For drawing lives on map
self.p1_lives = 3
self.p2_lives = 3
self.board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
self._initPositions()
def _initPositions(self):
index = 0
for row in range(16):
for column in range(16):
p = pos.Position()
p.setPosition(pos.Coordinate(row, column), None, False)
if self.board[row][column] == 1:
self.freeCoordinates.append(p.coordinate)
else:
p.setWall(True)
index = 16 * row + column
self.allPositions.insert(index, p)
def _updateAllPositions(self, entity, oldCoordinate, newCoordinate):
for p in self.allPositions:
if p.coordinate == newCoordinate:
#print("Entity: ", entity, " have landed on coordinate: ", newCoordinate)
p.entity = entity
if p.coordinate == oldCoordinate:
#print("Entity: ", entity, " have been removed from coordinate: ", oldCoordinate)
p.entity = None
def _updateFreeCoordinates(self, oldCoordinate, newCoordinate):
# We have moved to some new position. Need to remove that coordinates from list
if newCoordinate in self.freeCoordinates:
#print("Removing coordinate from list of free: ", newCoordinate)
self.freeCoordinates.remove(newCoordinate)
# We have moved from some coordinate. If it is not init coordinates(-1,-1), we need to add
# them to list of free coordinates
if oldCoordinate == pos.Coordinate(-1,-1):
#print("Initial moving of players...")
return
if oldCoordinate not in self.freeCoordinates:
#print("Adding old coordinate: ", oldCoordinate, " to the list of free coordinates!")
self.freeCoordinates.append(oldCoordinate)
# This method will update list of free positions, but also will update
# list of all positions, because we draw map based on that list
def updateMap(self, oldCoordinate, newCoordinate, entity):
self._updateAllPositions(entity, oldCoordinate, newCoordinate)
self._updateFreeCoordinates(oldCoordinate, newCoordinate)
def isInMap(self, coordinate):
if coordinate.row > 0 and coordinate.row < 15 and coordinate.column > 0 and coordinate.column < 15:
return True
else:
print("Coordinate not in map: ", coordinate)
return False
# Method for checking if cordinate is available when jump or move is performed
# Needs refactoring
def isCoordinateAvailable(self, checkCoordinate, entity):
if entity.action == "anti_gravity":
if self.isBubble(entity):
return self._canBubbleGoUp(bubble, checkCoordinate)
else:
if self.isPlayer(entity):
return self._canPlayerMoveOrJump(entity, checkCoordinate)
elif self.isEnemy(entity):
return self._canEnemyMoveOrJump(entity, checkCoordinate)
elif self.isBubble(entity):
return self._canBubbleMove(entity, checkCoordinate)
# Scenarios:
# 1. Bubble - Empty bubble, enemy inside bubble or full bubble - can move/jump, full is taking life
# 2. Enemy - Can move/jump - if not imuned, we are loosing life
# 3. Player - Can NOT move/jump
# 4. Empty block - Can move/jump if it is in list of free coordinates
def _canPlayerMoveOrJump(self, player, coordinate):
if not self.isInMap(coordinate):
return False
atCoordinate = self.getEntityAtCoordinate(coordinate)
if self.isBubble(atCoordinate):
# If it is a full bubble, loose a life. We will never be able to jump or move to our own full bubble
if atCoordinate.mode == 1 and not player.imune:
self.playerLostLife(player)
elif atCoordinate.mode == 1 and player.imune:
self.destroyEntity(atCoordinate)
# If we encounter empty bubble
elif atCoordinate.mode == 2 or atCoordinate.mode == 3:
if player.action == "jump":
# We can jump and pop baloon
self.destroyEntity(atCoordinate)
#TODO: Increase score if we pop empty baloon or if it was baloon with enemy, generate fruit
return True
else:
# When we move sideways and encounter empty ballon, just return false
return False
return True
elif self.isEnemy(atCoordinate):
if not player.imune:
self.playerLostLife(player)
return True
elif self.isPlayer(atCoordinate):
return False
elif self._isWall(coordinate):
return True
else:
return coordinate in self.freeCoordinates
# Scenarios:
# 1. Bubble - Empty bubble, enemy inside bubble or full bubble - can move/jump, full is taking life
# 2. Player - Can move/jump - if not imuned, we are taking his life
# 3. Enemy - Can NOT move/jump
# 4. Empty block - Can move/jump if it is in list of free coordinates
def _canEnemyMoveOrJump(self, enemy, coordinate):
atCoordinate = self.getEntityAtCoordinate(coordinate)
if self.isBubble(atCoordinate):
if atCoordinate.mode == 1:
enemy.destroyEnemy()
return True
else:
return True
elif self.isPlayer(atCoordinate):
if not atCoordinate.imune:
self.playerLostLife(atCoordinate)
return True
elif self.isEnemy(atCoordinate):
return False
else:
return coordinate in self.freeCoordinates
# Scenarios:
# 1. Enemy - Can move, in case we are full bubble, we are killing enemy
# 2. Player - Can move/jump - if not imuned, we are loosing life
# 3. Bubble - Can NOT move/jump
# 4. Empty block - Can move/jump if it is in list of free coordinates
def _canBubbleMove(self, bubble, coordinate):
print("Bubble: ", bubble, " want to move to coordinate: ", coordinate)
atCoordinate = self.getEntityAtCoordinate(coordinate)
if self.isEnemy(atCoordinate):
if bubble.mode == 1:
# destroy enemy
# self.destroyEntity(atCoordinate)
# self.removeCoordinate(coordinate)
bubble.mode = 2
return True
elif self.isPlayer(atCoordinate):
if bubble.mode == 1 and not atCoordinate.imune:
self.playerLostLife(atCoordinate)
bubble.mode = 2
elif bubble.mode == 1 and atCoordinate.imune:
self.destroyEntity(bubble)
return True
elif self.isBubble(atCoordinate):
print("Moving bubble: ", bubble, " encountered bubble: ", atCoordinate)
# If bubbly is carrying enemy, we DON'T want to destroy it. Insead destroy ourselvs if we are not carying enemy
if atCoordinate.mode == 3:
if bubble.mode is not 3:
self.destroyEntity(bubble)
return False
elif bubble.mode == 3:
if atCoordinate.mode is not 3:
self.destroyEntity(atCoordinate)
return False
else:
# Important thing is not to change order. Bubble that want to take position of
# another bubble needs to be destoryed after encountered bubble, to prevent encountered
# bubble to keep moving after destruction.
self.destroyEntity(atCoordinate)
self.destroyEntity(bubble)
return False
elif self._isWall(coordinate):
self.destroyEntity(bubble)
return False
else:
return coordinate in self.freeCoordinates
# So far keep it as sideways bubble movment. Not sure if it stay the same?
def _canBubbleGoUp(self, bubble, coordinate):
atCoordinate = self.getEntityAtCoordinate(coordinate)
if self.isEnemy(atCoordinate):
if bubble.mode == 1:
atCoordinate.destroyEnemy()
bubble.mode = 2
return True
elif self.isPlayer(atCoordinate):
if bubble.mode == 1:
atCoordinate.takeAwayLife()
return True
elif self.isBubble(atCoordinate):
bubble.destroyBubble()
atCoordinate.destroyBubble()
return True
else:
return coordinate in self.freeCoordinates
# Method for checking if we should apply coordinate on passed entity, by checking what is
# bellow us
def isGravityNeeded(self, entity):
coordinateBellow = pos.Coordinate(entity.coordinate.row + 1, entity.coordinate.column)
atCoordinate = self.getEntityAtCoordinate(coordinateBellow)
if self.isPlayer(entity):
# Player can land on:
# 1. Wall
# 2. Another player
# 3. On bubble -> TODO
if self._isWall(coordinateBellow) or self.isPlayer(atCoordinate):
return False
elif self.isBubble(atCoordinate):
if atCoordinate.mode == 1:
self.playerLostLife(entity)
# Killed, return false, not really need to drop down
return False
else:
# For now when we fall, and encounter bubble which is not full, call foundBubble
# For NOW we DON'T WANT on bubble which is going up.
# TODO: If it is empty bubble - land on bubble, so return false here
# TODO: If it is bubble which have enemy iside it - take points, destroy bubble and keep faling (return True)
#entity.foundBubble(atCoordinate)
return True
elif self.isEnemy(atCoordinate):
self.playerLostLife(entity)
return False
else:
return True
elif self.isEnemy(entity):
# Enemy can land on
# 1. On wall
# 2. On another enemy
if self._isWall(coordinateBellow) or self.isEnemy(atCoordinate):
return False
elif self.isBubble(atCoordinate):
if atCoordinate.mode == 1:
# If it is full bubble, we are dead
self.destroyEntity(entity)
return False
else:
return True
elif self.isPlayer(atCoordinate):
self.playerLostLife(atCoordinate)
return False
else:
return True
def getEntityAtCoordinate(self, coordinate):
for p in self.allPositions:
if p.coordinate == coordinate:
return p.entity
# Destroy entitity from coordinate he was on
# Also add coordinate to list of freeCoordinates
def destroyEntity(self, entity):
for p in self.allPositions:
if p.coordinate == entity.coordinate:
if self.isPlayer(entity):
entity.coordinate = pos.Coordinate(-1, -1)
p.entity = None
if p.coordinate not in self.freeCoordinates:
self.freeCoordinates.append(p.coordinate)
elif self.isBubble(entity) or self.isEnemy(entity):
entity.coordinate = pos.Coordinate(-1, -1)
print("Entity: ", entity, " is no more alive!!!")
entity.alive = False
p.entity = None
if p.coordinate not in self.freeCoordinates:
self.freeCoordinates.append(p.coordinate)
def _isWall(self, coordinate):
for pos in self.allPositions:
if pos.coordinate == coordinate:
return pos.wall
def playerLostLife(self, player):
if player.id == 1:
self.p1_lives -= 1
else:
self.p2_lives -= 1
player.takeAwayLife()
# If it was last life, remove it from map
if not player.isAlive():
self.destroyEntity(player)
else:
# Remove it from coordinate
self.destroyEntity(player)
player.afterLifeLoss()
# Maybe move this 3 methods to some util?
def isPlayer(self, entity):
return isinstance(entity, player.Player)
def isEnemy(self, entity):
return isinstance(entity, enemy.Enemy)
def isBubble(self, entity):
return isinstance(entity, bubble.Bubble)
def paintEvent(self, event):
painter = QPainter(self)
for p in self.allPositions:
if p.entity is not None:
self._drawEntity(p, painter)
else:
if p.wall:
self._drawWall(p, painter)
else:
self._drawMapBlock(p, painter)
self._drawP1Lives(painter, self.p1_lives)
self._drawP2Lives(painter, self.p2_lives)
def _drawEntity(self, position, painter):
self._drawMapBlock(position, painter)
if position.wall:
self._drawWall(position, painter)
# Draw player
if self.isPlayer(position.entity):
if position.entity.id == 1:
self._drawPlayer1(position, painter)
else:
self._drawPlayer2(position, painter)
# Draw enemy
elif self.isEnemy(position.entity):
self._drawEnemy(position, painter)
# Draw bubble
elif self.isBubble(position.entity):
self._drawBubble(position, painter)
def _drawPlayer1(self, position, painter):
if position.entity.action == "move_right" or position.entity.action == "init":
if position.entity.imune:
position.entity.label = position.entity.label_right_imuned
else:
position.entity.label = position.entity.label_right
elif position.entity.action == "move_left":
if position.entity.imune:
position.entity.label = position.entity.label_left_imuned
else:
position.entity.label = position.entity.label_left
self._drawPixmap(painter, position.coordinate, position.entity.label)
def _drawPlayer2(self, position, painter):
if position.entity.action == "move_left" or position.entity.action == "init":
if position.entity.imune:
position.entity.label = position.entity.label_left_imuned
else:
position.entity.label = position.entity.label_left
elif position.entity.action == "move_right":
if position.entity.imune:
position.entity.label = position.entity.label_right_imuned
else:
position.entity.label = position.entity.label_right
self._drawPixmap(painter, position.coordinate, position.entity.label)
def _drawEnemy(self, position, painter):
if position.entity.action == "move_left" or position.entity.action == "init" or position.entity.action == "gravity":
position.entity.label = position.entity.label_left
elif position.entity.action == "move_right":
position.entity.label = position.entity.label_right
self._drawPixmap(painter, position.coordinate, position.entity.label)
def _drawBubble(self, position, painter):
if position.entity.mode == 1:
self._drawPixmap(painter, position.coordinate, position.entity.label_mode_1)
elif position.entity.mode == 2:
self._drawPixmap(painter, position.coordinate, position.entity.label_mode_2)
else:
self._drawPixmap(painter, position.coordinate, position.entity.label_mode_3)
def _drawWall(self, position, painter):
self._drawPixmap(painter, position.coordinate, 'map/map_block.png')
def _drawMapBlock(self, position, painter):
painter.fillRect(position.coordinate.column * self.block_w,
position.coordinate.row*self.block_h,
self.block_w,
self.block_h,
Qt.black)
def _drawP1Lives(self, painter, numOfLives):
for i in range(numOfLives):
self._drawPixmap(painter, pos.Coordinate(LIVES_ROW_POS, i), 'characters/heart.png')
def _drawP2Lives(self, painter, numOfLives):
for i in range(numOfLives):
self._drawPixmap(painter, pos.Coordinate(LIVES_ROW_POS, P2_LIFE_1_COLUMN + i), 'characters/heart.png')
def _drawPixmap(self, painter, coordinate, image):
painter.drawPixmap(coordinate.column * self.block_w, coordinate.row * self.block_h, self.block_w, self.block_h, QPixmap(image))
self.update()