Exemple #1
0
class Barrel(QObject):
    draw_signal = pyqtSignal()
    remove_signal = pyqtSignal()
    fall_signal = pyqtSignal()

    def __init__(self, parent, index):
        super().__init__()
        self.__parent__ = parent
        self.item = QGraphicsPixmapItem()
        self.index = index

        self.current_animation_index = 0
        self.animation_frames = [
            QPixmap(IMAGES_DIR + "barrel/barrel_falling_1.png"),
            QPixmap(IMAGES_DIR + "barrel/barrel_falling_2.png")
        ]
        self.item.setPixmap(self.animation_frames[0])
        self.item.setZValue(4)

        self.draw_signal.connect(self.__draw)
        self.remove_signal.connect(self.__remove)
        self.fall_signal.connect(self.__fall)

    """ Handles animations """

    def __animate(self):
        count = len(self.animation_frames)
        if self.current_animation_index + 1 > count - 1:
            self.current_animation_index = 0
            self.item.setPixmap(
                self.animation_frames[self.current_animation_index])
        else:
            self.current_animation_index = self.current_animation_index + 1
            self.item.setPixmap(
                self.animation_frames[self.current_animation_index])

    """ Draws barrel to scene """

    def __draw(self):
        self.__parent__.draw_barrel(self.index)

    """ Removes barrel from scene """

    def __remove(self):
        self.__parent__.remove_barrel(self.index)

    """ Moves barrel down """

    def __fall(self):
        self.__animate()
        self.item.moveBy(0, 5)
