Esempio n. 1
0
class CluelessGamePlayer(ConnectionListener):
    """ This class begins the gameplay by drawing the board """

    def __init__(self, host="198.61.134.123", port=8080):

        self.display = None
        self.roomFont = None

        self.scratchPad = None
        self.entries = None
        self.playerArea = None
        self.cardArea = None
        self.gameBoard = None

        self.turn = -1
        self.youAre = 0
        self.myCards = []
        self.numplayers = 0

        self.game_process = None
        self.running = False
        self.need_initializing = False
        self.gameid = None
        self.Connect((host, int(port)))
        self.card_deck = CardDeck()
        self.suggestionDisproved = []

        self.need_to_notify = False
        self.newPosition = 0

    def Network(self, data):
        # print 'network data:', data
        pass

    def set_turn(self, turn):
        self.turn = turn
        # self.turn = 0

    def set_player_id(self, id):
        self.youAre = id
        # self.youAre = 0

    def set_player_cards(self, cards):
        self.myCards = cards

    def Network_hello(self, data):
        print "SERVER SAYS HELLO!"

    def Network_close(self, data):
        sys.exit()

    def set_total_num_players(self, numplayers):
        self.numplayers = numplayers

    def Network_startgame(self, data):
        player_character = data["character"]
        player_cards = data["cards"]

        player_id = data["youAre"]
        turn = data["turn"]
        numplayers = data["numplayers"]

        self.set_turn(turn)
        self.set_player_id(player_id)
        self.set_total_num_players(numplayers)
        self.set_player_cards(player_cards)

        success, cards = create_message("start")

        print "player_cards: ", player_cards
        print "start success?: ", success
        self.running = True
        self.initialize_game_board()
        if success:
            connection.Send({"action": "success", "ID": self.gameid})
        else:
            connection.Send({"action": "fail", "ID": self.gameid})

    def Network_updateTurn(self, data):
        """ This function is to update the player's server turn index """
        self.turn = data["turn"]

    def Network_endgame(self, data):
        create_message("end")
        sys.exit()

    def Network_winner(self, data):
        create_message("win")
        sys.exit()

    def Network_loser(self, data):
        create_message("lose")
        sys.exit()

    def updateTurn(self, turn):
        self.turn = turn

    def create_board(self):
        ############## Game and Font Initialization ###############
        pygame.init()
        self.display = pygame.display.set_mode((900, 800), HWSURFACE | DOUBLEBUF)
        pygame.display.set_caption("Board")
        myfont = pygame.font.SysFont("Times New Roman", 50)
        self.roomFont = pygame.font.SysFont("Times New Roman", 15)
        self.roomFont.set_bold(True)
        self.display.fill(TAN)

    def create_scratchPad(self):
        ############## Scratch Pad Integration ################
        self.scratchPad = ScratchPad(self.display, [], [])
        self.entries = self.scratchPad.runScratchPad()

    def create_playerArea(self):
        ############## Player Area Integration ################
        self.playerArea = PlayerArea([], self.display, 600, 320)

    def create_cardArea(self):
        ############## Card Area Initialization ##############
        self.cardArea = cardArea(self.display)
        self.cardArea.placeCards(self.myCards)

    def create_gameBoard(self):
        ############## Create the GameBoard #####################
        self.gameBoard = GameBoard(self.myCards, [], 1)
        self.gameBoard.drawGameBoard(self.display, self.roomFont)

    def draw_border(self):
        ############### Draw Border ###################
        pygame.draw.rect(self.display, BLACK, (0, 0, 600, 500), 4)

    def execute(self):

        self.game_process = Process(target=self.run)
        self.game_process.start()

    def initialize_game_board(self):

        self.create_board()
        self.create_scratchPad()
        self.create_playerArea()
        self.create_cardArea()
        self.create_gameBoard()
        self.draw_border()

    def Network_updateTurn(self, data):
        turn = data["turn"]
        self.turn = turn

    def Network_movePlayer(self, data):
        spotArrayIndex = data["spotArrayIndex"]
        self.update_player_position(spotArrayIndex)

    def update_player_position(self, spotArrayIndex):
        print "Updating Player position!"
        currentCharacter = characterArray[self.turn % len(characterArray)]
        currentCharacter.moveCharacter(self.display, self.roomFont, spotArray[spotArrayIndex])
        ##### Update the player area
        previousPlayer = self.playerArea.players[self.turn % len(self.playerArea.players)]
        if self.turn % len(self.playerArea.players) == 2:
            previousPlayer.drawPlayerArrow(self.display, TAN, False)
        elif self.turn % len(self.playerArea.players) == 5:
            previousPlayer.drawPlayerArrow(self.display, TAN, True)

        self.turn = self.turn + 1 if self.turn < self.numplayers - 1 else 0

        currentPlayer = self.playerArea.players[self.turn % len(self.playerArea.players)]

        if self.turn % len(self.playerArea.players) > 2:
            previousPlayer.drawPlayerArrow(self.display, TAN, True)
            currentPlayer.drawPlayerArrow(self.display, currentPlayer.getColor(), True)
        else:
            previousPlayer.drawPlayerArrow(self.display, TAN, False)
            currentPlayer.drawPlayerArrow(self.display, currentPlayer.getColor(), False)

        currentCharacter.draw(self.display, self.roomFont)
        pygame.display.update()

    def notify_move(self, spotArrayIndex):
        connection.Send({"action": "playerMove", "spotArrayIndex": spotArrayIndex, "playerId": self.youAre})
        self.need_to_notify = False

    def notify_suggestion(self, cards, spotArrayIndex):
        print "Notifying new suggestion: ", cards
        print "WITH SPOT ARRAY INDEX: ", spotArrayIndex
        connection.Send(
            {"action": "newSuggestion", "cards": cards, "playerId": self.youAre, "spotArrayIndex": spotArrayIndex}
        )

    def notify_finalaccusation(self, cards):
        connection.Send({"action": "finalAccusation", "cards": cards, "playerId": self.youAre})

    def Network_newSuggestion(self, data):
        cards = data["cards"]
        card_names = cards.values()
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        create_message("accuse", card_name_text)

    def Network_forceDisproval(self, data):
        print "FORCING DISPROVAL WITH SUGGESTION CARDS: ", data["cards"]
        cards = data["cards"]
        card_names = cards.values()
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        success, disproved_cards = create_message("mustDisprove", card_name_text, self.myCards)
        print "success: ", success
        print "disproved cards: ", disproved_cards
        if disproved_cards:
            connection.Send({"action": "disproved", "cards": disproved_cards})
        else:
            connection.Send({"action": "disproved", "cards": False})

    def Network_isDisproved(self, data):
        card_names = data["cards"]
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        create_message("disprove", card_name_text)
        self.notify_move(self.newPosition)

    def Network_notDisproved(self, data):
        create_message("notdisproved")
        self.notify_move(self.newPosition)

    def run(self):

        if not self.running:
            return

        if self.need_to_notify:
            self.notify_move(self.newPosition)

        self.playerArea.players[self.turn].drawPlayerArrow(
            self.display, self.playerArea.players[self.turn].getColor(), False
        )
        self.playerArea.players[self.youAre].drawBox(self.display)

        for event in pygame.event.get():
            # if self.turn == self.youAre:
            # create_message("move")
            if event.type == pygame.MOUSEBUTTONUP and self.turn == self.youAre:

                # MOVE A PLAYER
                for i in range(len(spotArray)):
                    rect = pygame.Rect(spotArray[i].x, spotArray[i].y, 120, 100)
                    if rect.collidepoint(event.pos):
                        currentCharacter = characterArray[self.turn % len(characterArray)]
                        characterRect = pygame.Rect(
                            currentCharacter.currentArea.x, currentCharacter.currentArea.y, 120, 100
                        )
                        if (
                            isValidSecretPassage(spotArray[i], currentCharacter.currentArea) == False
                            and spotArray[i].isAdjacent(spotArray, currentCharacter.currentArea) == False
                            or spotArray[i].maxOccupancy - len(spotArray[i].currentOccupants) <= 0
                        ):
                            create_message("invalidMove")
                            break
                        else:
                            suggestionMade = False
                            # self.update_player_position(i)
                            if isinstance(spotArray[i], Room):
                                currentCharacter.draw(self.display, self.roomFont)
                                pygame.display.update()
                                success, cards = create_message("newSuggestion", spotArray[i].name)
                                print "SUCCESS: ", success
                                print "CARDS: ", cards
                                if success and cards:
                                    suggestionMade = True
                                    self.notify_suggestion(cards, i)
                                    self.need_to_notify = False
                                else:
                                    self.need_to_notify = True
                            else:
                                self.need_to_notify = True
                            self.newPosition = i

                ##### Checks for making the final accusation #####
                rect = pygame.Rect(770, 460, 130, 40)
                if rect.collidepoint(event.pos):
                    print "MAKING FINAL ACCUSATION"
                    success, cards = create_message("newSuggestion", "None")
                    if success and cards:
                        self.notify_finalaccusation(cards)

            # UPDATE SCRATCH PAD
            if event.type == pygame.MOUSEBUTTONUP:
                yVal = 0
                ##### Checks for scratch pad #####
                for i in range(len(self.entries)):
                    r = self.entries[i].getRect()
                    if r.collidepoint(event.pos):
                        if self.scratchPad.scratchColorsArray[i] == True:
                            pygame.draw.line(self.display, RED, (r.x, r.y + 10), (r.x + 120, r.y + 10), 3)
                            self.scratchPad.scratchColorsArray[i] = False
                        else:
                            self.scratchPad.redrawEntryArea(self.display, r)
                            self.scratchPad.blitText(self.entries[i].getName(), r, self.display)
                            self.scratchPad.scratchColorsArray[i] = True
                    yVal += 20

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        pygame.display.update()
Esempio n. 2
0

############## Game and Font Initialization ###############
pygame.init()
display = pygame.display.set_mode((900, 600), 0, 32)
pygame.display.set_caption('Board')
myfont = pygame.font.SysFont("Times New Roman", 50)
display.fill(WHITE)


############## Scratch Pad Integration ################

scratchColorsArray = []
allArray = []
scratchPad = ScratchPad(display, scratchColorsArray, allArray)
allArray = scratchPad.runScratchPad()

############## Internal Class Declarations ##############
class Character:
    def __init__(self, name, color, currentSpace):
        self.name = name
        self.color = color
        self.currentSpace = currentSpace
    def name(self):
        return self.name
    def color(self):
        return self.color
    def draw(self):
        pygame.draw.circle(display, self.color, (self.currentSpace.x + 60, self.currentSpace.y + 60), 15)
    def currentSpace(self):
        return self.currentSpace