第十章- 加农炮放到右下角

在这里插入图片描述

让加农炮的炮筒的大小依赖于力量

void CannonField::setForce(int newton)
{
    if (newton < 0)
        newton = 0;
    if (f == newton)
        return;
    f = newton;
    repaint(cannonRect());
    emit forceChanged(f);
}
    // 使用 force 值调整矩形的大小
    int width = 15 + f / 10;  // 根据 force 值增加宽度
    int height = 8 + f / 10;  // 根据 force 值增加高度
    p.drawRect(QRect(33, -4, width, height));

把加农炮放到右下角:

void CannonField::paintEvent(QPaintEvent *e)
{
    if (!e->rect().intersects(cannonRect()))
        return;

    QRect cr = cannonRect(); //矩形更新
    QPixmap pix(cr.size());  //位图双缓冲
	pix.fill(Qt::transparent); //使用透明像素图

    QPainter p(&pix);  //&pix,在pixmap上画
    p.setBrush(Qt::blue);
    p.setPen(Qt::NoPen);

    p.translate(pix.width(), pix.height()); // 将原点移到画布的右下
    p.drawPie(QRect(-35, -35, 70, 70), 90 * 16, 90 * 16); //绘图,开始角度为90,弧长为90*16(四分之一圆)
    p.rotate(ang); // 旋转画布 正值表示逆时针旋转,负值表示顺时针旋转
    p.drawRect(QRect(-48, -4, 15, 8)); //
    p.end();
    
    p.begin(this); //在屏幕上画
    p.drawPixmap(cr.topLeft(), pix); //把pixmap的一部分绘制到屏幕上。像素映射pix,原点就放在cr.topLeft()处。
}

QRect CannonField::cannonRect() const
{
    QRect r(0, 0, 50, 50);
    r.moveBottomRight(rect().bottomRight()); //把画布移动到右下
    return r;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值