Exemple #2
0
class Boardgame(QGraphicsView):
    def __init__(self, centralWidget: QStackedWidget):

        super(Boardgame, self).__init__()
        self.hud = HUD()
        self.obstacles = [
            Obstacle(100),
            Obstacle(100),
            Obstacle(100),
            Obstacle(100)
        ]
        self.previous = 0  #obstacle
        self.viewlist = centralWidget
        self.winnerCar = ""
        self.winnerName = ""
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.keybed1 = [Qt.Key_Right, Qt.Key_Down, Qt.Key_Up, Qt.Key_Left]
        self.keybed2 = [Qt.Key_D, Qt.Key_S, Qt.Key_W, Qt.Key_A]
        self.keybed3 = [Qt.Key_L, Qt.Key_K, Qt.Key_I, Qt.Key_J]
        self.keybed4 = [Qt.Key_6, Qt.Key_5, Qt.Key_8, Qt.Key_4]
        self.keybeds = [self.keybed1, self.keybed2, self.keybed3, self.keybed4]
        self.initUI()
        self.gametype = 0
        self.networkcode = None

        #timer
        self.level = 1
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.speedUp)

        self.workertimer = QtCore.QTimer(self)
        self.workertimer.timeout.connect(self.movepicture)
        self.workertimer.timeout.connect(self.moveObstacle)

        self.collisionNotifier = Worker(100)
        self.collisionNotifier.update.connect(self.checkCollision)

        self.players = []  # moving cars
        self.deaths = [0, 0, 0, 0]  #number of deaths per player

        #self.worker = Worker(10)
        #self.worker.update.connect(self.movepicture)
        #self.worker.update.connect(self.moveObstacle)

        expipe, inpipe = mp.Pipe()
        self.process = ProcessCount(expipe, 0.01)
        self.listenerthread = WorkerListen(inpipe)
        self.listenerthread.update.connect(self.checkCollision)

        # 1. step - set one transition image - self.graphicsPixmapItem set to transition image
        # 2. step - set transition image on one and new image on the other (the one that previously had the transition image)
        # self.graphicsPixmapItem - new level image
        # self.mapcontiniue - transition image
        # 3. step - set the new image on the one that previously had the transition image
        # self.mapcontinue - new level image

        self.phaseIndex = -1
        self.nextBackground = 1

    def activateThreads(self):
        #self.worker.start() # resume option, not reseting obstacle position
        #self.collisionNotifier.start()
        self.process.startprocess()
        self.listenerthread.start()
        self.activatePlayerThreads(
        )  # for each player start key notifier thread
        self.timer.start(5000)
        self.workertimer.start(10)

    def stopThreads(self):
        #self.worker.stop()
        #self.collisionNotifier.stop()
        self.process.terminate()
        self.process.join()
        self.listenerthread.stop()
        self.workertimer.stop()
        self.stopPlayerThreads()  # for each player stop key notifier thread
        self.timer.stop()
        if self.gametype == 3 and self.networkcode.id:
            self.networkcode.stopNetworkThreads()

    def setStartPositions(self):
        for obsticale in self.obstacles:
            self.createObstacle(obsticale)

    def restart(self):
        self.setStartPositions(
        )  # reset option, resets all the positions and starts the thread again
        self.playerStartPositions(self.players)
        self.playerStartLives(self.players)
        self.hud.restart()
        self.hud.initHudFrames(self.players)
        self.playerStartLivesHud()
        self.resetBackground()
        self.activateThreads()
        self.level = 1
        self.deaths = [0, 0]

    def playerStartPositions(self, players):
        self.widthPosition = 550 / (1 + len(players))
        self.padding = 0
        for i in range(len(players)):
            self.players[i].setPos(
                150 + self.widthPosition + self.widthPosition * i +
                self.padding * i, 500)
            self.padding = 50

    def playerStartLives(self, players):
        for player in players:
            player.resetLives()

    # reset hearts shown in the hud frame for each player
    def playerStartLivesHud(self):
        for player in self.players:
            self.hud.updatePlayerLives(player)

    def stopPlayerThreads(self):
        for player in self.players:
            player.stopThread()

    # key notifier threads # question
    def activatePlayerThreads(self):
        for player in self.players:
            player.activateThreads()

    # add players to the boardgame
    def initPlayers(self, players, gametype, networkCode=None):
        self.players.clear()
        #host
        self.networkcode = networkCode

        self.i = 0
        for player in players:
            # players dictionary: key(player) - playerName, value(players[player]) - playerCar
            if gametype != 3:
                self.player = Player(player, str(players[player]),
                                     self.keybeds[self.i],
                                     self.grafickascena.width() / 15)
            else:
                self.player = Player(player, str(players[player]),
                                     self.keybeds[0],
                                     self.grafickascena.width() / 15)
            self.player.setNetworkCode(self.networkcode)
            self.players.append(self.player)
            self.grafickascena.addItem(self.player)
            self.i = self.i + 1

        self.gametype = gametype
        self.restart()

        if gametype == 3:
            for ob in self.obstacles:
                ob.hide()
            self.pauseButton.hide()
        else:
            self.pauseButton.show()

        # init hud frame with array of Player objects
        self.hud.initHudFrames(self.players)
        if gametype == 1:  # 1v1 turnir
            self.hud.setMode(1)

    # remove each car from the graphics scene, stop all threads, delete players list
    def deletePlayers(self):
        for player in self.players:
            self.grafickascena.removeItem(player)

        self.stopThreads()
        self.players = []
        self.deaths = [0, 0]

    def initUI(self):

        self.grafickascena = QGraphicsScene()
        self.grafickascena.setSceneRect(0, 0, 1000, 850)

        self.tempImg = QPixmap('PNG/background/road0.png')
        self.tempImg = self.tempImg.scaled(self.grafickascena.width(),
                                           self.grafickascena.height())

        self.graphicsPixmapItem = QGraphicsPixmapItem(self.tempImg)
        self.grafickascena.addItem(self.graphicsPixmapItem)

        self.mapContinue = QGraphicsPixmapItem(self.tempImg)
        self.grafickascena.addItem(self.mapContinue)
        self.graphicsPixmapItem.setY(-self.tempImg.height())

        self.grafickascena.addItem(self.obstacles[0])
        self.grafickascena.addItem(self.obstacles[1])
        self.grafickascena.addItem(self.obstacles[2])
        self.grafickascena.addItem(self.obstacles[3])

        #self.setStartPositions() #restart se zove na play button click pa ne treba ovde

        self.grafickascena.addWidget(
            self.hud).setY(self.grafickascena.height() - self.hud.height())

        self.pauseButton = StyleButton('PNG/Main_UI/Pause_BTN.png', 'Play', 40,
                                       40)
        self.pauseButton.clicked.connect(self.pauseButtonClick)
        self.pauseButton.move(self.grafickascena.width() - 45, 8)
        self.grafickascena.addWidget(self.pauseButton)

        self.setScene(self.grafickascena)

    def pauseButtonClick(self):
        self.pauseview = pauseView(self.viewlist,
                                   self.grafickascena.width() / 4,
                                   self.grafickascena.height() / 4)
        self.pauseview.move(self.grafickascena.width() / 3,
                            self.grafickascena.height() / 3)
        self.pauseButton.worker.killThread = True
        self.pauseButton.setEnabled(False)
        self.grafickascena.addWidget(self.pauseview)
        self.pauseview.show()
        self.stopThreads()

    def speedUp(self):
        self.level += 1
        if self.level % 4 == 0:
            self.setNextBackgroundPath()
            self.phaseIndex = 0
        self.hud.updateHUD()

    def resetBackground(self):
        self.phaseIndex = -1
        self.nextBackground = 1
        self.startBackground = QPixmap("PNG/Background/road0")
        self.startBackground = self.startBackground.scaled(
            self.grafickascena.width(), self.grafickascena.height())
        self.graphicsPixmapItem.setPixmap(self.startBackground)
        self.mapContinue.setPixmap(self.startBackground)

    def setNextBackgroundPath(self):
        self.pathNext = "PNG/Background/road" + str(self.nextBackground % 5)
        self.pathTransition = "PNG/Background/road" + str(
            (self.nextBackground - 1) % 5) + str(self.nextBackground % 5)
        self.nextBackground = self.nextBackground + 1

    def startBackgroundTransition(self):
        self.graphicsPixmapItem.setPixmap(
            QPixmap(self.pathTransition).scaled(self.grafickascena.width(),
                                                self.grafickascena.height()))
        self.phaseIndex = 1

    def middleBackgroundTransition(self):
        self.mapContinue.setPixmap(
            QPixmap(self.pathTransition).scaled(self.grafickascena.width(),
                                                self.grafickascena.height()))
        self.graphicsPixmapItem.setPixmap(
            QPixmap(self.pathNext).scaled(self.grafickascena.width(),
                                          self.grafickascena.height()))
        self.phaseIndex = 2

    def completeBackgroundTransition(self):
        self.mapContinue.setPixmap(
            QPixmap(self.pathNext).scaled(self.grafickascena.width(),
                                          self.grafickascena.height()))
        self.phaseIndex = -1

    @pyqtSlot()
    def movepicture(self):
        self.graphicsPixmapItem.moveBy(0, 2 + self.level * 0.2)
        res1 = self.graphicsPixmapItem.y() % self.tempImg.height()
        self.mapContinue.setY(res1)

        if self.graphicsPixmapItem.y() >= 0:
            if self.phaseIndex != -1:
                if self.phaseIndex == 0:
                    self.startBackgroundTransition()
                elif self.phaseIndex == 1:
                    self.middleBackgroundTransition()
                elif self.phaseIndex == 2:
                    self.completeBackgroundTransition()

            self.graphicsPixmapItem.setY(-self.tempImg.height())

    @pyqtSlot()
    def moveObstacle(self):

        for Ob in self.obstacles:
            Ob.moveBy(0, 2 + self.level * 0.2)
            if Ob.y() > (self.grafickascena.height() - 200):
                self.createObstacle(Ob)

    def createObstacle(self, Ob: Obstacle):

        if self.gametype == 3 and self.networkcode.id == False:
            return

        x = random.randint(170, 720)
        y = random.randint(-self.grafickascena.height(), -100)

        #while (x>(self.previous-100)) & (x<(self.previous+100)):
        ##    x = random.randint(300, 700)
        #self.previous = x
        Ob.setY(y)
        Ob.setX(x)
        Ob.setObstaclePix()
        sansa = random.randint(
            0, 100
        )  # simulacija coin toss-a. Ako je sansa 0 sakriti prepreku. Ako je jedan prikazati.
        # prvih nekoliko prepreka se pojavljuje na pocetku nezavisno od sanse?

        visible = False

        if self.level < 7:
            if sansa > self.level * 10:
                Ob.hide()
                visible = False
            else:
                Ob.show()
                visible = True
        else:
            if sansa > 70:
                Ob.hide()
                visible = False
            else:
                Ob.show()
                visible = True

        if self.gametype == 3 and self.networkcode.id == True:
            self.networkcode.broadcastObstacles(self.obstacles.index(Ob), x, y,
                                                Ob.type, visible)

    @pyqtSlot(int, int, int, int, bool)
    def networkSetObstacles(self, index, x, y, type, visible):
        obstacle = self.obstacles[index]
        obstacle.setPos(x, y)
        obstacle.setObstaclePixNum(type)
        if visible:
            obstacle.show()
        else:
            obstacle.hide()

    @pyqtSlot(str, float, float, int)
    def networkPlayerPosition(self, name, x, y, keyindex):
        for player in self.players:
            if player.playerName == name:
                player.setPos(x, y)
                player.checkifCollision(self.keybed1[keyindex])

    def keyPressEvent(self, event) -> None:
        if self.gametype == 3:
            if event.key() in self.keybed1:
                name = self.networkcode.getName()
                for player in self.players:
                    if player.playerName == name:
                        player.keyPressEvent(event)
        else:
            if event.key() in self.keybed1:
                self.players[0].keyPressEvent(event)
            if len(self.players) >= 2:
                if event.key() in self.keybed2:
                    self.players[1].keyPressEvent(event)
            if len(self.players) >= 3:
                if event.key() in self.keybed3:
                    self.players[2].keyPressEvent(event)
            if len(self.players) >= 4:
                if event.key() in self.keybed4:
                    self.players[3].keyPressEvent(event)

    def keyReleaseEvent(self, event) -> None:
        if self.gametype == 3:
            if event.key() in self.keybed1:
                name = self.networkcode.getName()
                for player in self.players:
                    if player.playerName == name:
                        player.keyReleaseEvent(event)
        else:
            if event.key() in self.keybed1:
                self.players[0].keyReleaseEvent(event)

            if len(self.players) >= 2:
                if event.key() in self.keybed2:
                    self.players[1].keyReleaseEvent(event)
            if len(self.players) >= 3:
                if event.key() in self.keybed3:
                    self.players[2].keyReleaseEvent(event)
            if len(self.players) >= 4:
                if event.key() in self.keybed4:
                    self.players[3].keyReleaseEvent(event)

    @pyqtSlot()
    def checkCollision(self):

        for player in self.players:
            for item in player.collidingItems():
                if isinstance(item, Obstacle):
                    if player.killable:
                        if item.id == 0:
                            player.die()
                            self.hud.updatePlayerLives(player)
                            self.checkAlivePlayers(player.playerName,
                                                   player.Car)
                            self.setPlayerPosition(player)

                            if self.gametype == 1:  # 1v1
                                index = self.players.index(player)
                                self.deaths[index] = self.deaths[index] + 1
                                self.hud.setHUDResult(self.deaths[0],
                                                      self.deaths[1])
                                opindex = 1 - index  # Opposite index 1 - 0 = 1, 1 - 1 = 0.

                                if (self.deaths[index] == 2
                                        and self.deaths[opindex]
                                        == 0) or self.deaths[index] == 3:
                                    self.setTournamentWinner(
                                        self.players[opindex])
                                    # self.drawTournamentTree(self.players[0])
                                    self.deletePlayers()
                            # 4 man tournament
                            elif self.gametype == 2:
                                index = self.players.index(player)
                                opindex = 1 - index

                                if self.players[index].getLives() == 0:
                                    # self.setTournamentWinner(self.players[0])
                                    self.drawTournamentTree(
                                        self.players[opindex])
                                    self.deletePlayers()

                        elif item.id == 1:
                            player.disableMoving()
                        elif item.id == 2:
                            item.hide(
                            )  # if not hidden it would add lives as long as the car is still colliding with it, other players could get it as well
                            player.addLife()
                            self.hud.updatePlayerLives(player)

    def drawTournamentTree(self, player):
        self.stopThreads()
        self.tree = self.viewlist.widget(5)
        self.winnerName, self.winnerCar = player.getNameCar()
        self.tree.setPhaseWinner(self.winnerName, self.winnerCar)
        self.viewlist.setCurrentWidget(self.tree)

    def setTournamentWinner(self, player):
        self.stopThreads()
        self.winner = self.viewlist.widget(6)
        self.winnerName, self.winnerCar = player.getNameCar()
        self.winner.lastPlayer(self.winnerName, self.winnerCar)
        self.viewlist.setCurrentWidget(self.winner)

    # position the player on the original starting position
    def setPlayerPosition(self, player):
        self.widthPosition = 550 / (1 + len(self.players))
        self.padding = 50
        for i in range(len(self.players)):
            if player == self.players[i]:
                self.players[i].setPos(
                    150 + self.widthPosition + self.widthPosition * i +
                    self.padding * i, 500)

    def checkAlivePlayers(self, playerName, playerCar):
        anyAlive = False
        for player in self.players:
            if player.lives != 0:
                anyAlive = True

        if anyAlive == False:
            self.View = self.viewlist.widget(6)
            self.View.lastPlayer(playerName, playerCar)
            self.stopThreads()
            self.viewlist.setCurrentWidget(self.View)
