def paintEvent(self, q_paint_event):
# board background color
painter = QtGui.QPainter(self)
bg_path = QtGui.QPainterPath()
bg_path.addRect(0, 0, self.width(), self.height())
painter.fillPath(bg_path, GREEN)
# draw the board lines
for i in range(8):
x_pos = self.width() / 8 * i
painter.drawLine(x_pos, 0, x_pos, self.height())
y_pos = self.height() / 8 * i
painter.drawLine(0, y_pos, self.width(), y_pos)
if self.board is not None and len(self.board) >= 8 * 8:
for h in range(8):
for w in range(8):
pieces_path = QtGui.QPainterPath()
w_dist = self.width() / 8
h_dist = self.height() / 8
x_pos = w_dist * w
y_pos = h_dist * h
bounding_rect = QtCore.QRectF(x_pos, y_pos, w_dist, h_dist)
index = (h * 8) + w
piece = self.board[index]
if piece == 'O':
pieces_path.addEllipse(bounding_rect)
painter.fillPath(pieces_path, WHITE)
elif piece == 'X':
pieces_path.addEllipse(bounding_rect)
painter.fillPath(pieces_path, BLACK)
评论列表
文章目录