| 主页 | 所有的类 | 主要的类 | 注释的类 | 分组的类 | 函数 |

在这个例子里我们介绍了一个定时器来实现动画的射击。
CannonField现在就有了射击能力。
void shoot();
当炮弹不在空中中,调用这个槽就会使加农炮射击。
private slots:
void moveShot();
当炮弹正在空中时,这个私有槽使用一个定时器来移动射击。
private:
void paintShot( QPainter * );
这个函数来画射击。
QRect shotRect() const;
当炮弹正在空中的时候,这个私有函数返回封装它所占用空间的矩形,否则它就返回一个没有定义的矩形。
int timerCount;
QTimer * autoShootTimer;
float shoot_ang;
float shoot_f;
};
这些私有变量包含了描述射击的信息。timerCount保留了射击进行后的时间。shoot_ang是加农炮射击时的角度,shoot_f是射击时加农炮的力量。
#include <math.h>
我们包含了数学库,因为我们需要使用sin()和cos()函数。
CannonField::CannonField( QWidget *parent, const char *name )
: QWidget( parent, name )
{
ang = 45;
f = 0;
timerCount = 0;
autoShootTimer = new QTimer( this, "movement handler" );
connect( autoShootTimer, SIGNAL(timeout()),
this, SLOT(moveShot()) );
shoot_ang = 0;
shoot_f = 0;
setPalette( QPalette( QColor( 250, 250, 200) ) );
}
我们初始化我们新的私有变量并且把QTimer::timeout()信号和我们的moveShot()槽相连。我们会在定时器超时的时候移动射击。
void CannonField::shoot()
{
if ( autoShootTimer->isActive() )
return;
timerCount = 0;
shoot_ang = ang;
shoot_f = f;
autoShootTimer->start( 50 );
}
只要炮弹不在空中,这个函数就会进行一次射击。timerCount被重新设置为零。shoot_ang和shoot_f设置为当前加农炮的角度和力量。最后,我们开始这个定时器。
void CannonField::moveShot()
{
QRegion r( shotRect() );
timerCount++;
QRect shotR = shotRect();
if ( shotR.x() > width() || shotR.y() > height() )
autoShootTimer->stop();
else
r = r.unite( QRegion( shotR ) );
repaint( r );
}
moveShot()是一个移动射击的槽,当QTimer开始的时候,每50毫秒被调用一次。
它的任务就是计算新的位置,重新画屏幕并把炮弹放到新的位置,并且如果需要的话,停止定时器。
首先我们使用QRegion来保留旧的shotRect()。QRegion可以保留任何种类的区域,并且我们可以用它来简化绘画过程。shotRect()返回现在炮弹所在的矩形——稍后我们会详细介绍。
然后我们增加timerCount,用它来实现炮弹在它的轨迹中移动的每一步。
下一步我们算出新的炮弹的矩形。
如果炮弹已经移动到窗口部件的右面或者下面的边界,我们停止定时器或者添加新的shotRect()到QRegion。
最后,我们重新绘制QRegion。这将会发送一个单一的绘画事件,但仅仅有一个到两个举行需要刷新。
void CannonField::paintEvent( QPaintEvent *e )
{
QRect updateR = e->rect();
QPainter p( this );
if ( updateR.intersects( cannonRect() ) )
paintCannon( &p );
if ( autoShootTimer->isActive() &&
updateR.intersects( shotRect() ) )
paintShot( &p );
}
绘画事件函数在前一章中已经被分成两部分了。现在我们得到的新的矩形区域需要绘画,检查加农炮和/或炮弹是否相交,并且如果需要的话,调用paintCannon()和/或paintShot()。
void CannonField::paintShot( QPainter *p )
{
p->setBrush( black );
p->setPen( NoPen );
p->drawRect( shotRect() );
}
这个私有函数画一个黑色填充的矩形作为炮弹。
我们把paintCannon()的实现放到一边,它和前一章中的paintEvent()一样。
QRect CannonField::shotRect() const
{
const double gravity = 4;
double time = timerCount / 4.0;
double velocity = shoot_f;
double radians = shoot_ang*3.14159265/180;
double velx = velocity*cos( radians );
double vely = velocity*sin( radians );
double x0 = ( barrelRect.right() + 5 )*cos(radians);
double y0 = ( barrelRect.right() + 5 )*sin(radians);
double x = x0 + velx*time;
double y = y0 + vely*time - 0.5*gravity*time*time;
QRect r = QRect( 0, 0, 6, 6 );
r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) );
return r;
}
这个私有函数计算炮弹的中心点并且返回封装炮弹的矩形。它除了使用自动增加所过去的时间的timerCount之外,还使用初始时的加农炮的力量和角度。
运算公式使用的是有重力的环境下光滑运动的经典牛顿公式。简单地说,我们已经选择忽略爱因斯坦理论的结果。
我们在一个y坐标向上增加的坐标系统中计算中心点。在我们计算出中心点之后,我们构造一个6*6大小的QRect,并把它的中心移动到我们上面所计算出的中心点。同样的操作我们把这个点移动到窗口部件的坐标系统(请看坐标系统)。
qRound()函数是一个在qglobal.h中定义的内嵌函数(被其它所有Qt头文件包含)。qRound()把一个双精度实数变为最接近的整数。
class MyWidget: public QWidget
{
public:
MyWidget( QWidget *parent=0, const char *name=0 );
};
唯一的增加是Shoot按钮。
QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" );
shoot->setFont( QFont( "Times", 18, QFont::Bold ) );
在构造函数中我们创建和设置Shoot按钮就像我们对Quit按钮所做的那样。注意构造函数的第一个参数是按钮的文本,并且第三个是窗口部件的名称。
connect( shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()) );
把Shoot按钮的clicked()信号和CannonField的shoot()槽连接起来。
The cannon can shoot, but there's nothing to shoot at.
(请看编译来学习如何创建一个makefile和连编应用程序。)
用一个填充的圆来表示炮弹。提示:QPainter::drawEllipse()会对你有所帮助。
当炮弹在空中的时候,改变加农炮的颜色。
现在你可以进行第十二章了。
| Copyright © 2002 Trolltech | Trademarks | 译者:Cavendish | Qt 3.0.5版
|