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 keyPressListener(self, key): if key == Qt.Key_A or key == Qt.Key_W or key == Qt.Key_D or key == Qt.Key_Space: current_row = self.player1.coordinate.row current_column = self.player1.coordinate.column desiredCoordinate = pos.Coordinate(current_row,current_column) shoot = False if key == Qt.Key_A: desiredCoordinate.column -= 1 elif key == Qt.Key_D: desiredCoordinate.column += 1 elif key == Qt.Key_W: desiredCoordinate.row -= 1 else: shoot = True if self.player1.isAlive() and self.player1.spawned: self.player1_move_thread = Thread(None,self.move_player,args=[self.player1, desiredCoordinate], name="Player1Thread") self.player1_move_thread.start() if shoot: self.player1_shoot_thread = Thread(None, self.shoot, args=[self.player1], name="Player1ShootThread") self.player1_shoot_thread.start() elif key == Qt.Key_Left or Qt.Key_Right or Qt.Key_Up or Qt.Key_0: current_row = self.player2.coordinate.row current_column = self.player2.coordinate.column desiredCoordinate = pos.Coordinate(current_row,current_column) shoot = False if key == Qt.Key_Left : desiredCoordinate.column -= 1 elif key == Qt.Key_Right: desiredCoordinate.column += 1 elif key == Qt.Key_Up: desiredCoordinate.row -= 1 else: shoot = True if self.player2.isAlive() and self.player2.spawned: self.player2_move_thread = Thread(None,self.move_player,args=[self.player2, desiredCoordinate], name="Player2Thread") self.player2_move_thread.start() if shoot: self.player2_shoot_thread = Thread(None, self.shoot, args=[self.player2], name="Player2ShootThread") self.player2_shoot_thread.start()
def __init__(self, name): self.name = name self.label_right = None self.label_left = None self.label = None self.action = None self.coordinate = pos.Coordinate(-1, -1) self.speed = 1
def __init__(self, id): self.coordinate = pos.Coordinate(-1, -1) self.id = id self.action = None self.label_mode_1 = None self.label_mode_2 = None self.label_mode_3 = None self.mode = -1 self.alive = False
def shoot(self, coordinate, bubble): print("Bubble shoot called for bubble: ", bubble, " with coordinate: ", coordinate) cor = pos.Coordinate(coordinate.row, coordinate.column) for i in range(3): sleep(BUBBLE_SPEED) if self.map.isCoordinateAvailable(cor, bubble) and bubble.isAlive(): self.move_success(cor, bubble) if bubble.action == "shoot_r": cor.column += 1 elif bubble.action == "shoot_l": cor.column -= 1 else: break print("Bubble shoot ended for bubble: ", bubble, " now coordinate is: ", bubble.coordinate) # After shoot is done, we need to start moving bubble on top cor = pos.Coordinate(bubble.coordinate.row - 1, bubble.coordinate.column) self.bubbleAntiGravityThread = Thread(None, self.bubbleAntiGravity, args=[bubble, cor], name="BubbleAGThread") self.bubbleAntiGravityThread.start()
def __init__(self, name, id): super().__init__() self.id = id self.name = name self.label_left = None self.label_left_imuned = None self.label_right = None self.label_right_imuned = None self.label = None self.action = None self.canMove = False self.coordinate = pos.Coordinate(-1, -1) self.initCoordinate = pos.Coordinate( -1, -1) self.lifes = 3 self.imune = False self.spawned = False self.dir = None self.imuneTimerThread = None self.movePlayerAfterLifeLossThread = None self.game_engine = None
def jump(self, coordinate, entity): cor = pos.Coordinate(coordinate.row, coordinate.column) for i in range(3): sleep(JUMP_SLEEP_DURATION) if self.map.isCoordinateAvailable(cor, entity): self.move_success(cor, entity) cor.row = cor.row - 1 else: break # when we finish jump we need to check for gravity self.gravity(entity)
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 startAI(self, enemy): while(self.running): # Find which player is closer. If distance is same, attack player 1 :P id = self.getCloserPlayer(enemy) #print("Goten id: ", id) # Execute step to move closer to player which is closer desiredCoordinate = None if id == 1: desiredCoordinate = pos.Coordinate(self.player1.coordinate.row, self.player1.coordinate.column) else: desiredCoordinate = pos.Coordinate(self.player2.coordinate.row, self.player2.coordinate.column) # Try to move to same column as player, so either gravity will move you to player, # or jump can move you to the player if desiredCoordinate.column < enemy.coordinate.column: enemy.action = "move_left" self.gameEngine.move(pos.Coordinate(enemy.coordinate.row, enemy.coordinate.column - 1), enemy) elif desiredCoordinate.column > enemy.coordinate.column: enemy.action = "move_right" self.gameEngine.move(pos.Coordinate(enemy.coordinate.row, enemy.coordinate.column + 1), enemy) else: if desiredCoordinate.row > enemy.coordinate.row: print("F**k it, i am confused, just go right, as real patriot") enemy.action = "move_right" self.gameEngine.move(pos.Coordinate(enemy.coordinate.row, enemy.coordinate.column + 1), enemy) elif desiredCoordinate.row < enemy.coordinate.row: enemy.action = "jump" self.gameEngine.move(pos.Coordinate(enemy.coordinate.row-1 , enemy.coordinate.column), enemy) sleep(self.thinkTime)
def gravity(self, entity): if not self.map.isGravityNeeded(entity): return coordinateBellow = pos.Coordinate(entity.coordinate.row + 1, entity.coordinate.column) gravity = True while(gravity): sleep(GRAVITY_SLEEP_DURATION) if not self.map.isGravityNeeded(entity): gravity = False else: entity.action = "gravity" self.move_success(coordinateBellow, entity) coordinateBellow.row += 1
def getCloserPlayer(self, enemy): print("Get closer player called") # If both players are dead, stop game if not self.player1.isAlive() and not self.player2.isAlive(): print("Killed both players, stopping game...") self.running = False else: # If p1 is dead, chase p2 if not self.player1.isAlive(): return 2 # If p2 is dead, chase p1 elif not self.player2.isAlive(): return 1 # When player lost life, he becomes imune to enemy. So while he is imune, he is of no iterest to us if self.player1.imune: print("P1 is imune, so i am chasing p2") return 2 elif self.player2.imune: print("P2 is imune, so i am chasing p1") return 1 p1Coordinate = pos.Coordinate(self.player1.coordinate.row, self.player1.coordinate.column) p2Coordinate = pos.Coordinate(self.player2.coordinate.row, self.player2.coordinate.column) enemyCoordinate = pos.Coordinate(enemy.coordinate.row, enemy.coordinate.column) p1Value = 16 * p1Coordinate.row + p1Coordinate.column p2Value = 16 * p2Coordinate.row + p2Coordinate.column enemyValue = 16 * enemyCoordinate.row + enemyCoordinate.column p1Diff = abs(enemyValue - p1Value) p2Diff = abs(enemyValue - p2Value) chase = min(p1Diff, p2Diff) if chase == p1Diff: return 1 else: return 2
def afterLifeLoss(self): # Reset coordinates and set player to be imuned, so enemy is not interested in us anymore! self.coordinate = pos.Coordinate(-1, -1) self.imune = True # Disable pressing buttons untill player is not again spawned on map self.spawned = False # Create imune time thread, which will start when we return to our init pos # Try to move player to init pos, after life loss self.imuneTimerThread = Thread(None,self.startImuneTimer, name="imuneTimerThread") self.movePlayerAfterLifeLossThread = Thread(None,self.movePlayerAfterLifeLoss, name="moveAfterLifeLossThread") self.movePlayerAfterLifeLossThread.start()
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)
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 setupPlayer(self, initCoordinate, game_engine): self.action = "init" self.canMove = True self.spawned = True self.game_engine = game_engine self.initCoordinate = pos.Coordinate(initCoordinate.row, initCoordinate.column) if self.id == 1: self.label_left = "characters/bub_left.png" self.label_left_imuned = "characters/bub_left_imuned.png" self.label_right = "characters/bub_right.png" self.label_right_imuned = "characters/bub_right_imuned.png" self.dir = "right" else: self.label_left = "characters/bob_left.png" self.label_left_imuned = "characters/bob_left_imuned.png" self.label_right = "characters/bob_right.png" self.label_right_imuned = "characters/bob_right_imuned.png" self.dir = "left"
def shoot(self, player): bub = bubble.Bubble(player.id) coordinate = pos.Coordinate(-1,-1) if player.dir == "right": if player.id == 1: bub.setupBubble(1) else: bub.setupBubble(1) bub.action = "shoot_r" coordinate.setCoordinate(player.coordinate.row, player.coordinate.column + 1) elif player.dir == "left": if player.id == 1: bub.setupBubble(1) else: bub.setupBubble(1) bub.action = "shoot_l" coordinate.setCoordinate(player.coordinate.row, player.coordinate.column - 1) self.gameEngine.move(coordinate, bub)
import key_notifier import map import game_engine import player import pos import enemy import bubble from threading import Thread from time import sleep SCREEN_WIDTH = 1200 SCREEN_HEIGHT = 960 P1_INIT_COORDINATE = pos.Coordinate(14,1) P2_INIT_COORDINATE = pos.Coordinate(14,14) ENEMY_INIT_COORDIATE = pos.Coordinate(5,7) class GameWindow(QMainWindow): win_change_signal = QtCore.pyqtSignal() def __init__(self, list_of_names): super().__init__() # p1 = green # p2 = blue # Create map and game engine self.map = map.Map()
def move_success(self, coordinate, entity): oldCoordinate = pos.Coordinate(entity.coordinate.row, entity.coordinate.column) entity.coordinate.setCoordinate(coordinate.row, coordinate.column) #print("Entity: ", entity, " executed action: ", entity.action, " from: ", oldCoordinate, " to: ", coordinate) self.map.updateMap(oldCoordinate, coordinate, entity)
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')