Exemple #3
0
class HUDOkvir(QGraphicsView):
    def __init__(self, name, car):
        super().__init__()
        self.name = name
        self.car = car
        self.lives = []  #array of QGraphicsPixmapItem for hearts
        self.initUI()

    def initUI(self):

        self.setStyleSheet("background: transparent")
        self.setFrameShape(QFrame.NoFrame)

        self.grafickascena = QGraphicsScene()
        self.grafickascena.setSceneRect(0, 0, 150, 200)

        tempImg = QPixmap('PNG/Level_Menu/Window.png')
        tempImg = tempImg.scaled(self.grafickascena.width(),
                                 self.grafickascena.height())

        self.graphicsPixmapItem = QGraphicsPixmapItem(tempImg)
        self.grafickascena.addItem(self.graphicsPixmapItem)

        self.path = 'PNG/Car_' + str(self.car) + '_Main_Positions/Car_' + str(
            self.car) + '_01'
        #slika na okviru za hud
        img = QPixmap(self.path)
        img = img.scaled(100, 120)
        self.image = QGraphicsPixmapItem(img)
        self.image.moveBy(25, 30)
        self.grafickascena.addItem(self.image)

        self.initLives(4)  # makes 4 heart images and positions them correctly
        self.setLives(3)  # show only 3 on init
        #-----------------------------------------

        self.label = QLabel(self.name)
        self.label.setFixedWidth(120)
        self.label.move(33, 11)
        self.label.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')

        self.grafickascena.addWidget(self.label)
        self.setScene(self.grafickascena)

    def getNameAndCar(self):
        return self.name, self.car

    def setNameAndCar(self, name, car):
        self.name = name
        self.car = car

        self.label.setText(name)
        if car == '0':
            self.grafickascena.removeItem(self.image)
        else:
            self.image.setPixmap(
                QPixmap('PNG/Car_' + car + '_Main_Positions/Car_' + car +
                        '_01').scaled(100, 120))
            self.grafickascena.addItem(self.image)

    def initLives(self, lifeCount):
        for i in range(lifeCount):
            heart = QPixmap('PNG/Main_UI/HP_Dot.png')
            heart = QGraphicsPixmapItem(heart.scaledToWidth(32))
            heart.moveBy(8 + i * 33, 160)
            self.lives.append(heart)
            self.grafickascena.addItem(heart)

    # show the exact amount of hearts a specific player has
    def setLives(self, lifeCount):
        for i in range(lifeCount):
            self.lives[i].show()
        for i in range(
                4 - lifeCount
        ):  # maximum number of lives is 4, calculate how many to hide
            self.lives[3 - i].hide()  # start from the last
