Пример #1
0
    def show_gui(self):
        FPS = 60
        clock = pygame.time.Clock()
        window = widget.Window(1400, 800, 'Welcome to Reversi AI',
                               'resources/images/background_100x100.png')
        keyboard = widget.Keyboard()
        board_widget      = widget.Board(window, 2, [0], players_name, 8, 8, 1, ('resources/images/white_82x82.png',         \
                          'resources/images/black_82x82.png', 'resources/images/board_82x82_b1.png'),                \
                          'resources/images/cursor_82x82.png')
        scoreboard = widget.ScoreBoard(window, 2, board_widget, ('resources/images/white_82x82.png',                               \
                                'resources/images/black_82x82.png', 'resources/images/background_100x100.png'))

        while True:
            # @ST if ESC is pressed, close window
            if not keyboard.monitor():
                window.quit()
                return

            self.state_mutex.acquire(
            )  # @ST self.history is shared among threads, we need a lock here
            if len(self.history) > 0:
                state = self.history[-1]
                self.state_mutex.release()
            else:
                self.state_mutex.release()
                continue
            pieces = [
                [-1] * self.board.cols for _ in range(self.board.rows)
            ]  # @ST @NOTE we use -1 for empty, 0 for player 1 and 1 for player 2
            score = [0, 0]  # @ST count pieces for two players
            p1_placed, p2_placed, previous, player = state
            for r in xrange(self.board.rows):
                for c in xrange(self.board.cols):
                    index = 1 << (self.board.cols * r + c)
                    if index & p1_placed:
                        pieces[r][c] = 0
                        score[0] += 1
                    if index & p2_placed:
                        pieces[r][c] = 1
                        score[1] += 1
            score[0] = format(score[0])
            score[1] = format(score[1])
            window.draw_background()
            board_widget.draw_self(pieces)
            self.status_text_mutex.acquire()  # @ST again, shared variable
            scoreboard.draw_self(
                score, self.status_text
            )  # @ST display info about who's turn or who's the winner,
            self.status_text_mutex.release()
            window.update(
            )  # @ST @NOTE You must call window.update() after you have drawn everything needed, or the screnn will flicker and flicker...
            clock.tick(FPS)
Пример #2
0
 def start(self):
     """Start user interface by generating widgets and objects."""
     add_new_btn = widget.Button("button_normal", "New")
     is_done_btn = widget.Button("button_light", "0")
     is_done_btn.on_click(self.__action.all_items, *(self, "show"))
     self._is_done_btn = is_done_btn
     c_height, c_width = self.__config.height_and_width
     height = c_height if c_height > 0 else DEFAULT_HEIGHT
     width = c_width if c_width > 0 else DEFAULT_WIDTH
     main_window = widget.Window("Main", "MTodo", None, width, height, {
         add_new_btn: "left",
         is_done_btn: "left"
     }, True)
     main_window.set_icon(self.__config.software_icon_file)
     main_window.on_resize(self.__action.reload_items, *(self, "refresh"))
     main_window.delegate("on_resize", self._update_preferences)
     self.__windows.update({"main_window": main_window})
     add_new_btn.on_click(self.__action.add_item, *(self, "new"))
     self.refresh(main_window)
     self.render()
Пример #3
0
    def todo_item(self,
                  todo_id=None,
                  title=None,
                  description=None,
                  is_done=None,
                  is_important=None):
        """New item window."""
        window_subtitle = "New Item"
        save_btn = widget.Button("button_blue", "Save")
        header_btns = {save_btn: "left"}
        if todo_id is not None:
            del_btn = widget.Button("button_red", "Delete")
            del_btn.on_click(self.__action.del_item, *(self, "del", todo_id))
            header_btns.update({del_btn: "left"})
            window_subtitle = "Edit Item"

        window = widget.Window("Main", "MTodo", window_subtitle, 500, 480,
                               header_btns, False)
        self.__windows["todo_item"] = window
        box = widget.Box("new_data", True)
        switch_box = widget.Box("switch_data", True)

        title_text = widget.Input("title", "Title", False)
        title_text.set_text(title)

        description_text = widget.Input("description", "Description", True)
        description_text.set_text(description)

        is_done_switch = widget.Switch("is_done")
        is_done_switch.set_value(is_done)
        is_done_label = widget.Label("is_done_label", "Is done")
        is_done_box = widget.Box("is_done_data", False)

        is_important_switch = widget.Switch("is_important")
        is_important_switch.set_value(is_important)
        is_important_label = widget.Label("is_important_label", "Is Important")
        is_important_box = widget.Box("is_important_data", False)

        is_done_box.join(is_done_label, True, True)
        is_done_box.join(is_done_switch)

        is_important_box.join(is_important_label, True, True)
        is_important_box.join(is_important_switch)

        box.join(title_text)
        box.join(description_text)
        switch_box.join(is_important_box)
        switch_box.join(is_done_box)

        if todo_id is None:
            save_btn.on_click(
                self.__action.add_item,
                *(self, "save", title_text, description_text, is_done_switch,
                  is_important_switch))
        else:
            save_btn.on_click(
                self.__action.edit_item,
                *(self, "save", todo_id, title_text, description_text,
                  is_done_switch, is_important_switch))
        window.join(box)
        window.join(switch_box)
        window.render()