我的应用程序是使用最新版本的 Python 3.7、PyQt5 和 python-chess 编写的。我有一个 SVG 棋盘,由 python-chess 本身制作。我的应用程序处理棋盘上的鼠标点击,突出显示点击的方块。我的精度有问题。有时会突出显示相邻的正方形。我在棋盘的左侧和顶部也有棋盘坐标,这是我的错误的根本原因。没有棋盘坐标,它可以完美运行。
如果有人有兴趣帮助我,这里是代码。
import chess
import chess.svg
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtSvg import QSvgWidget
COORDINATES = True
FLIPPED = False
class Chessboard(QSvgWidget):
def __init__(self):
super().__init__()
self.clicked_square = -20
self.move_from_square = -20
self.move_to_square = -20
self.piece_to_move = [None, None]
viewbox_size = 400
self.margin = chess.svg.MARGIN * 800 / viewbox_size if COORDINATES else 0
self.file_size = chess.svg.SQUARE_SIZE * 800 / viewbox_size
self.rank_size = chess.svg.SQUARE_SIZE * 800 / viewbox_size
self.chessboard = chess.Board()
self.draw_chessboard()
@pyqtSlot(QSvgWidget)
def mousePressEvent(self, event):
x_coordinate = event.x()
y_coordinate = event.y()
file = int(x_coordinate / 800 * 8)
rank = 7 - int(y_coordinate / 800 * 8)
if file < 0:
file = 0
if rank < 0:
rank = 0
if file > 7:
file = 7
if rank > 7:
rank = 7
self.clicked_square = chess.square(file, rank)
piece = self.chessboard.piece_at(self.clicked_square)
file_character = chr(file + 97)
rank_number = str(rank + 1)
ply = f"{file_character}{rank_number}"
if self.piece_to_move[0]:
move = chess.Move.from_uci(f"{self.piece_to_move[1]}{ply}")
if move in self.chessboard.legal_moves:
self.chessboard.push(move)
self.move_from_square = move.from_square
self.move_to_square = move.to_square
piece = None
ply = None
self.piece_to_move = [piece, ply]
self.draw_chessboard()
def draw_chessboard(self):
is_it_check = self.chessboard.king(self.chessboard.turn) \
if self.chessboard.is_check() \
else None
self.svg_chessboard = chess.svg.board(board=self.chessboard,
lastmove=chess.Move(from_square=self.move_from_square,
to_square=self.move_to_square),
arrows=[(self.clicked_square, self.clicked_square),
(self.move_from_square, self.move_to_square)],
check=is_it_check,
flipped=FLIPPED,
coordinates=COORDINATES,
size=800)
self.svg_chessboard_encoded = self.svg_chessboard.encode("utf-8")
self.load(self.svg_chessboard_encoded)