Example #1
0
    def moveBullets(self):
        bullets_to_be_removed = []
        for bullet in self.parent_widget.bullet_list:
            if bullet in bullets_to_be_removed:
                continue
            self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE)

            new_x = bullet.x
            new_y = bullet.y

            if bullet.orientation == Orientation.UP:
                new_y -= 1
            elif bullet.orientation == Orientation.RIGHT:
                new_x += 1
            elif bullet.orientation == Orientation.DOWN:
                new_y += 1
            elif bullet.orientation == Orientation.LEFT:
                new_x -= 1

            if Helper.isCollision(self.parent_widget, new_x, new_y, ElementType.BULLET):
                self.bulletImpact(new_x, new_y, bullet, bullets_to_be_removed)
            else:
                bullet.x = new_x
                bullet.y = new_y
                self.parent_widget.setShapeAt(bullet.x, bullet.y, Helper.enumFromOrientationBullet
                (bullet.orientation))

        for bullet in bullets_to_be_removed:
            if bullet in self.parent_widget.bullet_list:
                self.parent_widget.bullet_list.remove(bullet)
                self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE)
                bullet.bullet_owner.active_bullet = None

        self.sendUpdatedBullets()
    def playerMoved(self, new_x, new_y, new_orientation):
        self.parent_widget.setShapeAt(self.player.x, self.player.y, ElementType.NONE)
        self.player.x = new_x
        self.player.y = new_y
        self.player.orientation = new_orientation

        self.parent_widget.setShapeAt(self.player.x, self.player.y, Helper.enumFromOrientationPlayer(self.player.player_type, self.player.orientation))
    def moveEnemy(self):
        enemies_to_be_removed = []

        for enemy in self.parent_widget.enemy_list:
            new_x = enemy.x
            new_y = enemy.y

            if enemy.direction == Orientation.UP:
                new_y -= 1
                new_orientation = Orientation.RIGHT
            elif enemy.direction == Orientation.RIGHT:
                new_x += 1
                new_orientation = Orientation.DOWN
            elif enemy.direction == Orientation.DOWN:
                new_y += 1
                new_orientation = Orientation.LEFT
            elif enemy.direction == Orientation.LEFT:
                new_x -= 1
                new_orientation = Orientation.UP

            if Helper.isCollision(self.parent_widget, new_x, new_y,
                                  ElementType.ENEMY):
                is_bullet_collision = False
                board_width = self.parent_widget.BoardWidth
                board_height = self.parent_widget.BoardHeight

                if 0 <= new_x <= board_width - 1 and 0 <= new_y <= board_height - 1:
                    next_shape = self.parent_widget.getShapeType(new_x, new_y)

                    try:
                        type = self.parent_widget.findBulletAt(new_x,
                                                               new_y).type
                    except:
                        type = BulletType.ENEMY

                    if (next_shape == ElementType.BULLET
                            or ElementType.BULLET_UP <= next_shape <=
                            ElementType.BULLET_LEFT
                        ) and type == BulletType.FRIEND:
                        is_bullet_collision = True
                        self.parent_widget.setShapeAt(enemy.x, enemy.y,
                                                      ElementType.NONE)
                        enemies_to_be_removed.append(enemy)
                        bullet_to_die = self.parent_widget.findBulletAt(
                            new_x, new_y)
                        if bullet_to_die is not None:
                            self.parent_widget.setShapeAt(
                                bullet_to_die.x, bullet_to_die.y,
                                ElementType.NONE)
                            self.parent_widget.bullet_list.remove(
                                bullet_to_die)
                            bullet_to_die.bullet_owner.active_bullet = None

                if not is_bullet_collision:
                    enemy.direction = new_orientation
                    self.parent_widget.setShapeAt(
                        enemy.x, enemy.y,
                        Helper.enumFromOrientationEnemy(new_orientation))
            else:
                self.parent_widget.setShapeAt(enemy.x, enemy.y,
                                              ElementType.NONE)
                enemy.x = new_x
                enemy.y = new_y
                self.parent_widget.setShapeAt(
                    enemy.x, enemy.y,
                    Helper.enumFromOrientationEnemy(enemy.direction))

        for elemnt in enemies_to_be_removed:
            self.parent_widget.enemy_list.remove(elemnt)
            self.parent_widget.num_of_all_enemies -= 1
            self.send_status_update(
                enemies_left=self.parent_widget.num_of_all_enemies)
            if self.parent_widget.num_of_all_enemies > 0:  #dodavanje novog enemyja
                while (True):
                    rand_x = randint(0, self.parent_widget.BoardWidth)
                    if self.parent_widget.getShapeType(rand_x,
                                                       0) == ElementType.NONE:
                        break

                self.parent_widget.enemy_list.append(EnemyTank(rand_x))
                self.parent_widget.setShapeAt(rand_x, 0,
                                              ElementType.ENEMY_DOWN)

            elif self.parent_widget.num_of_all_enemies == -3:
                self.parent_widget.advanceToNextLevel()

        self.chosen_enemy = self.chooseRandomEnemy()
        if self.chosen_enemy is not None:
            if self.chosen_enemy.fireBullet():
                if Helper.isCollision(self.parent_widget,
                                      self.chosen_enemy.active_bullet.x,
                                      self.chosen_enemy.active_bullet.y,
                                      ElementType.BULLET):
                    self.bulletImpactOnFire(self.chosen_enemy.active_bullet.x,
                                            self.chosen_enemy.active_bullet.y,
                                            self.chosen_enemy.active_bullet)
                else:
                    self.bulletFired()

        self.sendUpdatedEnemies()
 def bulletFired(self):
     bullet = self.chosen_enemy.active_bullet
     self.parent_widget.setShapeAt(
         bullet.x, bullet.y,
         Helper.enumFromOrientationBullet(bullet.orientation))
     self.parent_widget.bullet_list.append(bullet)
    def bulletImpactOnFire(self, new_x, new_y, bullet):
        if not (0 <= new_x <= self.parent_widget.BoardWidth - 1
                and 0 <= new_y <= self.parent_widget.BoardHeight - 1):
            bullet.bullet_owner.active_bullet = None
            return

        next_shape = self.parent_widget.getShapeType(new_x, new_y)

        if next_shape == ElementType.WALL:
            self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE)

        elif next_shape == ElementType.BULLET or (ElementType.BULLET_UP <=
                                                  next_shape <=
                                                  ElementType.BULLET_LEFT):
            other_bullet = self.parent_widget.findBulletAt(new_x, new_y)
            if other_bullet is not None:
                self.parent_widget.setShapeAt(other_bullet.x, other_bullet.y,
                                              ElementType.NONE)
                self.parent_widget.bullet_list.remove(other_bullet)
                other_bullet.bullet_owner.active_bullet = None
            else:
                print(
                    "Move enemy thread: bulletImpactOnFire(): other_bullet is None"
                )

        elif (
                next_shape == ElementType.PLAYER1 or
            (ElementType.PLAYER1_UP <= next_shape <= ElementType.PLAYER1_LEFT)
                or next_shape == ElementType.PLAYER2 or
            (ElementType.PLAYER2_UP <= next_shape <=
             ElementType.PLAYER2_LEFT)) and bullet.type == BulletType.ENEMY:

            if next_shape == ElementType.PLAYER1 or (ElementType.PLAYER1_UP <=
                                                     next_shape <=
                                                     ElementType.PLAYER1_LEFT):
                gb_player = self.parent_widget.player_1
                starting_position = self.parent_widget.player_1_starting_position
            elif next_shape == ElementType.PLAYER2 or (
                    ElementType.PLAYER2_UP <= next_shape <=
                    ElementType.PLAYER2_LEFT):
                gb_player = self.parent_widget.player_2
                starting_position = self.parent_widget.player_2_starting_position

            gb_player.lives -= 1
            if gb_player.player_type == PlayerType.PLAYER_1:
                self.send_status_update(player_1_life=gb_player.lives)
            elif gb_player.player_type == PlayerType.PLAYER_2:
                self.send_status_update(player_2_life=gb_player.lives)

            if gb_player.lives > 0:
                self.parent_widget.setShapeAt(gb_player.x, gb_player.y,
                                              ElementType.NONE)
                gb_player.x = starting_position[0]
                gb_player.y = starting_position[1]
                gb_player.orientation = Orientation.UP
                self.parent_widget.setShapeAt(
                    gb_player.x, gb_player.y,
                    Helper.enumFromOrientationPlayer(gb_player.player_type,
                                                     Orientation.UP))
            else:
                self.parent_widget.gameOver()

        elif next_shape == ElementType.BASE and bullet.type == BulletType.ENEMY:
            self.parent_widget.gameOver()

        bullet.bullet_owner.active_bullet = None
