ChessWidget::ChessWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setFixedSize(800, 660);
this->setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Window, QColor("#B1723C"));
this->setPalette(palette);
for (int i = 0; i < 15; ++i)
{
for (int j = 0; j < 15; ++j)
{
chess[i][j] = empyt;
}
}
}
ChessWidget::~ChessWidget()
{
}
void ChessWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);//反走样
QPen pen;
pen.setColor(QColor("#8D5822"));
pen.setWidth(7);
painter.setPen(pen);
QBrush brush;
brush.setColor(QColor("#EEC085"));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.drawRect(10, 10, 620, 620);
QPainter painters(this);
painters.setRenderHint(QPainter::Antialiasing, true);
int i, j;
for (i = 0; i <= 15; ++i)
{
painters.drawLine(20, 20 + i * 40, 620, 20 + i * 40);
painters.drawLine(20 + i * 40, 20, 20 + i * 40, 620);
}
brush.setStyle(Qt::SolidPattern);
for (i = 0; i < 15; ++i)
{
for (j = 0; j < 15; ++j)
{
if (chess[i][j] == BLACK)
{
brush.setColor(Qt::black);
painters.setBrush(brush);
painters.drawEllipse(QPoint((i) * 40 + 20, (j ) * 40 + 20), 20, 20);
}
else if (chess[i][j] == WHITE)
{
brush.setColor(Qt::white);
painters.setBrush(brush);
painters.drawEllipse(QPoint((i) * 40 + 20, (j) * 40 + 20), 20, 20);
}
}
}
}
void ChessWidget::mousePressEvent(QMouseEvent *e)
{
int x, y;
if (e->x() >= 20 || e->x() <= 620 || e->y() >= 20 || e->y() <= 620)
{
x = (e->x()-20)/40;
y = (e->y()-20)/40;
if (chess[x][y]==empyt)
{
chess[x][y] = player++ % 2 + 1;
}
}
update();
}