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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题