Example #6
0
    def bulletImpact(self, new_x, new_y, bullet, bullets_to_be_removed):
        if not (0 <= new_x <= self.parent_widget.BoardWidth - 1 and 0 <= new_y <= self.parent_widget.BoardHeight - 1):
            self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE)
            bullets_to_be_removed.append(bullet)
            return

        next_shape = self.parent_widget.getShapeType(new_x, new_y)
        levelChanged = False
        if next_shape == ElementType.WALL:
            self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE)

        elif next_shape == ElementType.BULLET or (ElementType.BULLET_UP <= next_shape <= ElementType.BULLET_LEFT):
            other_bullet = self.findBulletAt(new_x, new_y)
            self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE)
            if other_bullet is not None:
                # self.parent_widget.setShapeAt(other_bullet.x, other_bullet.y, ElementType.NONE) #mozda setShape na new_x, new_y?
                bullets_to_be_removed.append(other_bullet)
                print("find other bullet!")
            else:
                print("bulletImpact(): other_bullet is None")

        elif (next_shape == ElementType.PLAYER1 or (ElementType.PLAYER1_UP <= next_shape <= ElementType.PLAYER1_LEFT) or next_shape == ElementType.PLAYER2 or (ElementType.PLAYER2_UP <= next_shape <= ElementType.PLAYER2_LEFT)) and bullet.type == BulletType.ENEMY:
            if next_shape == ElementType.PLAYER1 or (ElementType.PLAYER1_UP <= next_shape <= ElementType.PLAYER1_LEFT):
                gb_player = self.parent_widget.player_1
                starting_position = self.parent_widget.player_1_starting_position
            elif next_shape == ElementType.PLAYER2 or (ElementType.PLAYER2_UP <= next_shape <= ElementType.PLAYER2_LEFT):
                gb_player = self.parent_widget.player_2
                starting_position = self.parent_widget.player_2_starting_position



            gb_player.lives -= 1
            if gb_player.player_type == PlayerType.PLAYER_1:
                self.send_status_update(player_1_life=gb_player.lives) #slanje zahteva na klijent za osvezavanje stat framea
            elif gb_player.player_type == PlayerType.PLAYER_2:
                self.send_status_update(player_2_life=gb_player.lives)

            if gb_player.lives > 0:
                self.parent_widget.setShapeAt(gb_player.x, gb_player.y, ElementType.NONE)
                gb_player.x = starting_position[0]
                gb_player.y = starting_position[1]
                gb_player.orientation = Orientation.UP
                self.parent_widget.setShapeAt(gb_player.x, gb_player.y, Helper.enumFromOrientationPlayer(gb_player.player_type, Orientation.UP))
            else:
                self.parent_widget.gameOver()
        elif (next_shape == ElementType.ENEMY or (ElementType.ENEMY_UP <= next_shape <= ElementType.ENEMY_LEFT)) and bullet.type == BulletType.FRIEND:
            self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE)

            for enemy in self.parent_widget.enemy_list:
                if new_x == enemy.x and new_y == enemy.y:
                    self.parent_widget.enemy_list.remove(enemy)
                    self.parent_widget.num_of_all_enemies -= 1
                    self.send_status_update(enemies_left=self.parent_widget.num_of_all_enemies)
                    if self.parent_widget.num_of_all_enemies > 0:
                        while (True):
                            rand_x = randint(0, self.parent_widget.BoardWidth)
                            if self.parent_widget.getShapeType(rand_x, 0) == ElementType.NONE:
                                break

                        self.parent_widget.enemy_list.append(EnemyTank(rand_x))
                        self.parent_widget.setShapeAt(rand_x, 0, ElementType.ENEMY_DOWN)
                    elif self.parent_widget.num_of_all_enemies == -3:
                        self.parent_widget.advanceToNextLevel()
                        levelChanged = True
                    break

        elif next_shape == ElementType.BASE and bullet.type == BulletType.ENEMY:
            self.parent_widget.gameOver()

        if not levelChanged:
            bullets_to_be_removed.append(bullet)
            self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE)
    def movePlayer(self):
        #new_x, new_y, new_orientation = None

        text = ""
        changed = False
        while True:
            try:
                message = self.socket.recv(1024)
            except:
                self.cancel()
                return
            self.parent_widget.mutex.lock()

            new_x = self.player.x
            new_y = self.player.y
            new_orientation = self.player.orientation
            text += str(message, 'utf8')
            if not message or len(message) < 1024:
                break
            self.parent_widget.mutex.unlock()
            
        if not self.is_freezed:
            if text == "UP":
                new_y -= 1
                new_orientation = Orientation.UP
                changed = True
            elif text == "DOWN":
                new_y += 1
                new_orientation = Orientation.DOWN
                changed = True
            elif text == "RIGHT":
                new_x += 1
                new_orientation = Orientation.RIGHT
                changed = True
            elif text == "LEFT":
                new_x -= 1
                new_orientation = Orientation.LEFT
                changed = True
        if text == "FIRE":
            if self.player.fireBullet():

                if Helper.isCollision(self.parent_widget,
                                      self.player.active_bullet.x,
                                      self.player.active_bullet.y,
                                      ElementType.BULLET):

                    self.bulletImpactOnFire(self.player.active_bullet.x,
                                            self.player.active_bullet.y,
                                            self.player.active_bullet)
                else:
                    self.bulletFired()

        if changed:
            if Helper.isCollision(self.parent_widget, new_x, new_y, ElementType.PLAYER2):
                if not (
                        new_x >= self.parent_widget.BoardWidth or new_x < 0 or new_y >= self.parent_widget.BoardHeight or new_y < 0):
                    if self.parent_widget.getShapeType(new_x, new_y) == ElementType.FREEZE:
                        self.is_freezed = True
                        self.parent_widget.setShapeAt(self.player.x, self.player.y, ElementType.NONE)
                        self.player.x = new_x
                        self.player.y = new_y
                        self.parent_widget.setShapeAt(new_x, new_y, Helper.enumFromOrientationPlayer(self.player.player_type, self.player.orientation))
                        t = Timer(5, self.timeOut)
                        t.start()
                    elif self.parent_widget.getShapeType(new_x, new_y) == ElementType.LIFE:
                        self.player.lives += 1
                        if self.player.player_type == PlayerType.PLAYER_1:
                            self.send_status_update(player_1_lives=self.player.lives)
                        else:
                            self.send_status_update(player_2_lives=self.player.lives)
                    else:
                        new_x = self.player.x
                        new_y = self.player.y
                else:
                    new_x = self.player.x
                    new_y = self.player.y

                self.parent_widget.setShapeAt(new_x, new_y, Helper.enumFromOrientationPlayer(self.player.player_type, self.player.orientation))

            self.playerMoved(new_x, new_y, new_orientation)

        self.sendUpdatedPlayers()

        self.parent_widget.mutex.unlock()