Beispiel #1
0
    def __init__(self,
                 x,
                 y,
                 width,
                 height,
                 clear_colour,
                 autoplay=False,
                 view_all_cards=False,
                 terminal=False):
        # TODO: Reduce the amount of update_table call
        self.update_table = Signal()
        self.x = x
        self.y = y
        self.width = width
        self.height = height

        self.table_font = pygame.font.SysFont("None", 25)
        self.player_font = pygame.font.SysFont("None", 25)

        # For gameplay
        self.game_state = GameState.DEALING
        self.reshuffling_players = []
        self.current_round = 0
        self.passes = 0
        self.current_player = 0
        self.first_player = False  # This is for bidding purposes
        self.players = []
        self.players_playzone = []
        # Table status will be made known to the player by reference
        self.table_status = {
            'played cards': [0, 0, 0, 0],
            'leading player': 0,
            'trump suit': 1,
            'trump broken': False,
            'round history': [],
            'bid': 0,
            'partner': 0,
            'partner reveal': False,
            'defender': {
                'target': 0,
                'wins': 0
            },
            'attacker': {
                'target': 0,
                'wins': 0
            }
        }

        # Prepare the surfaces for displaying
        self.background = pygame.Surface((self.width, self.height))
        self.background.fill(clear_colour)
        self.background = self.background.convert()

        # TODO: Update the drawing of the table?
        # Prepare the card with dimensions
        w_deck = min(self.height, self.width) * 0.18
        l_deck = min(self.width, self.height) * 0.7
        # This is not a deck as it will never be drawn
        self.discard_deck = cards.prepare_playing_cards(
            int(w_deck * 0.6), int(w_deck * 0.6 * 97 / 71))
        game_margins = 5

        # Players' deck positioning
        playerx = ((self.width - l_deck) // 2, game_margins,
                   (self.width - l_deck) // 2,
                   self.width - w_deck - game_margins)
        playery = (self.height - w_deck - game_margins,
                   (self.height - l_deck) // 2, game_margins,
                   (self.height - l_deck) // 2)
        h_spacing = 20
        v_spacing = 25

        # Middle playfield for announcer and player playing deck positioning
        playfield_margins = 5
        margins_with_w_deck = w_deck + playfield_margins + game_margins
        playfield_x = margins_with_w_deck
        playfield_y = margins_with_w_deck
        playfield_width = self.width - margins_with_w_deck * 2
        playfield_height = self.height - margins_with_w_deck * 2

        playdeckx = (playfield_x + (playfield_width - w_deck) / 2, playfield_x,
                     playfield_x + (playfield_width - w_deck) / 2,
                     playfield_x + playfield_width - w_deck)
        playdecky = (playfield_y + playfield_height - w_deck,
                     playfield_y + (playfield_height - w_deck) / 2,
                     playfield_y,
                     playfield_y + (playfield_height - w_deck) / 2)

        # Player stats positioning
        stats_width = 100
        self.stats_height = 100
        stats_spacing = 10
        self.player_stats_x = (playdeckx[0] - stats_width - stats_spacing,
                               playdeckx[1],
                               playdeckx[2] + w_deck + stats_spacing,
                               playdeckx[3])
        self.player_stats_y = (playdecky[0] + w_deck - self.stats_height,
                               playdecky[1] - self.stats_height -
                               stats_spacing, playdecky[2],
                               playdecky[3] - w_deck - stats_spacing)

        self.player_stats = [[], [], [], []]

        # TODO: change surface to use colorkey, maybe, if the performance is tanked
        # Prepare all the player surfaces
        for i in range(NUM_OF_PLAYERS):
            vert = i % 2 == 1
            spacing = h_spacing
            if vert:
                spacing = v_spacing

            reveal_mode = cards.DeckReveal.HIDE_ALL
            if i == 0 or view_all_cards:
                reveal_mode = cards.DeckReveal.SHOW_ALL

            if i == 0:
                player_class = players.MainPlayer
                if terminal:
                    player_class = players.Player
                self.players.append(
                    player_class(playerx[i],
                                 playery[i],
                                 l_deck,
                                 w_deck,
                                 spacing,
                                 vert_orientation=vert,
                                 deck_reveal=reveal_mode))
            else:
                self.players.append(
                    players.Player(playerx[i],
                                   playery[i],
                                   l_deck,
                                   w_deck,
                                   spacing,
                                   vert_orientation=vert,
                                   deck_reveal=reveal_mode,
                                   flip=(i == 1 or i == 2),
                                   draw_from_last=(i == 2 or i == 3)))

            self.players[i].connect_to_table(self.table_status)
            if i > 0:
                self.players[i].add_ai(ai.VivianAI(self.table_status))

            self.players_playzone.append(
                cards.Deck(playdeckx[i], playdecky[i], w_deck, w_deck, 0))
            for j in range(3):
                surf = pygame.Surface((stats_width, self.stats_height / 3),
                                      pygame.SRCALPHA)
                rendered_text = self.player_font.render(
                    "Player {0:d}".format(i), True,
                    (255, 0, 255)).convert_alpha()
                self.center_text_on_surface(
                    surf, rendered_text,
                    (255, 255, 255, 255 * VIEW_TRANSPARENT))
                self.player_stats[i].append(surf)

        if autoplay:
            self.players[0].add_ai(ai.VivianAI(self.table_status))

        # Announcer positioning and surface creation
        announcer_margins = 5
        announcer_spacing = announcer_margins + w_deck
        self.announcer_x = playfield_x + announcer_spacing
        self.announcer_y = playfield_y + announcer_spacing
        self.announcer_width = playfield_width - 2 * announcer_spacing
        self.announcer_height = playfield_height - 2 * announcer_spacing
        self.announcer_line = []
        for i in range(3):
            surf = pygame.Surface(
                (self.announcer_width, self.announcer_height / 3),
                pygame.SRCALPHA)
            self.announcer_line.append(surf)

        self.update_all_players(role=True, wins=True, clear_wins=True)

        self.write_message("Press P to play!")

        self.ongoing = False
        self.require_player_input = False

        self.terminal_play = terminal
        self.calling_panel = UI.CallPanel(playdeckx[0] + w_deck + 5,
                                          playdecky[0] + w_deck - 100, 220,
                                          100)
        self.calling_panel.parent = self
        self.calling_panel.visible = False
        self.parent = None

        self.calling_panel.confirm_output.connect(self.emit_call)

        self.yes_button = UI.Button(playdeckx[0] + w_deck + 5,
                                    playdecky[0],
                                    50,
                                    25,
                                    text='yes')
        self.yes_button.visible = False
        self.yes_button.clicked.connect(lambda **z: pygame.event.post(
            pygame.event.Event(CALL_EVENT, call=True)))

        self.no_button = UI.Button(playdeckx[0] + w_deck + 5,
                                   playdecky[0] + 25 + 25,
                                   50,
                                   25,
                                   text='no')
        self.no_button.clicked.connect(lambda **z: pygame.event.post(
            pygame.event.Event(CALL_EVENT, call=False)))
        self.no_button.visible = False

        self.UI_elements = [
            self.calling_panel, self.yes_button, self.no_button
        ]