Esempio n. 1
0
    def initUI(self, width, height):

        self.grafickascena = QGraphicsScene()
        self.grafickascena.setSceneRect(0, 0, width, height)

        self.setbackground()
        self.resumebutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Resume',
                                        40, 40)
        self.resumebutton.clicked.connect(self.resumebuttonclick)
        self.replaybutton = StyleButton('PNG/Buttons/Replay_BTN.png', 'Replay',
                                        40, 40)
        self.replaybutton.clicked.connect(self.replaybuttonclick)
        self.mainmenubutton = StyleButton('PNG/Buttons/Close_BTN.png',
                                          'Main menu', 40, 40)
        self.mainmenubutton.clicked.connect(self.mainmenubuttonclick)

        self.holder = QVBoxLayout()
        self.buttonsLayout = QVBoxLayout()

        self.buttonsLayout.addWidget(self.resumebutton)
        self.buttonsLayout.addWidget(self.replaybutton)
        self.buttonsLayout.addWidget(self.mainmenubutton)
        self.buttonsLayout.setAlignment(Qt.AlignCenter)

        self.titleLabel = QLabel()
        self.titleLabel.setText("GAME PAUSED")
        self.titleLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.titleLabel.setAlignment(Qt.AlignCenter)

        self.holder.addWidget(self.titleLabel)
        self.holder.addLayout(self.buttonsLayout)

        self.setLayout(self.holder)
        self.setScene(self.grafickascena)
Esempio n. 2
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)
Esempio n. 3
0
    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)

        # set default label text
        self.label = QLabel('Car ' + str(self.Car))

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

        # set default car
        carImg = QPixmap('PNG/Car_1_Main_Positions/Car_1_01')
        carImg = carImg.scaled(100, 120)
        self.graphicsPixmapItemCar = QGraphicsPixmapItem(carImg)
        self.graphicsPixmapItemCar.setPos(25, 30)
        self.grafickascena.addItem(self.graphicsPixmapItemCar)

        self.nextButton = StyleButton('PNG/Main_UI/Right', '', 20, 15)
        self.nextButton.resize(20, 20)
        self.nextButton.move(130, 5)
        self.nextButton.clicked.connect(self.nextButtonClicked)
        self.grafickascena.addWidget(self.nextButton)

        self.previousButton = StyleButton('PNG/Main_UI/Left', '', 20, 15)
        self.previousButton.move(10, 5)
        self.previousButton.resize(20, 20)
        self.previousButton.clicked.connect(self.previousButtonClicked)
        self.grafickascena.addWidget(self.previousButton)

        self.playerNameEdit = QLineEdit()
        self.playerNameEdit.resize(130, 30)
        self.playerNameEdit.setText('Enter name')  #  + str(self.playercounter)
        self.playerNameEdit.editingFinished.connect(self.onChanged)
        self.playerNameEdit.move(10, 160)
        self.playerNameEdit.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')

        self.grafickascena.addWidget(self.playerNameEdit)
        self.grafickascena.addWidget(self.label)
        self.setScene(self.grafickascena)
Esempio n. 4
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.ipLabel = QLabel()
        self.ipLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.ipLabel.setAlignment(Qt.AlignCenter)

        self.titleLabel = QLabel()
        self.titleLabel.setText(
            "ENTER YOUR NAME CHOOSE A CAR AND WAIT FOR PLAYERS TO JOIN")
        self.titleLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.titleLabel.setAlignment(Qt.AlignCenter)

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

        self.setbackground()
        self.holder = QVBoxLayout()
        self.hostLayout = QHBoxLayout()
        self.guestsLayout = QHBoxLayout()
        self.buttonsLayout = QHBoxLayout()

        self.playbutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Play', 40,
                                      40)
        self.playbutton.clicked.connect(self.startgame)
        self.backbutton = StyleButton('PNG/Buttons/Close_BTN.png', 'Back', 40,
                                      40)
        self.backbutton.clicked.connect(self.backbuttonClick)

        self.buttonsLayout.addWidget(self.playbutton)
        self.buttonsLayout.addWidget(self.backbutton)
        self.buttonsLayout.setAlignment(Qt.AlignCenter)

        self.hostFrame = InputOkvir(0)
        self.hostLayout.addWidget(self.hostFrame)

        for i in range(0, len(self.guestFrames)):
            self.guestsLayout.addWidget(self.guestFrames[i])

        self.holder.addWidget(self.ipLabel)
        self.holder.addWidget(self.titleLabel)
        self.holder.addLayout(self.hostLayout)
        self.holder.addLayout(self.guestsLayout)
        self.holder.addLayout(self.buttonsLayout)

        self.setLayout(self.holder)

        self.setScene(self.grafickascena)
