示例#1
0
    def ask_tricks(self, n):
        """
        Ask the user for the number of tricks.
        :param n: the maximum number of tricks
        """
        # Create the container with the fade in effect.
        container = Widget((self.screen.get_width()/2-200, 100), (400, self.screen.get_height()-120), 40)
        container.opacity = 0
        container.add_action(actions.FadeInAction(0.5))
        self._ask_tricks_widget = container
        self._background_widget.add_widget(container)

        # Create the question text.
        text_w = special_widgets.warning_widget((0, 0), (400, 60), "How many tricks do you make?", self._font,
                                                close_on_click=False)
        text_w.visible = True
        container.add_widget(text_w)

        # Create the numbers.
        class ChooseHandler(object):
            def __init__(self, view, nn):
                self._view = view
                self._n = nn
            def __call__(self, x, y):
                self._view._handle_say_tricks(self._n)
        for i in xrange(n+1):
            size = (50, 50)
            pos = ((i % 6) * (size[0]+20), 80 + (i/6) * (size[1] + 20))
            w = special_widgets.warning_widget(pos, size, str(i), self._font, close_on_click=False)
            w.visible = True
            w.handle_clicked = ChooseHandler(self, i)
            container.add_widget(w)
示例#2
0
    def _create_widgets(self):
        """
        Create the widgets and return the one that contains them all.
        :return: the background widget
        """
        # Create the background widget.
        bg = self._rm.get_image(BACKGROUND_IMAGE, self.screen.get_size())
        bg_widget = ImageWidget((0, 0), self.screen.get_size(), -1, bg)

        # Create the container for the input widgets.
        x = (self.screen.get_width() - INPUT_WIDTH - INPUT_PADDING[1] - INPUT_PADDING[3]) / 2
        y = self.screen.get_height() / 2
        w = INPUT_WIDTH + INPUT_PADDING[1] + INPUT_PADDING[3]
        h = self.screen.get_height() - y
        input_container = Widget((x, y), (w, h), 0)
        bg_widget.add_widget(input_container)

        # Create the input widgets.
        username_input = TextInput(
            (0, 0),
            INPUT_WIDTH,
            0,
            self._font,
            padding=INPUT_PADDING,
            color=INPUT_FORE_COLOR,
            fill=INPUT_FILL_COLOR,
            default_text="username",
            default_font=self._default_font,
        )
        host_input = copy.copy(username_input)
        host_input.default_text = "host"
        host_input.position = (0, INPUT_OFFSET)
        port_input = copy.copy(username_input)
        port_input.default_text = "port"
        port_input.position = (0, 2 * INPUT_OFFSET)
        input_container.add_widget(username_input)
        input_container.add_widget(host_input)
        input_container.add_widget(port_input)
        self._text_inputs = {"username": username_input, "host": host_input, "port": port_input}

        # Create the button widget.
        btn = special_widgets.simple_button((0, 3 * INPUT_OFFSET), (w, 100), "Login", self._font)

        def btn_clicked(x, y):
            self._ev_manager.post(events.LoginRequestedEvent())

        btn.handle_clicked = btn_clicked
        input_container.add_widget(btn)

        # Create the connection failed warning.
        self._connection_failed_warning = special_widgets.warning_widget(
            None, (400, 100), "Connection failed", self._font, screen_size=self.screen.get_size()
        )
        bg_widget.add_widget(self._connection_failed_warning)
        self._username_taken_warning = special_widgets.warning_widget(
            None, (400, 100), "Username already taken", self._font, screen_size=self.screen.get_size()
        )
        bg_widget.add_widget(self._username_taken_warning)

        return bg_widget
