예제 #1
0
def gui():
    display = pygame.display.set_mode((300, 200))

    sb = SlideBar(print, (150, 100), (200, 30), 0, 160, 1, interval=4)

    def func_b():
        sb.color = RED

    red = Button(func_b, (300, 200), (60, 40), 'RED', anchor=BOTTOMRIGHT)

    def func_sb(value):
        red.topright = 300, value

    sb.func = func_sb
    sb.set(0)

    run = True
    while run:
        mouse = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                if mouse in sb:
                    sb.focus()

                if mouse in red:
                    red.click()

            if event.type == pygame.MOUSEBUTTONUP:
                red.release()
                sb.unfocus()

        display.fill(WHITE)
        sb.render(display)
        red.render(display)
        pygame.display.flip()
예제 #2
0
class MyButton:
    MODE_ACTIVATED_COLOR = (242, 142, 48)
    MODE_DEACTIVATED_COLOR = (219, 185, 151)
    MODE_DISABLED_COLOR = (181, 176, 171)

    def __init__(self,
                 callback,
                 panel_pos,
                 name,
                 label,
                 x,
                 y,
                 w,
                 h,
                 activated_color=GREEN,
                 deactivated_color=GREY):
        self.callback = callback
        self.panel_pos = panel_pos
        self.name = name
        self.activated_color = activated_color
        self.deactivated_color = deactivated_color
        self.gui_element = Button(self._do_action,
                                  (x + panel_pos[0], y + panel_pos[1]), (w, h),
                                  label,
                                  self.activated_color,
                                  anchor=TOPLEFT)
        self.down = False
        self.activated = True
        self.disabled = False
        self.handle_mouse_events = True

    def on_mouse_up(self):
        if self.down and not self.disabled:
            self.gui_element.unfocus()
            self.gui_element.release()
            self.down = False

    def on_mouse_down(self):
        if not self.down and not self.disabled:
            self.down = True
            self.gui_element.focus()
            self.gui_element.click()

    def activate(self):
        if not self.activated:
            self.gui_element.color = self.activated_color
            self.activated = True

    def deactivate(self):
        if self.activated:
            self.gui_element.color = self.deactivated_color
            self.activated = False

    def set_disabled(self, value):
        if value != self.disabled:
            self.disabled = value
            if self.disabled:
                self.gui_element.color = self.MODE_DISABLED_COLOR
            else:
                self.gui_element.color = self.activated_color

    def _do_action(self):
        self.callback()
예제 #3
0
def gui():
    """Main function"""

    global SCREEN_SIZE

    # #######
    # setup all objects
    # #######
    os.environ['SDL_VIDEO_CENTERED'] = '1'

    screen = new_widow()
    pygame.display.set_caption('Client swag')
    pygame.event.set_allowed([QUIT, KEYDOWN, MOUSEBUTTONDOWN])
    clock = pygame.time.Clock()
    # fps = FPSIndicator(clock)

    bound = Rectangle((0, 0), SCREEN_SIZE, BLUE, Rectangle.BORDER)
    login = InLineTextBox((5, 1), 200, MIDNIGHT_BLUE, anchor=TOPLEFT, default_text='Login: '******'Password: '******'Signed up !')
        print('login:'******'pass:'******'Signed in !')
        print('login:'******'pass:'******'Sign Up', YELLOW, anchor=TOPLEFT)
    sign_in = Button(sin, sign_up.topright, (100, passw.height), 'Sign In', GREEN, anchor=TOPLEFT)

    focus = FocusSelector(login, passw, sign_up, sign_in)
    focus.select(0)

    while True:

        # #######
        # Input loop
        # #######

        mouse = pygame.mouse.get_pos()

        for e in pygame.event.get():
            if e.type == QUIT:
                return 0  # quit

            elif e.type == KEYDOWN:
                # intercept special inputs
                if e.key == K_ESCAPE:
                    return 0  # quit

                elif e.key == K_F4 and e.mod & KMOD_ALT:
                    return 0  # quit

                elif e.key == K_TAB:
                    if e.mod & KMOD_SHIFT:
                        focus.prev()
                    else:
                        focus.next()

                elif e.key == K_RETURN:
                    if focus.selected() in (sign_up, sign_in):
                        focus.selected().click(40)
                    else:
                        focus.next()

                else:
                    # or give them to the selected box
                    focus.selected().update(e)

            elif e.type == VIDEORESIZE:
                SCREEN_SIZE = e.size
                screen = new_widow()

            elif e.type == MOUSEBUTTONDOWN:
                if mouse in login:
                    focus.select(login)

                elif mouse in passw:
                    focus.select(passw)

                elif mouse in sign_up:
                    sign_up.click()

                elif mouse in sign_in:
                    sign_in.click()

            elif e.type == MOUSEBUTTONUP:
                sign_in.release()
                sign_up.release()

        # #######
        # Draw all
        # #######

        screen.fill(WHITE)
        # fps.render(screen)
        bound.render(screen)
        login.render(screen)
        passw.render(screen)
        line(screen, login.topright + Sep(2, 0), login.bottomright + Sep(2, 0), BLUE)
        sign_up.render(screen)
        sign_in.render(screen)

        pygame.display.update()
        clock.tick(FPS)