Esempio n. 5
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

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

        self.setbackground()
        self.oneplayerbutton = StyleButton('PNG/Buttons/Play_BTN.png', '1 player', 40, 40)
        self.oneplayerbutton.clicked.connect(self.oneplayerbuttonclick)
        self.twoplayerbutton = StyleButton('PNG/Buttons/Play_BTN.png', '2 players', 40, 40)
        self.twoplayerbutton.clicked.connect(self.twoplayerbuttonclick)

        self.threeplayerbutton = StyleButton('PNG/Buttons/Play_BTN.png', '3 players', 40, 40)
        self.threeplayerbutton.clicked.connect(self.threeplayerbuttonclick)

        self.fourplayerbutton = StyleButton('PNG/Buttons/Play_BTN.png', '4 players', 40, 40)
        self.fourplayerbutton.clicked.connect(self.fourplayerbuttonclick)

        self.backbutton = StyleButton('PNG/Buttons/Close_BTN.png', 'Back', 40, 40)
        self.backbutton.clicked.connect(self.backbuttonclick)

        self.holder = QVBoxLayout()
        self.playersLayout1 = QHBoxLayout()
        self.playersLayout2 = QHBoxLayout()


        self.playersLayout1.addWidget(self.oneplayerbutton)
        self.playersLayout1.addWidget(self.twoplayerbutton)
        self.playersLayout1.setAlignment(Qt.AlignCenter)

        self.playersLayout2.addWidget(self.threeplayerbutton)
        self.playersLayout2.addWidget(self.fourplayerbutton)
        self.playersLayout2.setAlignment(Qt.AlignCenter)

        self.titleLabel = QLabel()
        self.titleLabel.setText("CHOOSE NUMBER OF PLAYERS:")
        self.titleLabel.setStyleSheet('color: yellow; font-weight: bold; background: transparent;')
        self.titleLabel.setAlignment(Qt.AlignCenter)

        self.holder.addWidget(self.titleLabel)
        self.holder.addLayout(self.playersLayout1)
        self.holder.addLayout(self.playersLayout2)

        self.holder.addWidget(self.backbutton, alignment=Qt.AlignCenter)

        self.setLayout(self.holder)
        self.setScene(self.grafickascena)
Esempio n. 6
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

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

        self.setbackground()
        self.backbutton = StyleButton('PNG/Buttons/Close_BTN.png', 'Back', 40, 40)
        self.playbutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Play', 40, 40)

        self.playbutton.clicked.connect(self.initNextPhase)
        self.backbutton.clicked.connect(self.backbuttonClick)

        self.holder = QVBoxLayout()
        self.playerslayout3 = QHBoxLayout()
        self.playerslayout2 = QHBoxLayout()
        self.playerslayout1 = QHBoxLayout()
        self.buttonsLayout = QHBoxLayout()

        self.titleLabel = QLabel()
        self.titleLabel.setText("ENTER A NAME AND CHOOSE A CAR:")
        self.titleLabel.setStyleSheet('color: yellow; font-weight: bold; background: transparent;')
        self.titleLabel.setAlignment(Qt.AlignCenter)

        self.playerslayout2.addWidget(self.winner1phase)
        self.playerslayout2.addWidget(self.winner2phase)
        self.playerslayout1.addWidget(self.winner)

        self.playerslayout2.setAlignment(Qt.AlignHCenter)
        self.playerslayout1.setAlignment(Qt.AlignHCenter)

        self.buttonsLayout.addWidget(self.backbutton)
        self.buttonsLayout.addWidget(self.playbutton)
        self.buttonsLayout.setAlignment(Qt.AlignCenter)

        self.holder.addWidget(self.titleLabel)
        self.holder.addLayout(self.playerslayout1)
        self.holder.addLayout(self.playerslayout2)
        self.holder.addLayout(self.playerslayout3)
        self.holder.addLayout(self.buttonsLayout)

        self.setLayout(self.holder)
        self.setScene(self.grafickascena)
