Cry . 2019-03-10 17:45 采纳率: 0%
浏览 563

用qt写的五子棋界面,请问为什么第一行和第一列 不能落棋子? 大佬 bai'tuo

图片说明

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();

}




  • 写回答

1条回答

  • 瓦史托德 2019-03-11 09:38
    关注

    为什么我数有16行和列?

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试