Exemple #4
0
class Gorilla(QObject):
    move_signal = pyqtSignal(Direction)
    animation_reset_signal = pyqtSignal(Direction)
    throw_start_signal = pyqtSignal()
    throw_finish_signal = pyqtSignal()

    def __init__(self, x, y):
        super().__init__()
        self.item = QGraphicsPixmapItem()
        self.current_frame_index = 0
        self.movement_frames = [
            QPixmap(IMAGES_DIR + 'gorilla/move/move_1.png'),
            QPixmap(IMAGES_DIR + "gorilla/move/move_2.png")
        ]
        self.throw_frames = [
            QPixmap(IMAGES_DIR + "gorilla/throw/throw_down.png"),
            QPixmap(IMAGES_DIR + "gorilla/throw/throw_up.png")
        ]
        self.item.setPixmap(self.movement_frames[0])
        self.item.setPos(x, y)

        self.move_signal[Direction].connect(self.__move)
        self.animation_reset_signal[Direction].connect(self.__reset_animation)
        self.throw_start_signal.connect(self.__throw_start)
        self.throw_finish_signal.connect(self.__throw_finish)

    """ Handles animations """

    def __animate(self):
        count = len(self.movement_frames)
        if self.current_frame_index + 1 > count - 1:
            self.current_frame_index = 0
        else:
            self.current_frame_index = self.current_frame_index + 1

        self.item.setPixmap(self.movement_frames[self.current_frame_index])

    """ Resets animations """

    def __reset_animation(self, direction: Direction):
        if direction == Direction.LEFT:
            self.item.setPixmap(self.movement_frames[0])
        elif direction == Direction.RIGHT:
            self.item.setPixmap(self.movement_frames[1])

    """ Handles movement """

    def __move(self, direction: Direction):
        if direction == Direction.LEFT:
            self.__go_left()
        elif direction == Direction.RIGHT:
            self.__go_right()

    """ Moves gorilla left """

    def __go_left(self):
        self.__animate()
        self.item.moveBy(-40, 0)

    """ Moves gorilla right """

    def __go_right(self):
        self.__animate()
        self.item.moveBy(40, 0)

    """ Starts throwing animation """

    def __throw_start(self):
        self.item.setPixmap(self.throw_frames[0])

    """ Starts throw finish animation """

    def __throw_finish(self):
        self.item.setPixmap(self.throw_frames[1])