Esempio n. 7
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.holder = QVBoxLayout()
        self.playersLayout1 = QHBoxLayout()
        self.playersLayout2 = QHBoxLayout()
        self.playersLayout3 = QHBoxLayout()

        self.playersLayout1.setAlignment(Qt.AlignCenter)
        self.playersLayout2.setAlignment(Qt.AlignCenter)
        self.playersLayout3.setAlignment(Qt.AlignCenter)

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

        self.setbackground()
        self.backbtn1 = StyleButton('PNG/Buttons/Close_BTN.png',
                                    'Back to Menu', 40, 40)
        self.backbtn1.clicked.connect(self.backtomenu)

        self.backbtn2 = StyleButton('PNG/Buttons/Replay_BTN.png', 'Replay', 40,
                                    40)
        self.backbtn2.clicked.connect(self.replay)

        self.playersLayout3.addWidget(self.backbtn1)

        self.playersLayout3.addWidget(self.backbtn2)

        self.okvir = HUDOkvir(
            "player1", 1)  # OVDE UBACITI KOJI IGRAC JE POBEDIO I NJEGOV AUTO

        tempImg = QPixmap('PNG/You_Win/Header.png')
        tempImg = tempImg.scaled(500, 100)
        self.graphicsPixmapItem = QGraphicsPixmapItem(tempImg)
        self.grafickascena.addItem(self.graphicsPixmapItem)
        self.graphicsPixmapItem.setPos(220, 100)
        self.playersLayout2.addWidget(self.okvir)
        self.holder.addLayout(self.playersLayout1)
        self.holder.addLayout(self.playersLayout2)
        self.holder.addLayout(self.playersLayout3)
        self.setScene(self.grafickascena)
        self.setLayout(self.holder)
Esempio n. 8
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)
Esempio n. 9
0
class InputOkvir(QGraphicsView):
    def __init__(self, number):  # number
        super().__init__()
        #self.playercounter = number + 1  # to avoid 'Player 0' name
        self.playerName = ''
        self.Car = 1  # default car choice is 1
        self.graphicsPixmapItemCar = QGraphicsPixmapItem()
        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)

        # set default label text
        self.label = QLabel('Car ' + str(self.Car))

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

        # set default car
        carImg = QPixmap('PNG/Car_1_Main_Positions/Car_1_01')
        carImg = carImg.scaled(100, 120)
        self.graphicsPixmapItemCar = QGraphicsPixmapItem(carImg)
        self.graphicsPixmapItemCar.setPos(25, 30)
        self.grafickascena.addItem(self.graphicsPixmapItemCar)

        self.nextButton = StyleButton('PNG/Main_UI/Right', '', 20, 15)
        self.nextButton.resize(20, 20)
        self.nextButton.move(130, 5)
        self.nextButton.clicked.connect(self.nextButtonClicked)
        self.grafickascena.addWidget(self.nextButton)

        self.previousButton = StyleButton('PNG/Main_UI/Left', '', 20, 15)
        self.previousButton.move(10, 5)
        self.previousButton.resize(20, 20)
        self.previousButton.clicked.connect(self.previousButtonClicked)
        self.grafickascena.addWidget(self.previousButton)

        self.playerNameEdit = QLineEdit()
        self.playerNameEdit.resize(130, 30)
        self.playerNameEdit.setText('Enter name')  #  + str(self.playercounter)
        self.playerNameEdit.editingFinished.connect(self.onChanged)
        self.playerNameEdit.move(10, 160)
        self.playerNameEdit.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')

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

    def onChanged(self):
        self.playerName = self.playerNameEdit.text()

    def nextButtonClicked(self):
        # generate path to the next car based on the current car
        self.tempCar = ((self.Car + 1) % 4)
        if self.tempCar == 0:
            self.Car = 1
        else:
            self.Car = self.tempCar

        path = 'PNG/Car_' + str(self.Car) + '_Main_Positions/Car_' + str(
            self.Car) + '_01'

        # replace car image
        carImg = QPixmap(path)
        carImg = carImg.scaled(100, 120)
        self.grafickascena.removeItem(self.graphicsPixmapItemCar)
        self.graphicsPixmapItemCar = QGraphicsPixmapItem(carImg)
        self.graphicsPixmapItemCar.setPos(25, 30)
        self.grafickascena.addItem(self.graphicsPixmapItemCar)

        # change label text above the image
        self.label.setText('Car ' + str(self.Car))

    def previousButtonClicked(self):
        # generate path to the previous car based on the current car
        self.tempCar = ((self.Car - 1) % 4)
        if self.tempCar == 0:
            self.Car = 3
        else:
            self.Car = self.tempCar
        path = 'PNG/Car_' + str(self.Car) + '_Main_Positions/Car_' + str(
            self.Car) + '_01'

        # replace car image
        carImg = QPixmap(path)
        carImg = carImg.scaled(100, 120)
        self.grafickascena.removeItem(self.graphicsPixmapItemCar)
        self.graphicsPixmapItemCar = QGraphicsPixmapItem(carImg)
        self.graphicsPixmapItemCar.setPos(25, 30)
        self.grafickascena.addItem(self.graphicsPixmapItemCar)

        # change label text above the image
        self.label.setText('Car ' + str(self.Car))
