当前位置: 首页>>代码示例>>Python>>正文


Python Board.get方法代码示例

本文整理汇总了Python中chess.Board.get方法的典型用法代码示例。如果您正苦于以下问题:Python Board.get方法的具体用法?Python Board.get怎么用?Python Board.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chess.Board的用法示例。


在下文中一共展示了Board.get方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Example

# 需要导入模块: from chess import Board [as 别名]
# 或者: from chess.Board import get [as 别名]
class Example(QtGui.QWidget):
    
    board_text_space = 15
    min_board_sz = 80
    hit_cell_color = "red"
    move_cell_color = "green"
    hit_and_move_cell_color = "orange"
    piece_cell_color = "yellow"

    img_path = os.path.join(self_dir, "chess_piece")

    piece_fname_mapping = {
        "WQ": "white_quinn",
        "WK": "white_king",
        "WP": "white_pawn",
        "WN": "white_knight",
        "WR": "white_rook",
        "WB": "white_bishop",
        "BQ": "black_quinn",
        "BK": "black_king",
        "BP": "black_pawn",
        "BN": "black_knight",
        "BR": "black_rook",
        "BB": "black_bishop",
    }

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        self.font = QtGui.QFont("times", 24)
        self.chess_piece_font = QtGui.QFont("console", 30)
        self.fm = QtGui.QFontMetrics(self.font)
        self.chess_piece_fm = QtGui.QFontMetrics(self.chess_piece_font)
        self.vertical_lines_x_corrds = []
        self.horizontal_lines_y_coords = []

        self.board = Board(knorre_vs_neumann())

        self.pieces_images = {}
        self.load_pieces(self.img_path)

        self.active_cell = None
        self.selected_piece = None

    def cell_to_coords(self, cell):
        return self.cell_index_to_coords(*to_tuple_pos(cell))

    def cell_index_to_coords(self, cell_x, cell_y):
        cell_y = 7 - cell_y
        x1, x2 = self.vertical_lines_x_corrds[cell_x: cell_x + 2]
        y1, y2 = self.horizontal_lines_y_coords[cell_y: cell_y + 2]
        return x1, y1, x2 - x1, y2 - y1

    def load_pieces(self, path):
        for key, fname in self.piece_fname_mapping.items():
            img = QtGui.QImage()
            img.load(os.path.join(path, fname))
            self.pieces_images[key] = img

    def initUI(self):      
        self.setGeometry(300, 300, 800, 800)
        self.setWindowTitle('Chess GUI')
        self.show()

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        qp.begin(self)
        self.draw_board(qp)

    def get_cell_from_abs_pos(self, x, y):
        if x < self.vertical_lines_x_corrds[0] or x > self.vertical_lines_x_corrds[-1]:
            return False, None, None

        if y < self.horizontal_lines_y_coords[0] or y > self.horizontal_lines_y_coords[-1]:
            return False, None, None

        for xpos, cx in enumerate(self.vertical_lines_x_corrds[1:]):
            if x < cx:
                break

        for ypos, cy in enumerate(self.horizontal_lines_y_coords[1:]):
            if y < cy:
                break

        return True, xpos, 7 - ypos

    def mousePressEvent(self, event):
        ok, xpos, ypos = self.get_cell_from_abs_pos(event.pos().x(), event.pos().y())
        if ok:
            piece = self.board.get((xpos, ypos))
            if piece is not None:
                self.selected_piece = piece
            else:
                self.selected_piece = None
        else:
            return super(Example, self).mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.selected_piece is not None:
            ok, xpos, ypos = self.get_cell_from_abs_pos(event.pos().x(), event.pos().y())
#.........这里部分代码省略.........
开发者ID:ddovbii,项目名称:python-classes,代码行数:103,代码来源:chess_gui.py


注:本文中的chess.Board.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。