Example #1
0
class VideoSettingsScreen(Screen):
    def __init__(self):
        Screen.__init__(self)

        # 504p button
        self.fiveOFourButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 135,
            (255, 255, 255),
            (0, 0, 0),
            "896 x 504",
        )

        # 648p button
        self.sixFourtyEightButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 80,
            (255, 255, 255),
            (0, 0, 0),
            "1152 x 648",
        )

        # 720p button
        self.sevenTwentyButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 25,
            (255, 255, 255),
            (0, 0, 0),
            "1280 x 720",
        )

        # Fullscreen button
        self.fullScreenButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 30,
            (255, 255, 255),
            (0, 0, 0),
            "FULLSCREEN",
        )

        # Back button
        self.mainMenuButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 85,
            (255, 255, 255),
            (0, 0, 0),
            "MAIN MENU",
        )

        # List of buttons
        self.buttonList.append(self.fiveOFourButton)
        self.buttonList.append(self.sixFourtyEightButton)
        self.buttonList.append(self.sevenTwentyButton)
        self.buttonList.append(self.fullScreenButton)
        self.buttonList.append(self.mainMenuButton)

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if the 504p button was clicked
                        if self.fiveOFourButton.isClicked(event.pos):
                            self.updateUi(896, 504)

                        # Check if the 648p button was clicked
                        if self.sixFourtyEightButton.isClicked(event.pos):
                            self.updateUi(1152, 648)

                        # Check if the 720p button was clicked
                        if self.sevenTwentyButton.isClicked(event.pos):
                            self.updateUi(1280, 720)

                        # Check if the fullscreen button was clicked
                        if self.fullScreenButton.isClicked(event.pos):
                            self.updateUi(1920, 1080, True)

                        # Check if back button was clicked
                        if self.mainMenuButton.isClicked(event.pos):
                            return GameState.TITLE

                # Check if mouse is hovering over button
                self.isMouseHoveringOverButtons()

            # Draw everything to the screen
            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        # Draw buttons and stuff
        self.fiveOFourButton.draw(self.screen)
        self.sixFourtyEightButton.draw(self.screen)
        self.sevenTwentyButton.draw(self.screen)
        self.fullScreenButton.draw(self.screen)
        self.mainMenuButton.draw(self.screen)
        self.displayTitle()

        pygame.display.flip()

    def displayTitle(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "VIDEO SETTINGS"
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)
        self.screen.blit(text, ((Resources.SCREEN_WIDTH / 2) - (textWidth / 2), 0))

    def updateUi(self, width, height, fullscreen=False):

        # Resize the screen
        self.screen, self.background = Resources.set_screen_size(width, height, fullscreen)

        # Update positions of other widgets
        self.fiveOFourButton.setPos(
            (Resources.SCREEN_WIDTH / 2) - 100, (Resources.SCREEN_HEIGHT / 2) - 135
        )
        self.sixFourtyEightButton.setPos(
            (Resources.SCREEN_WIDTH / 2) - 100, (Resources.SCREEN_HEIGHT / 2) - 80
        )
        self.sevenTwentyButton.setPos(
            (Resources.SCREEN_WIDTH / 2) - 100, (Resources.SCREEN_HEIGHT / 2) - 25
        )
        self.fullScreenButton.setPos(
            (Resources.SCREEN_WIDTH / 2) - 100, (Resources.SCREEN_HEIGHT / 2) + 30
        )
        self.mainMenuButton.setPos(
            (Resources.SCREEN_WIDTH / 2) - 100, (Resources.SCREEN_HEIGHT / 2) + 85
        )