예제 #4
0
파일: end_game.py 프로젝트: Wxf6/cchess
class GameTable():
    def __init__(self):

        self.screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
        #pygame.display.set_caption(u'中国象棋')  #.encode('utf-8'))
        self.selected = None
        self.last_moved = None
        self.last_checked = False
        #self.done = None

        self.surface = load_image('board.png')
        self.select_surface = load_image_alpha('select.png')
        self.done_surface = load_image_alpha('done.png')
        self.over_surface = load_image_alpha('over.png')
        self.pieces_image = {}

        #self.back_btn = Button(self.back_btn_clicked, (WIDTH+100, 40), (180, 40), 'BACK') #, anchor=BOTTOMRIGHT)
        self.back_btn = Button(self.back_btn_clicked, (WIDTH + 100, 82),
                               (180, 40), 'GoBack')  #, anchor=BOTTOMRIGHT)
        self.next_btn = Button(self.next_btn_clicked, (WIDTH + 100, 124),
                               (180, 40), 'NextGame')  #, anchor=BOTTOMRIGHT)
        self.prev_btn = Button(self.prev_btn_clicked, (WIDTH + 100, 166),
                               (180, 40), 'PrevGame')  #, anchor=BOTTOMRIGHT)
        self.restart_btn = Button(self.restart_btn_clicked, (WIDTH + 100, 40),
                                  (180, 40),
                                  'RestartGame')  #, anchor=BOTTOMRIGHT)
        self.info_box = InLineTextBox((WIDTH + 10, 186),
                                      220,
                                      RED,
                                      anchor=TOPLEFT,
                                      default_text='')
        self.good_box = InLineTextBox((WIDTH + 10, 206),
                                      220,
                                      RED,
                                      anchor=TOPLEFT,
                                      default_text='')

        for name in ['r', 'n', 'c', 'k', 'a', 'b', 'p']:
            self.pieces_image[name] = load_image_alpha(name + '.png')

        #self.check_sound = load_sound('check.wav')
        #self.move_sound = load_sound('move.wav')
        #self.capture_sound = load_sound('capture.wav')

        self.clock = pygame.time.Clock()
        self.board = ChessBoard()

        self.engine = [None, None]

    def back_btn_clicked(self):
        #sb.color = RED
        print("back btn_clicked")

    def next_btn_clicked(self):
        #sb.color = RED
        print("next btn_clicked")

    def prev_btn_clicked(self):
        #sb.color = RED
        print("prev btn_clicked")

    def restart_btn_clicked(self):
        #sb.color = RED
        print("prev btn_clicked")

    def new_game(self, title, fen, best_moves):

        self.board.from_fen(fen)
        self.best_moves = best_moves.split(",")
        self.best_index = 0

        self.selected = None
        self.last_moved = None
        self.move_history = []
        self.dead_side = None
        self.kill_count = 0

        engine = self.engine[self.board.move_side]
        if engine:
            engine.go_from(self.board.to_fen())
        #pygame.display.set_caption(title.encode('utf-8'))
        pygame.display.set_caption(title)

    def attach_engine(self, engine, side):
        self.engine[side] = engine

    def try_move(self, move_from, move_to):

        if not self.board.is_valid_move(move_from, move_to):
            # print u"走法错误."
            return (False, None)

        check_count = self.board.is_checked_move(move_from, move_to)
        if check_count:
            if self.last_checked:
                print(u"必须应将。")
            else:
                print(u"不能送将。")
            return (False, None)

        self.show_move(move_from, move_to)
        move = self.board.move(move_from, move_to)
        print(move.to_chinese())

        self.last_moved = Pos(move_to.x, move_to.y)
        self.selected = None

        if self.best_index >= 0:
            if move.to_iccs() == self.best_moves[self.best_index]:
                self.good_box.text = "Good"
                print(u"GOOD")
                self.best_index += 1
            else:
                self.good_box.text = "Not Good"
                self.best_index = -1
        if self.best_index >= len(self.best_moves):
            self.best_index = -1

        engine = self.engine[self.board.move_side]
        if engine:
            engine.stop_thinking()
        self.board.next_turn()

        move.for_ucci(self.board.move_side, self.move_history)
        self.move_history.append(move)

        if self.board.is_checkmate():
            self.info_box.text = "Dead!"
            print(u"将死!")
            return (True, self.board.move_side)

        self.last_checked = self.board.is_checked()
        if self.last_checked:
            self.info_box.text = "Checked!"
            print(u"将军!")

        if move.is_king_killed():
            self.info_box.text = "Killed!"
            print(u"杀死!")
            return (True, self.board.move_side)

        engine = self.engine[self.board.move_side]
        if engine:
            #print move.to_ucci_fen()
            if self.best_index >= 0:
                engine.preset_best_move(self.best_moves[self.best_index])
            else:
                engine.go_from(move.to_ucci_fen())

        return (True, None)

    def draw(self):

        self.screen.fill((0, 0, 0))
        self.screen.blit(self.surface, (0, 0))

        for x in range(9):
            for y in range(10):
                key = Pos(x, y)
                piece = self.board.get_piece(key)
                if not piece:
                    continue

                image = self.pieces_image[piece.fench.lower()]
                board_pos = pos_to_screen(key)

                if piece.side == ChessSide.RED:
                    offset = (0, 0, 52, 52)
                else:
                    offset = (53, 0, 52, 52)

                self.screen.blit(image, board_pos(), offset)

                if self.selected and key == self.selected:
                    self.screen.blit(self.select_surface, board_pos(), offset)
                elif self.last_moved and key == self.last_moved:
                    self.screen.blit(self.done_surface, board_pos(), offset)

                # elif key == self.done:
                #    self.screen.blit(self.over_surface, board_pos(), offset)

    def make_move_steps(self, p_from, p_to):

        steps = []
        step_count = 7

        for i in range(step_count):
            x = p_from.x + (p_to.x - p_from.x) / step_count * (i + 1)
            y = p_from.y + (p_to.y - p_from.y) / step_count * (i + 1)
            steps.append(Pos(x, y))

        return steps

    def show_move(self, p_from, p_to):

        board_p_from = pos_to_screen(p_from)
        board_p_to = pos_to_screen(p_to)
        steps = self.make_move_steps(board_p_from, board_p_to)

        for step in steps:
            self.screen.blit(self.surface, (0, 0))

            for x in range(9):
                for y in range(10):
                    key = Pos(x, y)
                    piece = self.board.get_piece(key)
                    if piece is None:
                        continue

                    board_pos = pos_to_screen(piece)
                    image = self.pieces_image[piece.fench.lower()]
                    offset = (int(piece.side) * 53, 0, 52, 52)
                    if key == p_from:
                        self.screen.blit(image, step(), offset)
                    else:
                        self.screen.blit(image, board_pos(), offset)

            pygame.display.flip()
            pygame.event.pump()
            msElapsed = self.clock.tick(30)

    def run_once(self):

        if self.kill_count:
            self.kill_count += 1
            #print self.kill_count
            if self.kill_count > 60:
                return (False, self.dead_side)

        engine = self.engine[self.board.move_side]

        if engine:
            engine.handle_msg_once()
            if not engine.move_queue.empty():
                output = engine.move_queue.get()
                # print output
                if output[0] == 'best_move':
                    p_from, p_to = output[1]["move"]
                    has_moved, dead_side = self.try_move(p_from, p_to)
                    if dead_side is not None:
                        self.kill_count = 1
                        self.dead_side = self.board.move_side
                        #return (False, dead_side)
                    if not has_moved:
                        print("engine output error", p_from, p_to)

                elif output[0] == 'dead':
                    # print self.board.move_side
                    print(win_dict[self.board.move_side])
                    self.kill_count = 1
                    self.dead_side = self.board.move_side
                    #return (False, self.board.move_side)

        self.clock.tick(30)

        for event in pygame.event.get():
            if event.type == QUIT:
                return (True, None)

            self.back_btn.update(event)
            self.next_btn.update(event)
            self.prev_btn.update(event)
            self.restart_btn.update(event)

            if event.type == MOUSEBUTTONDOWN:
                #print(event.button)
                mouse_left_down, _, mouse_right_down = pygame.mouse.get_pressed(
                )

                #if mouse_right_down:
                #    return (False, -1)

                sx, sy = mouse = pygame.mouse.get_pos()

                if mouse in self.back_btn:
                    self.back_btn.click()
                elif mouse in self.next_btn:
                    self.next_btn.click()
                elif mouse in self.prev_btn:
                    self.prev_btn.click()
                elif mouse in self.restart_btn:
                    #self.restart_btn.click()
                    return (False, -1)

                if sx < BORDER or sx > (WIDTH - BORDER):
                    break
                if sy < BORDER or sy > (HEIGHT - BORDER):
                    break

                key = screen_to_pos(Pos(sx, sy))
                piece = self.board.get_piece(key)

                if piece and piece.side == self.board.move_side:
                    self.selected = key
                    self.last_moved = None
                else:
                    # move check
                    has_moved = False
                    if self.selected and (key != self.selected):
                        move_from = self.selected
                        move_to = key
                        has_moved, dead_side = self.try_move(
                            move_from, move_to)

                        if dead_side is not None:
                            self.kill_count = 1
                            self.dead_side = dead_side
                            #time.sleep(1)
                            #return (False, dead_side)

                        # pickup check
                        # if not has_moved:
                        #    piece = self.board.get_piece(key)
                        #    if piece and piece.side == self.board.move_side:
                        #        self.selected = key
                        #        self.last_moved = None
            elif event.type == MOUSEBUTTONUP:
                self.back_btn.release()
                self.next_btn.release()
                self.prev_btn.release()
                self.restart_btn.release()

        self.draw()
        self.back_btn.render(self.screen)
        self.next_btn.render(self.screen)
        self.prev_btn.render(self.screen)
        self.restart_btn.render(self.screen)
        self.info_box.render(self.screen)
        self.good_box.render(self.screen)
        pygame.display.flip()

        return (False, None)