class PlayableCharacter(QObject):
    set_lives_signal = pyqtSignal(int)
    set_coin_life_signal = pyqtSignal(int)
    move_signal = pyqtSignal(Direction)
    animation_reset_signal = pyqtSignal(Direction)
    fall_signal = pyqtSignal()
    climb_up_signal = pyqtSignal(ClimbState)
    climb_down_signal = pyqtSignal(ClimbState)
    remove_signal = pyqtSignal()
    update_points_signal = pyqtSignal(int)

    def __init__(self, parent):
        super().__init__()
        self.__parent__ = parent
        self.item = QGraphicsPixmapItem()
        self.lives = QGraphicsPixmapItem()
        self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_3.png"))
        self.lives_remaining = 0
        self.font = QFont()
        self.font.setPointSize(12)
        self.points = QGraphicsTextItem("0")
        self.points.setDefaultTextColor(Qt.white)
        self.points.setFont(self.font)
        self.falling = False
        self.climbing = False
        self.alive = True
        self.starting_x = 0
        self.starting_y = 0
        self.keys_pressed = set()
        self.current_frame_index = 0
        self.latest_direction = None
        self.movement_frames_left = []
        self.movement_frames_right = []
        self.climb_frames = []
        self.climb_finish_frames = []
        self.default_frame_left = None
        self.default_frame_right = None
        self.default_frame_up = None

        self.move_signal[Direction].connect(self.__move)
        self.animation_reset_signal[Direction].connect(self.__reset_animation)
        self.fall_signal.connect(self.__fall)
        self.climb_up_signal[ClimbState].connect(self.__climb_up)
        self.climb_down_signal[ClimbState].connect(self.__climb_down)
        self.set_lives_signal[int].connect(self.set_lives)
        self.set_coin_life_signal[int].connect(self.set_coin_life)
        self.update_points_signal[int].connect(self.__update_points)

    """ Changes lives image and removes self from scene on 0 lives """

    def set_lives(self, lives: int):
        self.lives_remaining = lives
        if self.lives_remaining == 3:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_3.png"))
        elif self.lives_remaining == 2:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_2.png"))
        elif self.lives_remaining == 1:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_1.png"))
        elif self.lives_remaining == 0:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_0.png"))
            self.alive = False

        if self.alive:
            self.item.setPos(self.starting_x, self.starting_y)
        else:
            self.__parent__.removeItem(self.item)

    def set_coin_life(self, lives: int):
        self.lives_remaining = lives
        if self.lives_remaining == 3:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_3.png"))
        elif self.lives_remaining == 2:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_2.png"))
        elif self.lives_remaining == 1:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_1.png"))
        elif self.lives_remaining == 0:
            self.lives.setPixmap(QPixmap(IMAGES_DIR + "lives/lives_0.png"))
            self.__parent__.removeItem(self.item)

    """ Changes points counter in the scene """

    def __update_points(self, points: int):
        self.points.setPlainText(str(points))
        self.__parent__.set_points_position()

    """ Handles movement animations """

    def __animate(self, direction: Direction, state=False):
        count = -1
        if direction == Direction.LEFT:
            count = len(self.movement_frames_left)
        elif direction == Direction.RIGHT:
            count = len(self.movement_frames_right)
        elif direction == Direction.UP or direction == Direction.DOWN:
            count = len(self.climb_frames)

        if self.current_frame_index + 1 > count - 1:
            self.current_frame_index = 0
        else:
            self.current_frame_index = self.current_frame_index + 1

        if direction == Direction.LEFT:
            self.item.setPixmap(
                self.movement_frames_left[self.current_frame_index])
        elif direction == Direction.RIGHT:
            self.item.setPixmap(
                self.movement_frames_right[self.current_frame_index])
        elif direction == Direction.UP:
            if state is True:
                self.item.setPixmap(
                    self.climb_finish_frames[self.current_frame_index])
            else:
                self.item.setPixmap(
                    self.climb_frames[self.current_frame_index])
        elif direction == Direction.DOWN:
            if state is True:
                frames = self.climb_finish_frames
                frames.reverse()
                self.item.setPixmap(frames[self.current_frame_index])
            else:
                self.item.setPixmap(
                    self.climb_frames[self.current_frame_index])

    """ Resets animations to default frame """

    def __reset_animation(self, direction: Direction):
        if direction == Direction.LEFT:
            self.item.setPixmap(self.default_frame_left)
        elif direction == Direction.RIGHT:
            self.item.setPixmap(self.default_frame_right)
        elif direction == Direction.UP:
            self.item.setPixmap(self.default_frame_up)
        elif direction == Direction.DOWN:
            self.item.setPixmap(self.default_frame_up)

    """ Handles movement """

    def __move(self, direction: Direction):
        if direction == direction.LEFT:
            self.__animate(Direction.LEFT)
            self.item.moveBy(-5, 0)
        elif direction == direction.RIGHT:
            self.__animate(Direction.RIGHT)
            self.item.moveBy(5, 0)

    """ Handles climbing up"""

    def __climb_up(self, climb_state: ClimbState):
        if climb_state == ClimbState.CLIMB:
            self.climbing = True
            self.__animate(Direction.UP)
            self.latest_direction = Direction.UP
            self.item.moveBy(0, -5)
        elif climb_state == ClimbState.FINISH:
            self.climbing = True
            self.__finish_climbing_up()
        elif climb_state == ClimbState.NONE:
            self.climbing = False
            self.__reset_animation(Direction.UP)

    """ Handles finishing climbing up  """

    def __finish_climbing_up(self):
        self.__animate(Direction.UP, state=True)
        self.item.moveBy(0, -5)

    """ Handles climbing down """

    def __climb_down(self, climb_state: ClimbState):
        if climb_state == ClimbState.CLIMB:
            self.climbing = True
            self.__animate(Direction.DOWN)
            self.item.moveBy(0, 5)
        elif climb_state == ClimbState.FINISH:
            self.climbing = True
            self.__finish_climbing_down()
        elif climb_state == ClimbState.NONE:
            self.climbing = False
            self.__reset_animation(Direction.DOWN)

    """ Handles finishing climbing down """

    def __finish_climbing_down(self):
        self.__animate(Direction.DOWN, state=True)
        self.item.moveBy(0, 5)

    """ Handles falling """

    def __fall(self):
        self.item.moveBy(0, 5)