Esempio n. 10
0
class menuView(QGraphicsView):
    def __init__(self, centralWidget: QStackedWidget):

        super(menuView, self).__init__()

        self.viewlist = centralWidget
        self.initUI()

    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

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

        self.setbackground()
        self.playbutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Play', 40,
                                      40)
        self.playbutton.clicked.connect(self.playbuttonclick)
        self.tourney2button = StyleButton('PNG/You_Win/Play_Tournament_1.png',
                                          '2 Player tournament', 40, 40)
        self.tourney2button.clicked.connect(self.tournament2click)
        self.tourney4button = StyleButton('PNG/You_Win/Play_Tournament_2.png',
                                          '4 Player tournament', 40, 40)
        self.tourney4button.clicked.connect(self.tournament4click)
        self.connectroombutton = StyleButton('PNG/Buttons/Play_BTN.png',
                                             'Connect to host', 40, 40)
        self.connectroombutton.clicked.connect(self.connectRoomButtonClick)
        self.hostbutton = StyleButton('PNG/You_Win/Play_BTN.png',
                                      'Host a game', 40, 40)
        self.hostbutton.clicked.connect(self.hostgame)
        self.exitbtn = StyleButton('PNG/Buttons/Close_BTN.png', 'Exit', 40, 40)
        self.exitbtn.clicked.connect(self.closeThis)

        self.grafickascena.addWidget(self.playbutton)
        self.grafickascena.addWidget(self.tourney2button)
        self.grafickascena.addWidget(self.tourney4button)
        self.grafickascena.addWidget(self.hostbutton)
        self.grafickascena.addWidget(self.connectroombutton)
        self.grafickascena.addWidget(self.exitbtn)

        self.playbutton.move(150, 250)
        self.tourney2button.move(150, 300)
        self.tourney4button.move(150, 350)
        self.hostbutton.move(150, 400)
        self.connectroombutton.move(150, 450)
        self.exitbtn.move(150, 500)

        self.setScene(self.grafickascena)

    def hostgame(self):
        self.hostview = self.viewlist.widget(1)
        self.hostview.startServerHosting()
        self.viewlist.setCurrentWidget(self.hostview)

    def tournament2click(self):
        self.turnir = self.viewlist.widget(4)
        self.turnir.initFrames(2, 1)
        self.viewlist.setCurrentWidget(self.turnir)

    def tournament4click(self):

        self.turnir = self.viewlist.widget(4)
        self.turnir.initFrames(4, 2)
        self.viewlist.setCurrentWidget(self.turnir)

    def setbackground(self):
        tempImg = QPixmap('PNG/9c49087c09fd07a10ae3887a7825f389.jpg')
        tempImg = tempImg.scaled(self.grafickascena.width(),
                                 self.grafickascena.height())

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

    def playbuttonclick(self):

        self.viewlist.setCurrentWidget(self.viewlist.widget(3))

    def connectRoomButtonClick(self):
        self.viewlist.setCurrentWidget(self.viewlist.widget(7))

    def closeThis(self):
        sys.exit()
