示例#1
0
    def _init_slots(self):
        """
        Initialize the profile slots
        :return:
        """
        # margin between two buttons
        margin = 30
        width = (self.width / self._limit) - margin
        height = self.height - 40
        for i in range(self._limit):
            # draw the profile button for each profile
            x = self.x + ((width + margin) * i) + (margin / 2)
            y = self.y + 20

            btn = RectButton(
                x, y, width, height, color.STANDARDBTN, 0,
                Text(pygame.font.SysFont('Agency FB', 25), "Empty",
                     color.BLACK))
            remove_btn = RectButton(
                btn.x, (btn.y + self.height - 80) + 10, btn.width, 30,
                color.YELLOW, 0,
                Text(pygame.font.SysFont('Agency FB', 16), "Remove",
                     color.BLACK))
            s = RectLabel(
                btn.x, (btn.y + self.height - 120), btn.width, 30,
                color.STANDARDBTN, 0,
                Text(pygame.font.SysFont('Agency FB', 16), "dadsad",
                     color.BLACK))

            s.set_transparent_background(True)
            self._stats_list.append(s)
            self._remove_btn_list.append(remove_btn)
            self._btn_list.append(btn)
            self._profile_list.add(btn)
示例#2
0
 def init_error_message(self, msg):
     label_width = 400
     label_left = (pygame.display.get_surface().get_size()[0] / 2) - (label_width / 2)
     label_top = (pygame.display.get_surface().get_size()[1] / 6) * 2
     error_msg_label = RectLabel(label_left, label_top, label_width, label_width, (255, 255, 255),
                                 txt_obj=(Text(pygame.font.SysFont('Agency FB', 24), msg, Color.RED)))
     error_msg_label.set_transparent_background(True)
     self.error_msg = error_msg_label
示例#3
0
 def not_enough_players_ready_prompt(self):
     """Prompt to the host that there are not enough players to join the game."""
     label_width = 400
     label_height = 30
     label_left = 1050 - 100
     label_top = (pygame.display.get_surface().get_size()[1] -
                  50) - (label_height / 2)
     message = f"Not all players are ready!"
     prompt_label = RectLabel(label_left,
                              label_top,
                              label_width,
                              label_height,
                              Color.WHITE,
                              txt_obj=Text(
                                  pygame.font.SysFont('Agency FB', 24),
                                  message))
     prompt_label.set_transparent_background(True)
     self.players_not_ready_prompt = prompt_label
示例#4
0
class ChatBox:
    def __init__(self, current_player: PlayerModel):
        self.chat_history = GameStateModel.instance().chat_history
        self.group = pg.sprite.Group()
        self._init_chatbox()
        self.to_be_renamed = []
        self.current_player = current_player

    def _init_chatbox(self):
        """
        Chat box dimensions
        """
        self.offset = 5
        chat_box_x = 0
        chat_box_y = int(0.6 * SCREEN_RESOLUTION[1])
        chat_box_w = 250
        chat_box_h = SCREEN_RESOLUTION[1] - chat_box_y
        """
        Chat history dimensions
        """
        chat_hist_x = chat_box_x + self.offset
        chat_hist_y = chat_box_y + self.offset
        chat_hist_w = chat_box_w - 2 * self.offset
        chat_hist_h = 0.85 * chat_box_h
        """
        Chat textbox dimensions
        """
        chat_textbox_x = chat_box_x + self.offset
        chat_textbox_y = chat_hist_y + chat_hist_h + self.offset
        chat_textbox_w = chat_hist_w
        chat_textbox_h = chat_box_h - chat_hist_h - 15

        self.chat_box = RectLabel(chat_box_x,
                                  chat_box_y,
                                  chat_box_w,
                                  chat_box_h,
                                  background=Color.BLACK)
        self.chat_history_bg = RectLabel(chat_hist_x,
                                         chat_hist_y,
                                         chat_hist_w,
                                         chat_hist_h,
                                         background=Color.WHITE)
        self.chat_history_bg.set_transparent_background(True)
        self.chat_textbox = InputBox(x=chat_textbox_x,
                                     y=chat_textbox_y,
                                     w=chat_textbox_w,
                                     h=chat_textbox_h,
                                     fsize=20)
        self.group.add(
            [self.chat_box, self.chat_history_bg, self.chat_textbox])

    def update(self, event_queue: EventQueue):

        self.chat_textbox.update(event_queue)
        message = self.chat_textbox.message
        if message:
            chat_event = ChatEvent(self.chat_textbox.message,
                                   self.current_player.nickname)

            if self.current_player.ip == GameStateModel.instance().host.ip:
                Networking.get_instance().send_to_all_client(chat_event)
            else:
                Networking.get_instance().send_to_server(chat_event)

            self.chat_textbox.message = ''

        if GameStateModel.instance():
            self.chat_history = GameStateModel.instance().chat_history
            self._init_message_box()
            self.chat_textbox.rect.w = self.chat_history_bg.rect.w

    def draw(self, screen):
        for message in self.to_be_renamed:
            self.chat_history_bg.image.blit(message.image, message.rect)

        screen.blit(self.chat_history_bg.image, self.chat_history_bg.rect)
        self.chat_textbox.draw(screen)

    def _init_message_box(self):
        message_box_x = self.offset
        message_box_w = self.chat_history_bg.rect.w - 2 * self.offset
        message_box_h = TEXT_BOX_FONT_SIZE + 2
        chat_hist_bottom = self.chat_history_bg.rect.h

        max_messages = math.floor(self.chat_history_bg.rect.h / message_box_h)
        count = 0
        self.to_be_renamed = []

        for old_message in reversed(self.chat_history):
            if count < max_messages:
                message_box_y = chat_hist_bottom - (message_box_h *
                                                    (count + 1))
                old_message_box = RectLabel(
                    message_box_x,
                    message_box_y,
                    message_box_w,
                    message_box_h,
                    background=Color.WHITE,
                    txt_pos=Text.Position.LEFT,
                    txt_obj=Text(font=pg.font.SysFont("Agency FB",
                                                      TEXT_BOX_FONT_SIZE - 2),
                                 text=f"{old_message[1]}: {old_message[0]}"))

                self.to_be_renamed.append(old_message_box)
                count += 1
            else:
                break

    @property
    def box(self):
        return self.chat_textbox.rect