示例#3
0
    def _create_widgets(self):
        """
        Create the widgets and return the one that contains them all.
        :return: the background widget
        """
        # Create the background widget.
        bg = self._rm.get_image(BACKGROUND_IMAGE, self.screen.get_size())
        bg_widget = ImageWidget((0, 0), self.screen.get_size(), -1, bg)

        # Create the container for the input widgets.
        x = (self.screen.get_width() - INPUT_WIDTH - INPUT_PADDING[1] - INPUT_PADDING[3]) / 2
        y = self.screen.get_height() / 2
        w = INPUT_WIDTH + INPUT_PADDING[1] + INPUT_PADDING[3]
        h = self.screen.get_height() - y
        input_container = Widget((x, y), (w, h), 0)
        bg_widget.add_widget(input_container)

        # Create the input widgets.
        username_input = TextInput((0, 0), INPUT_WIDTH, 0, self._font, padding=INPUT_PADDING, color=INPUT_FORE_COLOR,
                                   fill=INPUT_FILL_COLOR, default_text="username",
                                   default_font=self._default_font)
        host_input = copy.copy(username_input)
        host_input.default_text = "host"
        host_input.position = (0, INPUT_OFFSET)
        port_input = copy.copy(username_input)
        port_input.default_text = "port"
        port_input.position = (0, 2*INPUT_OFFSET)
        input_container.add_widget(username_input)
        input_container.add_widget(host_input)
        input_container.add_widget(port_input)
        self._text_inputs = {"username": username_input, "host": host_input, "port": port_input}

        # Create the button widget.
        btn = special_widgets.simple_button((0, 3*INPUT_OFFSET), (w, 100), "Login", self._font)

        def btn_clicked(x, y):
            self._ev_manager.post(events.LoginRequestedEvent())
        btn.handle_clicked = btn_clicked
        input_container.add_widget(btn)

        # Create the connection failed warning.
        self._connection_failed_warning = special_widgets.warning_widget(None, (400, 100), "Connection failed",
                                                                         self._font, screen_size=self.screen.get_size())
        bg_widget.add_widget(self._connection_failed_warning)
        self._username_taken_warning = special_widgets.warning_widget(None, (400, 100), "Username already taken",
                                                                      self._font, screen_size=self.screen.get_size())
        bg_widget.add_widget(self._username_taken_warning)

        return bg_widget
示例#4
0
    def _show_player_order(self, player_order):
        """
        Show the player order.
        :param player_order: the player order
        """
        width = 100
        height = 50
        margin_x = 10
        margin_y = 10

        cx = self._screen.get_width() / 2
        cy = self._screen.get_height() / 2
        rx = self._screen.get_width() / 2 - width/2 - margin_x
        ry = self._screen.get_height() / 2 - height/2 - margin_y

        # Rotate the players, so that the current user is at the bottom of the window and the player order is clockwise.
        i = player_order.index(self.username)
        player_order = player_order[i+1:] + player_order[:i]

        # Compute the positions of the other players.
        if len(player_order) == 1:
            self._player_positions[player_order[0]] = (cx, height/2 + margin_y)
        else:
            n = len(player_order)-1
            for i, p in enumerate(player_order):
                d = i * math.pi / n
                x = int(cx - math.cos(d) * rx)
                y = int(cy - math.sin(d) * ry)
                self._player_positions[p] = (x, y)
        self._player_positions[self.username] = self.screen.get_size()

        # Show the other players.
        # TODO: The widget size should adapt to the length of the player name.
        for p in player_order:
            pos = self._player_positions[p]
            x = pos[0] - width/2
            y = pos[1] - height/2
            w = special_widgets.warning_widget((x, y), (width, height), p, self._font, close_on_click=False)
            w.visible = True
            self._background_widget.add_widget(w)

        # Add the box for the said tricks.
        width = 80
        for p in player_order:
            pos = self._player_positions[p]
            x = pos[0] - width/2
            y = pos[1] + height/2 + 10
            w = Text((x, y), (width, height), 50, "0/0", self._font, fill=(0, 0, 0, 160))
            w.opacity = 0
            self._said_tricks_widgets[p] = w
            self._background_widget.add_widget(w)
        height = 40
        x = self._user_move_widget.position[0] + self._user_move_widget.size[0] - width
        y = self._user_move_widget.position[1] + self._user_move_widget.size[1] + 10
        w = Text((x, y), (width, height), 50, "0 / 0", self._font, fill=(0, 0, 0, 160))
        w.opacity = 0
        self._said_tricks_widgets[self.username] = w
        self._background_widget.add_widget(w)
示例#5
0
    def ask_tricks(self, n):
        """
        Ask the user for the number of tricks.
        :param n: the maximum number of tricks
        """
        # Create the container with the fade in effect.
        container = Widget((self.screen.get_width() / 2 - 200, 100),
                           (400, self.screen.get_height() - 120), 40)
        container.opacity = 0
        container.add_action(actions.FadeInAction(0.5))
        self._ask_tricks_widget = container
        self._background_widget.add_widget(container)

        # Create the question text.
        text_w = special_widgets.warning_widget((0, 0), (400, 60),
                                                "How many tricks do you make?",
                                                self._font,
                                                close_on_click=False)
        text_w.visible = True
        container.add_widget(text_w)

        # Create the numbers.
        class ChooseHandler(object):
            def __init__(self, view, nn):
                self._view = view
                self._n = nn

            def __call__(self, x, y):
                self._view._handle_say_tricks(self._n)

        for i in xrange(n + 1):
            size = (50, 50)
            pos = ((i % 6) * (size[0] + 20), 80 + (i / 6) * (size[1] + 20))
            w = special_widgets.warning_widget(pos,
                                               size,
                                               str(i),
                                               self._font,
                                               close_on_click=False)
            w.visible = True
            w.handle_clicked = ChooseHandler(self, i)
            container.add_widget(w)