Example #2
0
class HostScreen(Screen):
    def __init__(self):

        # Call Screen constructor
        Screen.__init__(self)

        # Host button
        self.hostButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 25,
            (255, 255, 255),
            (0, 0, 0),
            "HOST GAME",
        )

        # Back button
        self.mainMenuButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 80,
            (255, 255, 255),
            (0, 0, 0),
            "MAIN MENU",
        )

        # List of buttons
        self.buttonList.append(self.hostButton)
        self.buttonList.append(self.mainMenuButton)

        # Text box to enter game name
        self.textBox = TextBox(
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 30,
            200,
            50,
        )

        # Game mode incrementer
        gameModeOptions = ["Normal", "Jick"]
        self.gameModeIncrementer = Incrementer(gameModeOptions)
        INCREMENTER_X = (Resources.SCREEN_WIDTH / 2) - (
            self.gameModeIncrementer.incrementerWidth / 2
        )
        INCREMENTER_Y = 40
        self.gameModeIncrementer.setPos(INCREMENTER_X, INCREMENTER_Y)

        # Number of players incrementer
        numPlayersOptions = ["3", "4"]
        self.numPlayersIncrementer = Incrementer(numPlayersOptions)
        INCREMENTER_X = (Resources.SCREEN_WIDTH / 2) - (
            self.numPlayersIncrementer.incrementerWidth / 2
        )
        INCREMENTER_Y = 90
        self.numPlayersIncrementer.setPos(INCREMENTER_X, INCREMENTER_Y)

        # Score required to win incrementer
        scoreOptions = ["11", "21"]
        self.scoreOptionsIncrementer = Incrementer(scoreOptions)
        INCREMENTER_X = (Resources.SCREEN_WIDTH / 2) - (
            self.scoreOptionsIncrementer.incrementerWidth / 2
        )
        INCREMENTER_Y = 140
        self.scoreOptionsIncrementer.setPos(INCREMENTER_X, INCREMENTER_Y)

        # Flag to show textbox input error
        self.showError = False

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT, None, None, None, None, None, None

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if host button was clicked
                        if self.hostButton.isClicked(event.pos):
                            if len(self.textBox.text) == 0:
                                self.showError = True
                            else:
                                return (
                                    GameState.NEWGAME,
                                    True,
                                    self.textBox.text,
                                    None,
                                    int(self.numPlayersIncrementer.activeOption),
                                    self.gameModeIncrementer.activeOption,
                                    self.scoreOptionsIncrementer.activeOption,
                                )

                        # Check if back button was clicked
                        if self.mainMenuButton.isClicked(event.pos):
                            return GameState.TITLE, False, None, None, None, None, None

                        # Handle input for incrementers
                        self.gameModeIncrementer.handleInput(event.pos)
                        self.numPlayersIncrementer.handleInput(event.pos)
                        self.scoreOptionsIncrementer.handleInput(event.pos)

                # Proceed to game if enter is pressed in the textbox
                isInputEntered = self.textBox.handle_event(event)
                if isInputEntered and len(self.textBox.text) != 0:
                    return (
                        GameState.NEWGAME,
                        True,
                        self.textBox.text,
                        None,
                        int(self.numPlayersIncrementer.activeOption),
                        self.gameModeIncrementer.activeOption,
                        int(self.scoreOptionsIncrementer.activeOptions),
                    )
                elif isInputEntered and len(self.textBox.text) == 0:
                    self.showError = True

                # Check if mouse is hovering over buttons
                self.isMouseHoveringOverButtons()

            # Draw everything to the screen
            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        # Draw buttons and stuff
        self.hostButton.draw(self.screen)
        self.mainMenuButton.draw(self.screen)
        self.textBox.draw(self.screen)
        self.gameModeIncrementer.draw(self.screen)
        self.numPlayersIncrementer.draw(self.screen)
        self.scoreOptionsIncrementer.draw(self.screen)
        self.displayTitle()

        if self.showError:
            self.displayInputError()

        pygame.display.flip()

    def displayTitle(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "HOST"
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)

        self.screen.blit(text, ((Resources.SCREEN_WIDTH / 2) - (textWidth / 2), 0))

    def displayInputError(self):

        # Draw text to screen
        textColor = (255, 0, 0)
        text = "*Game name must not be blank"
        text = Resources.FONT_FIFTEEN.render(text, 1, textColor)

        self.screen.blit(
            text, ((Resources.SCREEN_WIDTH / 2) - 100, self.textBox.y - 20)
        )
