reversi_window.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:reversi_ai 作者: andysalerno 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号