示例#6
0
    def _show_player_order(self, player_order):
        """
        Show the player order.
        :param player_order: the player order
        """
        width = 100
        height = 50
        margin_x = 10
        margin_y = 10

        cx = self._screen.get_width() / 2
        cy = self._screen.get_height() / 2
        rx = self._screen.get_width() / 2 - width / 2 - margin_x
        ry = self._screen.get_height() / 2 - height / 2 - margin_y

        # Rotate the players, so that the current user is at the bottom of the window and the player order is clockwise.
        i = player_order.index(self.username)
        player_order = player_order[i + 1:] + player_order[:i]

        # Compute the positions of the other players.
        if len(player_order) == 1:
            self._player_positions[player_order[0]] = (cx,
                                                       height / 2 + margin_y)
        else:
            n = len(player_order) - 1
            for i, p in enumerate(player_order):
                d = i * math.pi / n
                x = int(cx - math.cos(d) * rx)
                y = int(cy - math.sin(d) * ry)
                self._player_positions[p] = (x, y)
        self._player_positions[self.username] = self.screen.get_size()

        # Show the other players.
        # TODO: The widget size should adapt to the length of the player name.
        for p in player_order:
            pos = self._player_positions[p]
            x = pos[0] - width / 2
            y = pos[1] - height / 2
            w = special_widgets.warning_widget((x, y), (width, height),
                                               p,
                                               self._font,
                                               close_on_click=False)
            w.visible = True
            self._background_widget.add_widget(w)

        # Add the box for the said tricks.
        width = 80
        for p in player_order:
            pos = self._player_positions[p]
            x = pos[0] - width / 2
            y = pos[1] + height / 2 + 10
            w = Text((x, y), (width, height),
                     50,
                     "0/0",
                     self._font,
                     fill=(0, 0, 0, 160))
            w.opacity = 0
            self._said_tricks_widgets[p] = w
            self._background_widget.add_widget(w)
        height = 40
        x = self._user_move_widget.position[0] + self._user_move_widget.size[
            0] - width
        y = self._user_move_widget.position[1] + self._user_move_widget.size[
            1] + 10
        w = Text((x, y), (width, height),
                 50,
                 "0 / 0",
                 self._font,
                 fill=(0, 0, 0, 160))
        w.opacity = 0
        self._said_tricks_widgets[self.username] = w
        self._background_widget.add_widget(w)
示例#7
0
    def _create_widgets(self):
        """
        Create the widgets and return the background widget.
        :return: the background widget
        """
        # Create the background widget.
        bg = self._rm.get_image(BACKGROUND_IMAGE, self.screen.get_size())
        bg_widget = ImageWidget((0, 0), self.screen.get_size(), -1, bg)

        # Create the waiting text.
        wait_box = special_widgets.warning_widget(
            None, (400, 100),
            "Waiting for other players",
            self._font,
            screen_size=self.screen.get_size(),
            close_on_click=False)
        wait_box.visible = True
        bg_widget.add_widget(wait_box)
        self._warnings["wait_box"] = wait_box

        # Create the "invalid num tricks" warning.
        invalid_num_warning = special_widgets.warning_widget(
            None, (400, 100),
            "Invalid number of tricks",
            self._font,
            screen_size=self.screen.get_size())
        bg_widget.add_widget(invalid_num_warning)
        self._warnings["invalid_num_tricks"] = invalid_num_warning

        # Create the chat widget.
        chat_box = special_widgets.warning_widget(
            (10, self.screen.get_height() - 260), (260, 200),
            "chat",
            self._font,
            close_on_click=False)
        chat_box.visible = True
        bg_widget.add_widget(chat_box)

        # Create the "Your move" box.
        your_move_w = Text(
            (self.screen.get_width() - 140, self.screen.get_height() - 110),
            (120, 40),
            0,
            "Your move",
            self._font,
            fill=(0, 0, 0, 160))
        your_move_w.opacity = 0
        bg_widget.add_widget(your_move_w)
        self._user_move_widget = your_move_w

        # Create the trump widgets.
        trump_pos = (180, 180)
        trump_size = (125, 125)
        for color in ["W", "H", "D", "S", "C"]:
            im_filename = get_color_image_filename(color)
            im = self._rm.get_image(im_filename, trump_size)
            im_w = ImageWidget(trump_pos, trump_size, 0, im)
            im_w.opacity = 0
            bg_widget.add_widget(im_w)
            self._trump_widgets[color] = im_w

        # Create the "choose trump" widgets.
        class ChooseHandler(object):
            def __init__(self, view, trump):
                self._view = view
                self._trump = trump

            def __call__(self, x, y):
                self._view._handle_choose_trump(self._trump)

        choose_size = (90, 90)
        choose_trump_bg = pygame.Surface((400, 170), flags=pygame.SRCALPHA)
        choose_trump_bg.fill((0, 0, 0, 160))
        font_obj = self._font.render("Choose the trump:", True,
                                     (255, 255, 255, 255))
        choose_trump_bg.blit(
            font_obj,
            ((choose_trump_bg.get_width() - font_obj.get_width()) / 2, 20))
        choose_trump_container = ImageWidget(
            (self.screen.get_width() / 2 - 200, 200),
            choose_trump_bg.get_size(),
            99,
            choose_trump_bg,
            visible=False)
        for i, color in enumerate(["D", "S", "H", "C"]):
            im_filename = get_color_image_filename(color)
            im = self._rm.get_image(im_filename, choose_size)
            im_w = ImageWidget((i * (choose_size[0] + 10), 70), choose_size, 0,
                               im)
            choose_trump_container.add_widget(im_w)
            im_w.handle_clicked = ChooseHandler(self, color)
        bg_widget.add_widget(choose_trump_container)
        self._choose_trump_widget = choose_trump_container

        return bg_widget