Example #3
0
class OptionsScreen(Screen):
    def __init__(self):

        # Call Screen constructor
        Screen.__init__(self)

        # Video settings button
        self.videoSettingsButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 25,
            (255, 255, 255),
            (0, 0, 0),
            "VIDEO SETTINGS",
        )

        # Back button
        self.mainMenuButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 30,
            (255, 255, 255),
            (0, 0, 0),
            "MAIN MENU",
        )

        # List of buttons
        self.buttonList.append(self.videoSettingsButton)
        self.buttonList.append(self.mainMenuButton)

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if video settings button was clicked
                        if self.videoSettingsButton.isClicked(event.pos):
                            return GameState.VIDEO_SETTINGS

                        # Check if back button was clicked
                        if self.mainMenuButton.isClicked(event.pos):
                            return GameState.TITLE

                # Check if mouse is hovering over buttons
                self.isMouseHoveringOverButtons()

            # Draw everything to the screen
            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        # Draw buttons and stuff
        self.videoSettingsButton.draw(self.screen)
        self.mainMenuButton.draw(self.screen)
        self.displayTitle()

        pygame.display.flip()

    def displayTitle(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "OPTIONS"
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)
        self.screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - 100,
            ),
        )
Example #4
0
class GameScreen(Screen):
    def __init__(
        self,
        username,
        isHost,
        gameName=None,
        gameKey=None,
        maxPlayers=None,
        gameMode=None,
        winningScore=None,
    ):
        Screen.__init__(self)

        # Set up connection to server
        self.n = Network()
        self.n.connect()

        # Tell the server to create/join the game and return player id
        if isHost:
            self.player = self.n.getPlayer(
                f"host/{gameName}/{maxPlayers}/{gameMode}/{winningScore}")
        else:
            self.player = self.n.getPlayer(f"join/{gameKey}")

        # Get the game from the server
        self.game = self.n.send("get")

        # Check if the game could be received from the server
        connectionAttempts = 0
        MAX_ATTMEPTS = 10000
        while self.game is None and connectionAttempts < MAX_ATTMEPTS:
            self.game = self.n.send("get")
            connectionAttempts += 1

        # Only initialize game screen objects if server connectino was succesful
        if self.game is not None:

            # Set player username
            self.game.players[self.player].username = username
            self.n.send("username: "******"S"

            self.scoreButton = Button(
                SCORE_BUTTON_WIDTH,
                SCORE_BUTTON_HEIGHT,
                SCORE_BUTTON_X,
                SCORE_BUTTON_Y,
                SCORE_BUTTON_COLOR,
                SCORE_BUTTON_TEXT_COLOR,
                SCORE_BUTTON_TEXT,
            )

            # Ten and Under Button
            TEN_AND_UNDER_BUTTON_WIDTH = 100
            TEN_AND_UNDER_BUTTON_HEIGHT = 100
            TEN_AND_UNDER_BUTTON_PADDING = 10
            TEN_AND_UNDER_BUTTON_X = (self.screen.get_width() /
                                      2) - (TEN_AND_UNDER_BUTTON_WIDTH / 2)
            TEN_AND_UNDER_BUTTON_Y = (
                (self.screen.get_height() / 2) -
                (TEN_AND_UNDER_BUTTON_HEIGHT / 2)) - (
                    TEN_AND_UNDER_BUTTON_HEIGHT + TEN_AND_UNDER_BUTTON_PADDING)
            TEN_AND_UNDER_BUTTON_COLOR = (255, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT_COLOR = (0, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT = "TEN\nAND\nUNDER"

            self.tenAndUnderButton = Button(
                TEN_AND_UNDER_BUTTON_WIDTH,
                TEN_AND_UNDER_BUTTON_HEIGHT,
                TEN_AND_UNDER_BUTTON_X,
                TEN_AND_UNDER_BUTTON_Y,
                TEN_AND_UNDER_BUTTON_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT,
            )

            # List of all the buttons
            self.buttonList.append(self.scoreButton)
            self.buttonList.append(self.tenAndUnderButton)

            # Score screen
            self.scoreScreen = ScoreScreen()

            # Win Screen
            self.winScreen = WinScreen()

            self.showGameScreen = False
            self.showScoreScreen = False
            self.showWinScreen = False
            self.gameReady = False

    def run(self):

        # Check if the connection to the server was successful
        if self.game is None:
            return GameState.SERVER_ERROR

        # Game loop
        while True:
            self.clock.tick(60)

            # Attempt to get the game from the server
            try:

                self.game = self.n.send("get")

                # If a player left the game (numPlayers decreased),
                # disconnect from server and return to the title screen
                if self.game.numPlayers != len(self.game.players):
                    self.n.disconnect()
                    return GameState.DISCONNECT

                # Check to see if game is ready to start
                if (self.game.numPlayers >= self.game.maxPlayers
                        and self.showScoreScreen == False
                        and self.showWinScreen == False):
                    self.gameReady = True
                    self.showGameScreen = True

                # Update main pile
                self.main_pile = get_main_pile(self.game.mainPile)

                # Set trump image
                if self.game.trump is not None and self.trump_image != self.game.trump:
                    self.trump_image = Trump(self.game.trump)
                else:
                    self.trump_image = None

                # Update hand
                if self.game.players[self.player].ready:
                    self.test_hand = get_hand(
                        self.game.players[self.player].playerHand)
                    self.n.send("not ready")

            except:
                print("Couldn't get game")

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP and self.gameReady:
                    if event.button == 1:  # the right mouse button
                        for card in self.test_hand:

                            # Only play card if it is this player's turn
                            if (card.rect.collidepoint(event.pos) and
                                    self.game.players[self.player].playerTurn
                                    and
                                    self.game.players[self.player].playerBid
                                    is not None
                                    and self.game.didPlayersBid() == True
                                    and card.isPlayable(
                                        self.game,
                                        self.test_hand.hasCurrentSuit(
                                            self.game.currentSuit),
                                    )):

                                # Set small delay to ensure that the card played does
                                # not get counted as part of the previous trick
                                # TODO: This is hacky, look into another way to fix this
                                pygame.time.delay(200)

                                # Remove the card from the player's hand to be played in the main pile
                                self.test_hand.remove(card)

                                # Set not ready
                                if len(self.test_hand) == 0:
                                    self.n.send("not ready")

                                # Send card to server
                                self.n.send("card: " + str(card.value) + " " +
                                            card.suit)

                        # Check if a bid button was clicked
                        if (self.game.players[self.player].playerBidTurn
                                and self.game.biddingStage):
                            for button in self.bid_screen.buttonList:
                                if button.isClicked(event.pos):
                                    self.n.send("bid: " + button.text)

                        # Check if score button was clicked
                        if (self.scoreButton.isClicked(event.pos)
                                and self.showGameScreen == True
                                and self.showWinScreen == False):
                            self.showGameScreen = False
                            self.showScoreScreen = True
                        elif (self.scoreButton.isClicked(event.pos)
                              and self.showScoreScreen == True
                              and self.showWinScreen == False):
                            self.showGameScreen = True
                            self.showScoreScreen = False

                        # Check if ten and under button was clicked
                        if self.hasTenAndUnder():
                            if (self.tenAndUnderButton.isClicked(event.pos)
                                    and self.showGameScreen == True):

                                # Alert the server that player is turning in ten and under
                                self.n.send("tenAndUnder")

                                # Update the game for the client
                                self.game = self.n.send("get")

                                # Get the new player hand from the updated game
                                self.test_hand = get_hand(
                                    self.game.players[self.player].playerHand)

                        # Check if buttons on win screen were clicked and handle input
                        if self.showWinScreen == True:
                            self.winScreen.handleInput(event.pos)

                # Check if mouse is hovering over button
                self.isMouseHoveringOverButtons()

            # Check if the game is over
            if self.game.isGameOver() and self.showWinScreen == False:
                self.showWinScreen = True
                self.showGameScreen = False
                self.showScoreScreen = False

            self.draw()

    def draw(self):

        if self.showGameScreen == True and self.showWinScreen == False:

            # Blit the background of the screen
            self.screen.blit(self.background, (0, 0))

            # Draw the hand on the screen
            self.test_hand.draw(self.screen)

            # Draw the main pile to the screen
            self.main_pile.draw(self.screen)

            # Draw the trump image to the screen
            if self.trump_image is not None:
                self.trump_image.draw(self.screen)

            # Draw player turn indicator image when it is player's turn
            if self.game.players[self.player].playerTurn:
                self.playerTurnIndicator.draw(self.screen)

            # Draw the bid screen
            if self.game.biddingStage and self.game.players[
                    self.player].playerBidTurn:
                self.bid_screen.draw(self.screen, self.game)

            # Draw score button
            self.scoreButton.draw(self.screen)

            # Draw the ten and under button
            if self.hasTenAndUnder():
                self.tenAndUnderButton.draw(self.screen)

            # Draw the username list
            self.username_list.draw(self.game, self.screen)

            pygame.display.flip()

            # Time delay to keep the last card on screen in trick
            if (self.game.mainPile.size() % len(self.game.players) == 0
                    and self.game.mainPile.size() != 0
                    and not self.game.isPlayersReady()):
                pygame.time.delay(2000)
                self.n.send("ready")

        if self.showScoreScreen == True and self.showWinScreen == False:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            # Draw score button
            self.scoreButton.draw(self.screen)

            self.scoreScreen.draw(self.game, self.screen)

            pygame.display.flip()

        if self.showWinScreen == True:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            # Draw the win screen
            self.winScreen.draw(self.game, self.screen)

            pygame.display.flip()

        if self.gameReady == False:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            self.displayWaitMessage()

            pygame.display.flip()

    def hasTenAndUnder(self):
        """Player has a ten and under that meets the criteria to turn in"""

        return (
            # Players are actively bidding
            self.game.isBiddingStage() and
            # Player's turn to bid
            self.game.players[self.player].playerBidTurn and
            # Player has a ten and under
            self.test_hand.hasTenAndUnder() and (
                # Player is not the last bidder
                (self.game.getNumberOfBids() < (self.game.numPlayers - 1)) or
                # Player is the last bidder and someone has bid already
                ((self.game.getNumberOfBids() == (self.game.numPlayers - 1))
                 and (self.game.getHighestBid() > 0))))

    def displayWaitMessage(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "Waiting For More Players..."
        textWidth, textHeight = Resources.FONT_THIRTY.size(text)
        text = Resources.FONT_THIRTY.render(text, 1, textColor)

        self.screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - (textHeight / 2),
            ),
        )
Example #5
0
class TitleScreen(Screen):
    def __init__(self):

        # Call screen constructor
        Screen.__init__(self)

        # Host button
        self.hostButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 80,
            (255, 255, 255),
            (0, 0, 0),
            "HOST",
        )

        # Join button
        self.joinButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 25,
            (255, 255, 255),
            (0, 0, 0),
            "JOIN",
        )

        # Options button
        self.optionsButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 30,
            (255, 255, 255),
            (0, 0, 0),
            "OPTIONS",
        )

        # Quit button
        self.quitButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) + 85,
            (255, 255, 255),
            (0, 0, 0),
            "QUIT",
        )

        # Text botx to enter username
        self.textBox = TextBox(
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 135,
            200,
            50,
        )

        # Add buttons to button list
        self.buttonList.append(self.hostButton)
        self.buttonList.append(self.joinButton)
        self.buttonList.append(self.optionsButton)
        self.buttonList.append(self.quitButton)

        self.showError = False

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if host button was clicked
                        if self.hostButton.isClicked(event.pos):
                            if len(self.textBox.text) == 0:
                                self.showError = True
                            else:
                                return GameState.HOST, self.textBox.text

                        # Check if join button was clicked
                        if self.joinButton.isClicked(event.pos):
                            if len(self.textBox.text) == 0:
                                self.showError = True
                            else:
                                return GameState.JOIN, self.textBox.text

                        # Check if options button was clicked
                        if self.optionsButton.isClicked(event.pos):
                            return GameState.OPTIONS, None

                        # Check if quit button was clicked
                        if self.quitButton.isClicked(event.pos):
                            return GameState.QUIT, None

                # Check if buttons are being hovered over
                self.isMouseHoveringOverButtons()

                # Proceed to game if enter is pressed in the textbox
                isInputEntered = self.textBox.handle_event(event)

            # Draw everything to the screen
            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        # Draw buttons and stuff
        self.hostButton.draw(self.screen)
        self.joinButton.draw(self.screen)
        self.optionsButton.draw(self.screen)
        self.quitButton.draw(self.screen)
        self.textBox.draw(self.screen)

        # Show error if username field is left blank
        if self.showError:
            self.displayInputError()

        pygame.display.flip()

    def displayInputError(self):

        # Draw text to screen
        textColor = (255, 0, 0)
        text = "*Username must not be blank"
        text = Resources.FONT_FIFTEEN.render(text, 1, textColor)
        self.screen.blit(
            text,
            ((Resources.SCREEN_WIDTH / 2) - 100,
             (Resources.SCREEN_HEIGHT / 2) - 155),
        )