Esempio n. 11
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

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

        self.setbackground()
        self.playbutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Play', 40,
                                      40)
        self.playbutton.clicked.connect(self.playbuttonclick)
        self.tourney2button = StyleButton('PNG/You_Win/Play_Tournament_1.png',
                                          '2 Player tournament', 40, 40)
        self.tourney2button.clicked.connect(self.tournament2click)
        self.tourney4button = StyleButton('PNG/You_Win/Play_Tournament_2.png',
                                          '4 Player tournament', 40, 40)
        self.tourney4button.clicked.connect(self.tournament4click)
        self.connectroombutton = StyleButton('PNG/Buttons/Play_BTN.png',
                                             'Connect to host', 40, 40)
        self.connectroombutton.clicked.connect(self.connectRoomButtonClick)
        self.hostbutton = StyleButton('PNG/You_Win/Play_BTN.png',
                                      'Host a game', 40, 40)
        self.hostbutton.clicked.connect(self.hostgame)
        self.exitbtn = StyleButton('PNG/Buttons/Close_BTN.png', 'Exit', 40, 40)
        self.exitbtn.clicked.connect(self.closeThis)

        self.grafickascena.addWidget(self.playbutton)
        self.grafickascena.addWidget(self.tourney2button)
        self.grafickascena.addWidget(self.tourney4button)
        self.grafickascena.addWidget(self.hostbutton)
        self.grafickascena.addWidget(self.connectroombutton)
        self.grafickascena.addWidget(self.exitbtn)

        self.playbutton.move(150, 250)
        self.tourney2button.move(150, 300)
        self.tourney4button.move(150, 350)
        self.hostbutton.move(150, 400)
        self.connectroombutton.move(150, 450)
        self.exitbtn.move(150, 500)

        self.setScene(self.grafickascena)
Esempio n. 12
0
    def initUI(self):

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

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

        self.setbackground()
        self.backbutton = StyleButton('PNG/Buttons/Close_BTN.png', 'Back', 40,
                                      40)
        self.backbutton.clicked.connect(self.backtomenu)
        self.playbutton = StyleButton('PNG/Buttons/Play_BTN.png', 'Play', 40,
                                      40)
        self.playbutton.clicked.connect(self.play)

        self.holder = QVBoxLayout()
        self.playerLayout = QHBoxLayout()
        self.buttonsLayout = QHBoxLayout()
        self.hostLayout = QHBoxLayout()

        self.ipLabel = QLabel()
        self.ipLabel.setText("ENTER HOST ADDRESS: ")
        self.ipLabel.setFixedWidth(150)
        self.ipLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')

        self.hostText = QLineEdit()
        self.hostText.setFixedWidth(150)
        self.hostText.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.hostText.setAlignment(Qt.AlignLeft)

        self.hostLayout.addWidget(self.ipLabel)
        self.hostLayout.addWidget(self.hostText)
        self.hostLayout.setAlignment(Qt.AlignJustify)

        self.titleLabel = QLabel()
        self.titleLabel.setText("ENTER A NAME AND CHOOSE A CAR:")
        self.titleLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.titleLabel.setAlignment(Qt.AlignCenter)

        self.playerLayout.addWidget(self.inputFrame)

        self.buttonsLayout.addWidget(self.playbutton)
        self.buttonsLayout.addWidget(self.backbutton)
        self.buttonsLayout.setAlignment(Qt.AlignCenter)

        self.infoLabel = QLabel(
        )  # moze da se doda kasnije ako bude neka greska, npr server posalje da je vec je poslao neko taj username, da ne moze da se konektuje itd
        self.infoLabel.setStyleSheet(
            'color: yellow; font-weight: bold; background: transparent;')
        self.infoLabel.setAlignment(Qt.AlignCenter)

        self.holder.addLayout(self.hostLayout)
        self.holder.addWidget(self.titleLabel)
        self.holder.addLayout(self.playerLayout)
        self.holder.addWidget(self.infoLabel)
        self.holder.addLayout(self.buttonsLayout)

        self.setLayout(self.holder)
        self.setScene(self.grafickascena)