让加农炮的炮筒的大小依赖于力量
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;
}