class DisconnectScreen(Screen):
    def __init__(self):

        # Call screen constructor
        Screen.__init__(self)

        # Back button
        self.mainMenuButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 25,
            (255, 255, 255),
            (0, 0, 0),
            "MAIN MENU",
        )

        # Add buttons to button list
        self.buttonList.append(self.mainMenuButton)

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if back button was clicked
                        if self.mainMenuButton.isClicked(event.pos):
                            return GameState.TITLE

                # Check if mouse is hover over buttons
                self.isMouseHoveringOverButtons()

            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        # Draw buttons and stuff
        self.mainMenuButton.draw(self.screen)
        self.displayError()

        pygame.display.flip()

    def displayError(self):

        # Draw text to screen
        textColor = (255, 0, 0)
        text = "Game ended unexpectedy due to player disconnect"
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)
        self.screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - 100,
            ),
        )
Example #7
0
class WinScreen:
    def __init__(self):

        # Screen width and height
        self.w, self.h = pygame.display.get_surface().get_size()

        # Button list
        self.buttonList = []

        # Back button
        self.mainMenuButton = Button(
            200,
            50,
            (Resources.SCREEN_WIDTH / 2) - 100,
            (Resources.SCREEN_HEIGHT / 2) - 25,
            (255, 255, 255),
            (0, 0, 0),
            "MAIN MENU",
        )

        # Add buttons to button list
        self.buttonList.append(self.mainMenuButton)

    def handleInput(self, eventPos):
        if self.mainMenuButton.isClicked(eventPos):
            # TODO: Send player back to the main menu
            pass

    def draw(self, game, screen):

        # Display the win message
        self.displayWinMessage(game, screen)

        # Draw the main menu button
        self.mainMenuButton.draw(screen)

        # Check if mouse is hovering over buttons
        self.mainMenuButton.isHovering(pygame.mouse.get_pos())

    def displayWinMessage(self, game, screen):

        # Get the list of usernames for who won
        winnerList = game.getWinners()

        # Draw text to screen
        textColor = (0, 0, 0)

        # Create text to display to screen
        text = "Winner(s): "
        for winner in winnerList:
            text += winner

        # Get the size of the text
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)

        # Blit to the screen
        screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - 100,
            ),
        )