This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
655 lines (563 loc) · 28.8 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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
from PyQt5.QtWidgets import QWidget, QLabel
from PyQt5.QtGui import QPixmap, QFont
from PyQt5.QtCore import Qt, pyqtSignal, QTimer
import config
from player_actions import ShootLaser
from player import Player
from enemy_actions import MoveEnemy, EnemyShoot, EnemyAttack
from multiprocessing import Process, Queue, current_process
from random import randint
from deus_ex_machina import DeusExMachina
class Game(QWidget):
gameOverSignal = pyqtSignal()
def __init__(self, players):
super().__init__()
print('Players to play: ', players)
# PowerUp process
self.powerUpQueue = Queue()
print('Regular process id: ', current_process().pid)
powerUpProcess = Process(target=powerup_process, args=[self.powerUpQueue])
powerUpProcess.start()
self.powerUpLabels = []
# ShootLaser thread
self.shootLaser = ShootLaser()
self.shootLaser.calc_done.connect(self.move_laser_up)
self.shootLaser.collision_detected.connect(self.player_laser_enemy_collide)
self.shootLaser.moving_collision_detected.connect(self.player_laser_moving_enemy_collide)
self.shootLaser.start()
# MoveEnemy thread
self.moveEnemy = MoveEnemy()
self.moveEnemy.calc_done.connect(self.move_enemy)
self.moveEnemy.start()
# EnemyShoot thread
self.enemyShoot = EnemyShoot()
self.enemyShoot.can_shoot.connect(self.enemy_shoot_laser)
self.enemyShoot.move_down.connect(self.move_enemy_laser)
self.enemyShoot.collision_detected.connect(self.enemy_hit_player)
self.enemyShoot.next_level.connect(self.next_level)
self.enemyShoot.start()
# EnemyAttack thread
self.enemyAttack = EnemyAttack()
self.enemyAttack.can_attack.connect(self.enemy_attack_player)
self.enemyAttack.move_down.connect(self.move_enemy_down)
self.enemyAttack.player_collision.connect(self.enemy_attack_player_hit)
self.enemyAttack.start()
# PowerUp thread
self.deusExMachina = DeusExMachina()
self.deusExMachina.collision_detected.connect(self.powerup_collision)
self.deusExMachina.powerup_timeout.connect(self.powerup_timeout)
self.deusExMachina.start()
# Power up timer
self.powerUpTimer = QTimer()
self.powerUpTimer.setInterval(config.POWERUP_TIMEOUT)
self.powerUpTimer.timeout.connect(self.show_powerup)
self.powerUpTimer.start()
# Gameplay options
self.activePlayers = players
self.startPlayers = players
self.playerOneSpeed = config.PLAYER_SPEED
self.playerTwoSpeed = config.PLAYER_SPEED
self.playerOneCanShoot = True
self.playerTwoCanShoot = True
# Add background pixmap
self.backgroundPixmap = QPixmap('images/background.png')
# Add player one
self.playerPixmap = QPixmap('images/ship.png')
# add second player
if self.startPlayers == 2:
self.playerTwoPixmap = QPixmap('images/ship_two.png')
# Set enemy pixmaps
self.enemyPixmaps = []
enemyPixmap = QPixmap('images/enemy_1.png')
enemyPixmap = enemyPixmap.scaledToWidth(config.IMAGE_WIDTH - 20)
enemyPixmap = enemyPixmap.scaledToHeight(config.IMAGE_HEIGHT - 20)
self.enemyPixmaps.append(enemyPixmap)
enemyPixmap = QPixmap('images/enemy_2.png')
enemyPixmap = enemyPixmap.scaledToWidth(config.IMAGE_WIDTH - 20)
enemyPixmap = enemyPixmap.scaledToHeight(config.IMAGE_HEIGHT - 20)
self.enemyPixmaps.append(enemyPixmap)
enemyPixmap = QPixmap('images/enemy_3.png')
enemyPixmap = enemyPixmap.scaledToWidth(config.IMAGE_WIDTH - 20)
enemyPixmap = enemyPixmap.scaledToHeight(config.IMAGE_HEIGHT - 20)
self.enemyPixmaps.append(enemyPixmap)
enemyPixmap = QPixmap('images/enemy_4.png')
enemyPixmap = enemyPixmap.scaledToWidth(config.IMAGE_WIDTH - 20)
enemyPixmap = enemyPixmap.scaledToHeight(config.IMAGE_HEIGHT - 20)
self.enemyPixmaps.append(enemyPixmap)
self.__init_ui__()
def __init_ui__(self):
# Set background
numOfLabelsX = config.BOARD_WIDTH // config.IMAGE_WIDTH
numOfLabelsY = config.BOARD_HEIGHT // config.IMAGE_HEIGHT
for x in range(numOfLabelsX):
for y in range(numOfLabelsY):
backgroundLabel = QLabel(self)
backgroundLabel.setPixmap(self.backgroundPixmap)
backgroundLabelX = config.IMAGE_WIDTH * x
backgroundLabelY = config.IMAGE_HEIGHT * y
backgroundLabel.setGeometry(backgroundLabelX, backgroundLabelY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
# Set lives label
self.playerLivesLabel = QLabel(self)
self.playerLivesLabelText = "<font color='white'>Lives: 3</font>"
self.playerLivesLabel.setText(self.playerLivesLabelText)
self.playerLivesLabel.setFont(QFont('Times', 16, QFont.Bold))
# Set second player lives label
if self.startPlayers == 2:
self.playerTwoLivesLabel = QLabel(self)
self.playerTwoLivesLabelText = "<font color='white'>Lives: 3</font>"
self.playerTwoLivesLabel.setText(self.playerTwoLivesLabelText)
self.playerTwoLivesLabel.setFont(QFont('Times', 16, QFont.Bold))
self.playerTwoLivesLabel.setGeometry(config.BOARD_WIDTH - 100, 0, 100, 30)
#Set level label
self.gameLevel = QLabel(self)
self.gameLevel.setFont(QFont("Times", 16, QFont.Bold))
levelX = config.BOARD_WIDTH // 2 - 50 # centar
levelY = 0
self.gameLevel.setGeometry(levelX, levelY, 100, 30)
self.update_level(1)
# Set player start positions
if self.startPlayers == 1:
self.playerLabel = QLabel(self)
self.playerLabel.setPixmap(self.playerPixmap)
playerLabelX = config.BOARD_WIDTH // 2 - config.IMAGE_WIDTH
playerLabelY = config.BOARD_HEIGHT - config.IMAGE_HEIGHT
self.playerLabel.setGeometry(playerLabelX, playerLabelY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
self.player = Player(self.playerLabel)
elif self.startPlayers == 2:
# set player 1 start position
self.playerLabel = QLabel(self)
self.playerLabel.setPixmap(self.playerPixmap)
playerLabelX = 0
playerLabelY = config.BOARD_HEIGHT - config.IMAGE_HEIGHT
# set player 2 start position
self.playerTwoLabel = QLabel(self)
self.playerTwoLabel.setPixmap(self.playerTwoPixmap)
playerTwoLabelX = config.BOARD_WIDTH - config.IMAGE_WIDTH
playerTwoLabelY = config.BOARD_HEIGHT - config.IMAGE_HEIGHT
self.playerLabel.setGeometry(playerLabelX, playerLabelY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
self.playerTwoLabel.setGeometry(playerTwoLabelX, playerTwoLabelY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
# Players
self.player = Player(self.playerLabel)
self.playerTwo = Player(self.playerTwoLabel)
# Set enemy start positions
self.enemyLabels = []
for i in range(3):
for j in range(10):
enemyLabel = QLabel(self)
randIndex = randint(0, len(self.enemyPixmaps)-1)
enemyLabel.setPixmap(self.enemyPixmaps[randIndex])
positionX = config.IMAGE_WIDTH * (j+3)
positionY = config.IMAGE_WIDTH * (i+1)
enemyLabel.setGeometry(positionX, positionY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
enemyLabel.show()
self.enemyLabels.append(enemyLabel)
self.activate_enemy_threads()
def show_powerup(self):
if self.powerUpQueue.qsize() < 2:
powerUpProcess = Process(target=powerup_process, args=[self.powerUpQueue])
powerUpProcess.start()
powerUpX = self.powerUpQueue.get()
powerUpPixmap = QPixmap('images/pewdiepie.png')
powerUpLabel = QLabel(self)
powerUpLabel.setPixmap(powerUpPixmap)
powerUpLabel.setGeometry(powerUpX, config.BOARD_HEIGHT-config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
powerUpLabel.show()
self.deusExMachina.add_powerup(powerUpLabel)
self.powerUpLabels.append(powerUpLabel)
def powerup_timeout(self, powerUpLabel: QLabel):
if powerUpLabel in self.powerUpLabels:
self.powerUpLabels.remove(powerUpLabel)
powerUpLabel.hide()
def powerup_collision(self, powerUpLabel: QLabel, playerLabel: QLabel):
if powerUpLabel in self.powerUpLabels:
self.powerUpLabels.remove(powerUpLabel)
powerUpLabel.hide()
# choose random powerup
randIndex = randint(0, len(config.POWERUPS)-1)
powerUpAction = config.POWERUPS[0]
print('Action: ', powerUpAction)
if self.startPlayers == 2:
if self.player.playerLabel == playerLabel:
# Player 1
print('Player 1 je pokupio powerup')
if powerUpAction == 'sonic_speed':
if not self.playerOneSpeed + 10 > 30:
self.playerOneSpeed += 10
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'additional_life':
if self.player.get_lives() < 3:
self.player.lives += 1
print('Player 1 lives: ', self.player.get_lives())
self.update_lives_label(1)
elif powerUpAction == 'turtle_speed':
if self.playerOneSpeed - 10 >= 5:
self.playerOneSpeed -= 10
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'stop_shooting':
self.playerOneCanShoot = False
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
if self.playerTwo.playerLabel == playerLabel:
# Player 2
print('Player 2 je pokupio powerup')
if powerUpAction == 'sonic_speed':
if not self.playerTwoSpeed + 10 > 30:
self.playerTwoSpeed += 10
# Cooldown
# Power up expire timer
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(2, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'additional_life':
if self.playerTwo.get_lives() < 3:
self.playerTwo.lives += 1
self.update_lives_label(2)
elif powerUpAction == 'turtle_speed':
if self.playerTwoSpeed - 10 >= 5:
print("PlayerTwoSpeed: ",self.playerTwoSpeed)
self.playerTwoSpeed -= 10
print("PlayerTwoSpeed: ", self.playerTwoSpeed)
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(2, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'stop_shooting':
self.playerTwoCanShoot = False
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(2, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
else:
# Player 1
print('Player 1 je pokupio powerup')
if powerUpAction == 'sonic_speed':
if not self.playerOneSpeed + 10 > 30:
self.playerOneSpeed += 10
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'additional_life':
if self.player.get_lives() < 3:
self.player.lives += 1
print('Player 1 lives: ', self.player.get_lives())
self.update_lives_label(1)
elif powerUpAction == 'turtle_speed':
if self.playerOneSpeed - 10 >= 5:
self.playerOneSpeed -= 10
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
elif powerUpAction == 'stop_shooting':
self.playerOneCanShoot = False
# Cooldown
self.powerUpCooldownTimer = QTimer()
self.powerUpCooldownTimer.setInterval(config.POWERUP_COOLDOWN_TIMER)
self.powerUpCooldownTimer.timeout.connect(lambda: self.stop_powerup(1, powerUpAction))
self.powerUpCooldownTimer.setSingleShot(True)
self.powerUpCooldownTimer.start()
def stop_powerup(self, playerNum, powerUpAction):
print('Stop powerup player num: ', playerNum)
print('Stop powerup action: ', powerUpAction)
if powerUpAction == 'sonic_speed':
if playerNum == 1:
self.playerOneSpeed = config.PLAYER_SPEED
elif playerNum == 2:
self.playerTwoSpeed = config.PLAYER_SPEED
else:
self.playerOneSpeed = config.PLAYER_SPEED
self.playerTwoSpeed = config.PLAYER_SPEED
elif powerUpAction == 'turtle_speed':
if playerNum == 1:
self.playerOneSpeed = config.PLAYER_SPEED
elif playerNum == 2:
self.playerTwoSpeed = config.PLAYER_SPEED
else:
self.playerOneSpeed = config.PLAYER_SPEED
self.playerTwoSpeed = config.PLAYER_SPEED
elif powerUpAction == 'stop_shooting':
if playerNum == 1:
self.playerOneCanShoot = True
elif playerNum == 2:
self.playerTwoCanShoot = True
else:
self.playerOneCanShoot = True
self.playerTwoCanShoot = True
def next_level(self, current_level):
if self.activePlayers == 0 and len(self.enemyLabels) == 0:
self.displayGameOver()
else:
self.enemyShoot.update_level(config.NEXTLVL_SHOOT_TIMER, config.NEXTLVL_ENEMY_LASER_SPEED)
# Set enemy start positions
self.enemyLabels = []
self.update_level(current_level)
for i in range(3):
for j in range(10):
enemyLabel = QLabel(self)
randIndex = randint(0, len(self.enemyPixmaps) - 1)
enemyLabel.setPixmap(self.enemyPixmaps[randIndex])
positionX = config.IMAGE_WIDTH * (j + 3)
positionY = config.IMAGE_WIDTH * (i + 1)
enemyLabel.setGeometry(positionX, positionY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
enemyLabel.show()
self.enemyLabels.append(enemyLabel)
# add enemies for other stuff
for i in range(len(self.enemyLabels)):
self.moveEnemy.add_enemy(self.enemyLabels[i])
self.enemyShoot.add_enemy(self.enemyLabels[i])
self.shootLaser.add_enemy(self.enemyLabels[i])
self.enemyAttack.add_enemy(self.enemyLabels[i])
def update_level(self, current_level):
print("LEVEL: ", current_level)
gameLevelText = "<font color='white'>Level: {} </font>".format(current_level)
self.gameLevel.setText(gameLevelText)
def activate_enemy_threads(self):
# add player for collision detection first
self.enemyShoot.add_player(self.playerLabel)
self.enemyAttack.add_player(self.playerLabel)
self.deusExMachina.add_player(self.playerLabel)
if self.startPlayers == 2:
self.enemyShoot.add_player(self.playerTwoLabel)
self.enemyAttack.add_player(self.playerTwoLabel)
self.deusExMachina.add_player(self.playerTwoLabel)
# add enemies for other stuff
for i in range(len(self.enemyLabels)):
self.moveEnemy.add_enemy(self.enemyLabels[i])
self.enemyShoot.add_enemy(self.enemyLabels[i])
self.shootLaser.add_enemy(self.enemyLabels[i])
self.enemyAttack.add_enemy(self.enemyLabels[i])
def remove_enemy_label(self, enemyLabel: QLabel):
if enemyLabel in self.enemyLabels:
self.enemyLabels.remove(enemyLabel)
def update_lives_label(self, player):
if player == 1:
lives = self.player.get_lives()
print('Update label lives: ', lives)
if lives == 3:
self.playerLivesLabelText = "<font color='white'>Lives: 3</font>"
self.playerLivesLabel.setText(self.playerLivesLabelText)
elif lives == 2:
self.playerLivesLabelText = "<font color='white'>Lives: 2</font>"
self.playerLivesLabel.setText(self.playerLivesLabelText)
elif lives == 1:
self.playerLivesLabelText = "<font color='white'>Lives: 1</font>"
self.playerLivesLabel.setText(self.playerLivesLabelText)
else:
self.playerLivesLabelText = "<font color='white'>Lives: 0</font>"
self.playerLivesLabel.setText(self.playerLivesLabelText)
# ukloni igraca
self.enemyShoot.remove_player(self.playerLabel)
self.enemyAttack.remove_player(self.playerLabel)
self.deusExMachina.remove_player(self.playerLabel)
self.playerLabel.hide()
self.activePlayers -= 1
# Check for second player
if player == 2:
if self.startPlayers == 2:
lives = self.playerTwo.get_lives()
if lives == 3:
self.playerTwoLivesLabelText = "<font color='white'>Lives: 3</font>"
self.playerTwoLivesLabel.setText(self.playerTwoLivesLabelText)
elif lives == 2:
self.playerTwoLivesLabelText = "<font color='white'>Lives: 2</font>"
self.playerTwoLivesLabel.setText(self.playerTwoLivesLabelText)
elif lives == 1:
self.playerTwoLivesLabelText = "<font color='white'>Lives: 1</font>"
self.playerTwoLivesLabel.setText(self.playerTwoLivesLabelText)
else:
self.playerTwoLivesLabelText = "<font color='white'>Lives: 0</font>"
self.playerTwoLivesLabel.setText(self.playerTwoLivesLabelText)
# ukloni igraca
self.enemyShoot.remove_player(self.playerTwoLabel)
self.enemyAttack.remove_player(self.playerTwoLabel)
self.deusExMachina.remove_player(self.playerTwoLabel)
self.playerTwoLabel.hide()
self.activePlayers -= 1
# check if game over
if self.activePlayers == 0:
while len(self.enemyLabels) != 0:
for enemy in self.enemyLabels:
try:
enemy.hide()
except Exception as e:
print("EXP in game for hide(): ", e)
self.enemyAttack.remove_enemy(enemy)
self.enemyAttack.remove_moving_enemy(enemy)
self.enemyShoot.remove_enemy(enemy)
self.moveEnemy.remove_enemy(enemy)
self.shootLaser.remove_enemy(enemy)
self.shootLaser.remove_falling_enemy(enemy)
self.remove_enemy_label(enemy)
# hide powerup labels
self.powerUpTimer.stop()
self.deusExMachina.shouldCheck = False
print('Stopped power up timer')
for label in self.powerUpLabels:
label.hide()
#game over label
self.gameOver = QLabel(self)
self.gameOver.setFont(QFont("Times", 64, QFont.Bold))
gameOverX = config.BOARD_WIDTH // 2 - config.IMAGE_WIDTH * 5 # centar
gameOverY = config.BOARD_HEIGHT // 2 - config.IMAGE_HEIGHT * 5 - 100
self.gameOver.setGeometry(gameOverX, gameOverY, 550, 550)
gameOverText = "<font color='red'>GAME OVER </font>"
self.gameOver.setText(gameOverText)
self.gameOver.show()
def displayGameOver(self):
self.gameOverSignal.emit()
def hideEnemy(self, enemyLabel: QLabel):
enemyLabel.hide()
def move_enemy(self, enemyLabel: QLabel, newX, newY):
enemyLabel.move(newX, newY)
def enemy_shoot_laser(self, startX, startY):
enemyLaserPixmap = QPixmap('images/enemy_laser.png')
enemyLaserLabel = QLabel(self)
enemyLaserLabel.setPixmap(enemyLaserPixmap)
enemyLaserLabel.setGeometry(startX, startY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
enemyLaserLabel.show()
# dodamo laser da moze da se krece ka dole
self.enemyShoot.add_laser(enemyLaserLabel)
def move_enemy_laser(self, enemyLaser: QLabel, newX, newY):
if newY < config.BOARD_HEIGHT - config.IMAGE_HEIGHT:
enemyLaser.move(newX, newY)
else:
enemyLaser.hide()
self.enemyShoot.remove_laser(enemyLaser)
def enemy_hit_player(self, laserLabel: QLabel, playerLabel: QLabel):
laserLabel.hide()
if self.startPlayers == 2:
if self.player.playerLabel == playerLabel:
self.player.lower_lives()
self.update_lives_label(1)
if self.playerTwo.playerLabel == playerLabel:
self.playerTwo.lower_lives()
self.update_lives_label(2)
else:
self.player.lower_lives()
self.update_lives_label(1)
def enemy_attack_player(self, enemyLabel: QLabel):
self.moveEnemy.remove_enemy(enemyLabel)
self.shootLaser.add_falling_enemy(enemyLabel)
self.shootLaser.remove_enemy(enemyLabel)
self.enemyShoot.remove_enemy(enemyLabel)
def move_enemy_down(self, enemyLabel: QLabel, newX, newY):
if newY < config.BOARD_HEIGHT - config.IMAGE_HEIGHT:
enemyLabel.move(newX, newY)
else:
enemyLabel.hide()
self.enemyAttack.remove_moving_enemy(enemyLabel)
self.shootLaser.remove_falling_enemy(enemyLabel)
self.remove_enemy_label(enemyLabel)
def enemy_attack_player_hit(self, enemyLabel: QLabel, playerLabel: QLabel):
enemyLabel.hide()
self.remove_enemy_label(enemyLabel)
if self.startPlayers == 2:
if self.player.playerLabel == playerLabel:
self.player.lower_lives()
self.update_lives_label(1)
if self.playerTwo.playerLabel == playerLabel:
self.playerTwo.lower_lives()
self.update_lives_label(2)
else:
self.player.lower_lives()
self.update_lives_label(1)
def player_laser_enemy_collide(self, enemyLabel: QLabel, laserLabel: QLabel):
try:
enemyLabel.hide()
laserLabel.hide()
self.remove_enemy_label(enemyLabel)
self.moveEnemy.remove_enemy(enemyLabel)
self.enemyShoot.remove_enemy(enemyLabel)
self.enemyAttack.remove_enemy(enemyLabel)
except Exception as e:
print('Exception in Main_Thread/player_laser_enemy_collide method: ', str(e))
def player_laser_moving_enemy_collide(self, enemyLabel: QLabel, laserLabel: QLabel):
try:
enemyLabel.hide()
laserLabel.hide()
self.remove_enemy_label(enemyLabel)
self.enemyAttack.remove_moving_enemy(enemyLabel)
except Exception as e:
print('Exception in Main_Thread/player_laser_enemy_collide method: ', str(e))
def try_move_player(self, x):
if (x > (config.BOARD_WIDTH - config.IMAGE_WIDTH)) or (x < 0):
return False
return True
def player_shoot_laser(self, startX, startY):
laserPixmap = QPixmap('images/laser.png')
laserLabel = QLabel(self)
laserLabel.setPixmap(laserPixmap)
laserLabel.setGeometry(startX, startY, config.IMAGE_WIDTH, config.IMAGE_HEIGHT)
laserLabel.show()
self.shootLaser.add_laser(laserLabel)
def move_laser_up(self, laserLabel: QLabel, newX, newY):
if newY > 0:
laserLabel.move(newX, newY)
else:
laserLabel.hide()
self.shootLaser.remove_laser(laserLabel)
def __update_position__(self, key):
playerPos = self.playerLabel.geometry()
if key == Qt.Key_D:
if self.try_move_player(playerPos.x() + self.playerOneSpeed):
self.playerLabel.setGeometry(playerPos.x() + self.playerOneSpeed, playerPos.y(), playerPos.width(), playerPos.height())
elif key == Qt.Key_A:
if self.try_move_player(playerPos.x() - self.playerOneSpeed):
self.playerLabel.setGeometry(playerPos.x() - self.playerOneSpeed, playerPos.y(), playerPos.width(), playerPos.height())
elif key == Qt.Key_Space:
if self.player.get_lives() > 0 and self.playerOneCanShoot:
self.player_shoot_laser(playerPos.x() + config.IMAGE_WIDTH//2, playerPos.y() - config.IMAGE_HEIGHT)
# 2 players
if self.startPlayers == 2:
playerTwoPos = self.playerTwoLabel.geometry()
# player two moving
if key == Qt.Key_Right:
if self.try_move_player(playerTwoPos.x() + self.playerTwoSpeed):
self.playerTwoLabel.setGeometry(playerTwoPos.x() + self.playerTwoSpeed, playerTwoPos.y(), playerTwoPos.width(), playerTwoPos.height())
elif key == Qt.Key_Left:
if self.try_move_player(playerTwoPos.x() - self.playerTwoSpeed):
self.playerTwoLabel.setGeometry(playerTwoPos.x() - self.playerTwoSpeed, playerTwoPos.y(), playerTwoPos.width(), playerTwoPos.height())
elif key == Qt.Key_0:
if self.playerTwo.get_lives() > 0 and self.playerTwoCanShoot:
self.player_shoot_laser(playerTwoPos.x() + config.IMAGE_WIDTH // 2, playerTwoPos.y() - config.IMAGE_HEIGHT)
# ovo je mozda moglo u Deus Ex Machina po nekoj logici
# Process for powerup positions
def powerup_process(q: Queue):
print('Powerup process: ', current_process().pid)
while q.qsize() < 10:
# print('Q Size: ', q.qsize())
randomX = randint(0, config.BOARD_WIDTH - config.IMAGE_HEIGHT)
q.put(randomX)
# Process for powerup actions - presporo je ovako
"""
def powerup_action(q: Queue):
powerUpActions = ['additional_life', 'sonic_speed', 'turtle_speed', 'faster_lasers']
randIndex = randint(0, len(powerUpActions))
powerUpAction = powerUpActions[1] # Sonic speed
q.put(powerUpAction)
"""