<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt - 3.3.8 - espenr - 2499/qt - x11 - free - 3.3.8/doc/tutorial.doc:1506 -->
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title > TQt Tutorial - Chapter 11: Giving It a Shot< / title >
< style type = "text/css" > < ! - -
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
-->< / style >
< / head >
< body >
< table border = "0" cellpadding = "0" cellspacing = "0" width = "100%" >
< tr bgcolor = "#E5E5E5" >
< td valign = center >
< a href = "index.html" >
< font color = "#004faf" > Home< / font > < / a >
| < a href = "classes.html" >
< font color = "#004faf" > All Classes< / font > < / a >
| < a href = "mainclasses.html" >
< font color = "#004faf" > Main Classes< / font > < / a >
| < a href = "annotated.html" >
< font color = "#004faf" > Annotated< / font > < / a >
| < a href = "groups.html" >
< font color = "#004faf" > Grouped Classes< / font > < / a >
| < a href = "functions.html" >
< font color = "#004faf" > Functions< / font > < / a >
< / td >
< td align = "right" valign = "center" > < img src = "logo32.png" align = "right" width = "64" height = "32" border = "0" > < / td > < / tr > < / table > < h1 align = center > TQt Tutorial - Chapter 11: Giving It a Shot< / h1 >
< p > < center > < img src = "t11.png" alt = "Screenshot of tutorial eleven" > < / center >
< p > In this example we introduce a timer to implement animated shooting.
< p > < ul >
< li > < a href = "t11-lcdrange-h.html" > t11/lcdrange.h< / a > contains the LCDRange
class definition.
< li > < a href = "t11-lcdrange-cpp.html" > t11/lcdrange.cpp< / a > contains the LCDRange
implementation.
< li > < a href = "t11-cannon-h.html" > t11/cannon.h< / a > contains the CannonField class
definition.
< li > < a href = "t11-cannon-cpp.html" > t11/cannon.cpp< / a > contains the CannonField
implementation.
< li > < a href = "t11-main-cpp.html" > t11/main.cpp< / a > contains MyWidget and main.
< / ul >
< p > < h2 > Line-by-line Walkthrough
< / h2 >
< a name = "1" > < / a > < p > < h3 > < a href = "t11-cannon-h.html" > t11/cannon.h< / a >
< / h3 >
< a name = "1-1" > < / a > < p > The CannonField now has shooting capabilities.
< p >
< p > < pre > void shoot();
< / pre >
< p > Calling this slot will make the cannon shoot if a shot is not in the air.
< p > < pre > private slots:
void moveShot();
< / pre >
< p > This private slot is used to move the shot while it is in the air,
using a < a href = "ntqtimer.html" > TQTimer< / a > .
< p > < pre > private:
void paintShot( < a href = "ntqpainter.html" > TQPainter< / a > * );
< / pre >
< p > This private function paints the shot.
< p > < pre > < a href = "ntqrect.html" > TQRect< / a > shotRect() const;
< / pre >
< p > This private function returns the shot's enclosing rectangle if
one is in the air; otherwise the returned rectangle is undefined.
< p > < pre > int timerCount;
< a href = "ntqtimer.html" > TQTimer< / a > * autoShootTimer;
float shoot_ang;
float shoot_f;
};
< / pre >
< p > These private variables contain information that describes the shot. The
< tt > timerCount< / tt > keeps track of the time passed since the shot was fired.
The < tt > shoot_ang< / tt > is the cannon angle and < tt > shoot_f< / tt > is the cannon force
when the shot was fired.
< p > < h3 > < a href = "t11-cannon-cpp.html" > t11/cannon.cpp< / a >
< / h3 >
< a name = "1-2" > < / a > < p >
< p > < pre > #include < math.h>
< / pre >
< p > We include the math library because we need the sin() and cos() functions.
< p > < pre > CannonField::CannonField( < a href = "ntqwidget.html" > TQWidget< / a > *parent, const char *name )
: < a href = "ntqwidget.html" > TQWidget< / a > ( parent, name )
{
ang = 45;
f = 0;
timerCount = 0;
autoShootTimer = new < a href = "ntqtimer.html" > TQTimer< / a > ( this, "movement handler" );
< a name = "x2379" > < / a > < a href = "ntqobject.html#connect" > connect< / a > ( autoShootTimer, TQ_SIGNAL(< a href = "ntqtimer.html#timeout" > timeout< / a > ()),
this, TQ_SLOT(moveShot()) );
shoot_ang = 0;
shoot_f = 0;
< a href = "ntqwidget.html#setPalette" > setPalette< / a > ( TQPalette( TQColor( 250, 250, 200) ) );
}
< / pre >
< p > We initialize our new private variables and connect the < a href = "ntqtimer.html#timeout" > TQTimer::timeout< / a > () signal to our moveShot() slot. We'll move the
shot every time the timer times out.
< p > < pre > void CannonField::shoot()
{
< a name = "x2376" > < / a > if ( autoShootTimer-> < a href = "ntqtimer.html#isActive" > isActive< / a > () )
return;
timerCount = 0;
shoot_ang = ang;
shoot_f = f;
< a name = "x2377" > < / a > autoShootTimer-> < a href = "ntqtimer.html#start" > start< / a > ( 50 );
}
< / pre >
< p > This function shoots a shot unless a shot is in the air. The < tt > timerCount< / tt >
is reset to zero. The < tt > shoot_ang< / tt > and < tt > shoot_f< / tt > are set to the current
cannon angle and force. Finally, we start the timer.
< p > < pre > void CannonField::moveShot()
{
< a href = "ntqregion.html" > TQRegion< / a > r( shotRect() );
timerCount++;
< a href = "ntqrect.html" > TQRect< / a > shotR = shotRect();
if ( shotR.< a href = "ntqrect.html#x" > x< / a > () > width() || shotR.< a href = "ntqrect.html#y" > y< / a > () > height() )
< a name = "x2378" > < / a > autoShootTimer-> < a href = "ntqtimer.html#stop" > stop< / a > ();
else
< a name = "x2373" > < / a > r = r.< a href = "ntqrect.html#unite" > unite< / a > ( TQRegion( shotR ) );
< a href = "ntqwidget.html#repaint" > repaint< / a > ( r );
}
< / pre >
< p > moveShot() is the slot that moves the shot, called every 50
milliseconds when the < a href = "ntqtimer.html" > TQTimer< / a > fires.
< p > Its tasks are to compute the new position, repaint the screen with the
shot in the new position, and if necessary, stop the timer.
< p > First we make a < a href = "ntqregion.html" > TQRegion< / a > that holds the old shotRect(). A < a href = "ntqregion.html" > TQRegion< / a >
is capable of holding any sort of region, and we'll use it here to
simplify the painting. ShotRect() returns the rectangle where the
shot is now - it is explained in detail later.
< p > Then we increment the < tt > timerCount< / tt > , which has the effect of moving the
shot one step along its trajectory.
< p > Next we fetch the new shot rectangle.
< p > If the shot has moved beyond the right or bottom edge of the widget, we
stop the timer or we add the new shotRect() to the TQRegion.
< p > Finally, we repaint the TQRegion. This will send a single paint event
for just the one or two rectangles that need updating.
< p > < pre > void CannonField::< a href = "ntqwidget.html#paintEvent" > paintEvent< / a > ( < a href = "qpaintevent.html" > TQPaintEvent< / a > *e )
{
< a name = "x2369" > < / a > < a href = "ntqrect.html" > TQRect< / a > updateR = e-> < a href = "qpaintevent.html#rect" > rect< / a > ();
< a href = "ntqpainter.html" > TQPainter< / a > p( this );
< a name = "x2370" > < / a > if ( updateR.< a href = "ntqrect.html#intersects" > intersects< / a > ( cannonRect() ) )
paintCannon( & p );
if ( autoShootTimer-> < a href = "ntqtimer.html#isActive" > isActive< / a > () & &
updateR.< a href = "ntqrect.html#intersects" > intersects< / a > ( shotRect() ) )
paintShot( & p );
}
< / pre >
< p > The paint event function has been split in two since the previous
chapter. Now we fetch the bounding rectangle of the region that
needs painting, check whether it intersects either the cannon and/or
the shot, and if necessary, call paintCannon() and/or paintShot().
< p > < pre > void CannonField::paintShot( < a href = "ntqpainter.html" > TQPainter< / a > *p )
{
p-> < a href = "ntqpainter.html#setBrush" > setBrush< / a > ( black );
p-> < a href = "ntqpainter.html#setPen" > setPen< / a > ( NoPen );
< a name = "x2366" > < / a > p-> < a href = "ntqpainter.html#drawRect" > drawRect< / a > ( shotRect() );
}
< / pre >
< p > This private function paints the shot by drawing a black filled rectangle.
< p > We leave out the implementation of paintCannon(); it is the same as
the paintEvent() from the previous chapter.
< p > < pre > TQRect 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 );
< a name = "x2372" > < / a > double x0 = ( barrelRect.< a href = "ntqrect.html#right" > right< / a > () + 5 )*cos(radians);
double y0 = ( barrelRect.< a href = "ntqrect.html#right" > right< / a > () + 5 )*sin(radians);
double x = x0 + velx*time;
double y = y0 + vely*time - 0.5*gravity*time*time;
< a href = "ntqrect.html" > TQRect< / a > r = TQRect( 0, 0, 6, 6 );
< a name = "x2371" > < / a > r.< a href = "ntqrect.html#moveCenter" > moveCenter< / a > ( TQPoint( tqRound(x), height() - 1 - tqRound(y) ) );
return r;
}
< / pre >
< p > This private function calculates the center point of the shot and returns
the enclosing rectangle of the shot. It uses the initial cannon force and
angle in addition to < tt > timerCount< / tt > , which increases as time passes.
< p > The formula used is the classical Newtonian formula for frictionless
movement in a gravity field. For simplicity, we've chosen to
disregard any Einsteinian effects.
< p > We calculate the center point in a coordinate system where y
coordinates increase upward. After we have calculated the center
point, we construct a < a href = "ntqrect.html" > TQRect< / a > with size 6x6 and move its center point to
the point calculated above. In the same operation we convert the
point into the widget's coordinate system (see < a href = "coordsys.html" > The
Coordinate System< / a > ).
< p > The tqRound() function is an inline function defined in ntqglobal.h (included
by all other TQt header files). tqRound() rounds a double to the closest
integer.
< p > < h3 > < a href = "t11-main-cpp.html" > t11/main.cpp< / a >
< / h3 >
< a name = "1-3" > < / a > < p >
< p > < pre > class MyWidget: public < a href = "ntqwidget.html" > TQWidget< / a >
{
public:
MyWidget( < a href = "ntqwidget.html" > TQWidget< / a > *parent=0, const char *name=0 );
};
< / pre >
< p > The only addition is the Shoot button.
< p > < pre > < a href = "ntqpushbutton.html" > TQPushButton< / a > *shoot = new < a href = "ntqpushbutton.html" > TQPushButton< / a > ( "& Shoot", this, "shoot" );
< a name = "x2382" > < / a > shoot-> < a href = "ntqwidget.html#setFont" > setFont< / a > ( TQFont( "Times", 18, TQFont::Bold ) );
< / pre >
< p > In the constructor we create and set up the Shoot button exactly like we
did with the Quit button. Note that the first argument to the constructor
is the button text, and the third is the widget's name.
< p > < pre > < a href = "ntqobject.html#connect" > connect< / a > ( shoot, TQ_SIGNAL(< a href = "ntqbutton.html#clicked" > clicked< / a > ()), cannonField, TQ_SLOT(shoot()) );
< / pre >
< p > Connects the clicked() signal of the Shoot button to the shoot() slot
of the CannonField.
< p > < h2 > Behavior
< / h2 >
< a name = "2" > < / a > < p > The cannon can shoot, but there's nothing to shoot at.
< p > (See < a href = "tutorial1-07.html#compiling" > Compiling< / a > for how to create a
makefile and build the application.)
< p > < h2 > Exercises
< / h2 >
< a name = "3" > < / a > < p > Make the shot a filled circle. Hint: < a href = "ntqpainter.html#drawEllipse" > TQPainter::drawEllipse< / a > () may
help.
< p > Change the color of the cannon when a shot is in the air.
< p > You're now ready for < a href = "tutorial1-12.html" > Chapter 12.< / a >
< p > [< a href = "tutorial1-10.html" > Previous tutorial< / a > ]
[< a href = "tutorial1-12.html" > Next tutorial< / a > ]
[< a href = "tutorial.html" > Main tutorial page< / a > ]
< p >
<!-- eof -->
< p > < address > < hr > < div align = center >
< table width = 100% cellspacing = 0 border = 0 > < tr >
< td > Copyright © 2007
< a href = "troll.html" > Trolltech< / a > < td align = center > < a href = "trademarks.html" > Trademarks< / a >
< td align = right > < div align = right > TQt 3.3.8< / div >
< / table > < / div > < / address > < / body >
< / html >