示例#8
0
    def _create_widgets(self):
        """
        Create the widgets and return the background widget.
        :return: the background widget
        """
        # Create the background widget.
        bg = self._rm.get_image(BACKGROUND_IMAGE, self.screen.get_size())
        bg_widget = ImageWidget((0, 0), self.screen.get_size(), -1, bg)

        # Create the waiting text.
        wait_box = special_widgets.warning_widget(None, (400, 100), "Waiting for other players", self._font,
                                                  screen_size=self.screen.get_size(), close_on_click=False)
        wait_box.visible = True
        bg_widget.add_widget(wait_box)
        self._warnings["wait_box"] = wait_box

        # Create the "invalid num tricks" warning.
        invalid_num_warning = special_widgets.warning_widget(None, (400, 100), "Invalid number of tricks", self._font,
                                                             screen_size=self.screen.get_size())
        bg_widget.add_widget(invalid_num_warning)
        self._warnings["invalid_num_tricks"] = invalid_num_warning

        # Create the chat widget.
        chat_box = special_widgets.warning_widget((10, self.screen.get_height()-260), (260, 200), "chat", self._font,
                                                  close_on_click=False)
        chat_box.visible = True
        bg_widget.add_widget(chat_box)

        # Create the "Your move" box.
        your_move_w = Text((self.screen.get_width()-140, self.screen.get_height()-110), (120, 40), 0, "Your move",
                           self._font, fill=(0, 0, 0, 160))
        your_move_w.opacity = 0
        bg_widget.add_widget(your_move_w)
        self._user_move_widget = your_move_w

        # Create the trump widgets.
        trump_pos = (180, 180)
        trump_size = (125, 125)
        for color in ["W", "H", "D", "S", "C"]:
            im_filename = get_color_image_filename(color)
            im = self._rm.get_image(im_filename, trump_size)
            im_w = ImageWidget(trump_pos, trump_size, 0, im)
            im_w.opacity = 0
            bg_widget.add_widget(im_w)
            self._trump_widgets[color] = im_w

        # Create the "choose trump" widgets.
        class ChooseHandler(object):
            def __init__(self, view, trump):
                self._view = view
                self._trump = trump
            def __call__(self, x, y):
                self._view._handle_choose_trump(self._trump)
        choose_size = (90, 90)
        choose_trump_bg = pygame.Surface((400, 170), flags=pygame.SRCALPHA)
        choose_trump_bg.fill((0, 0, 0, 160))
        font_obj = self._font.render("Choose the trump:", True, (255, 255, 255, 255))
        choose_trump_bg.blit(font_obj, ((choose_trump_bg.get_width()-font_obj.get_width())/2, 20))
        choose_trump_container = ImageWidget((self.screen.get_width()/2 - 200, 200), choose_trump_bg.get_size(), 99,
                                             choose_trump_bg, visible=False)
        for i, color in enumerate(["D", "S", "H", "C"]):
            im_filename = get_color_image_filename(color)
            im = self._rm.get_image(im_filename, choose_size)
            im_w = ImageWidget((i*(choose_size[0]+10), 70), choose_size, 0, im)
            choose_trump_container.add_widget(im_w)
            im_w.handle_clicked = ChooseHandler(self, color)
        bg_widget.add_widget(choose_trump_container)
        self._choose_trump_widget = choose_trump_container

        return bg_widget