[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:2027 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>t13/cannon.cpp Example File</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>t13/cannon.cpp Example File</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <pre>/****************************************************************
|
---|
| 36 | **
|
---|
| 37 | ** Implementation CannonField class, Qt tutorial 13
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "cannon.h"
|
---|
| 42 | #include <<a href="qtimer-h.html">qtimer.h</a>>
|
---|
| 43 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
| 44 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
| 45 | #include <<a href="qdatetime-h.html">qdatetime.h</a>>
|
---|
| 46 |
|
---|
| 47 | #include <math.h>
|
---|
| 48 | #include <stdlib.h>
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | <a name="f94"></a>CannonField::CannonField( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 52 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 53 | {
|
---|
| 54 | ang = 45;
|
---|
| 55 | f = 0;
|
---|
| 56 | timerCount = 0;
|
---|
| 57 | autoShootTimer = new <a href="qtimer.html">QTimer</a>( this, "movement handler" );
|
---|
| 58 | <a href="qobject.html#connect">connect</a>( autoShootTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()),
|
---|
| 59 | this, SLOT(moveShot()) );
|
---|
| 60 | shoot_ang = 0;
|
---|
| 61 | shoot_f = 0;
|
---|
| 62 | target = QPoint( 0, 0 );
|
---|
| 63 | gameEnded = FALSE;
|
---|
| 64 | <a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) );
|
---|
| 65 | newTarget();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | void <a name="f95"></a>CannonField::setAngle( int degrees )
|
---|
| 70 | {
|
---|
| 71 | if ( degrees < 5 )
|
---|
| 72 | degrees = 5;
|
---|
| 73 | if ( degrees > 70 )
|
---|
| 74 | degrees = 70;
|
---|
| 75 | if ( ang == degrees )
|
---|
| 76 | return;
|
---|
| 77 | ang = degrees;
|
---|
| 78 | <a href="qwidget.html#repaint">repaint</a>( cannonRect(), FALSE );
|
---|
| 79 | emit angleChanged( ang );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | void <a name="f96"></a>CannonField::setForce( int newton )
|
---|
| 84 | {
|
---|
| 85 | if ( newton < 0 )
|
---|
| 86 | newton = 0;
|
---|
| 87 | if ( f == newton )
|
---|
| 88 | return;
|
---|
| 89 | f = newton;
|
---|
| 90 | emit forceChanged( f );
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | void <a name="f97"></a>CannonField::shoot()
|
---|
| 95 | {
|
---|
| 96 | if ( isShooting() )
|
---|
| 97 | return;
|
---|
| 98 | timerCount = 0;
|
---|
| 99 | shoot_ang = ang;
|
---|
| 100 | shoot_f = f;
|
---|
| 101 | autoShootTimer-><a href="qtimer.html#start">start</a>( 50 );
|
---|
| 102 | emit canShoot( FALSE );
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | void <a name="f98"></a>CannonField::newTarget()
|
---|
| 107 | {
|
---|
| 108 | static bool first_time = TRUE;
|
---|
| 109 | if ( first_time ) {
|
---|
| 110 | first_time = FALSE;
|
---|
| 111 | <a href="qtime.html">QTime</a> midnight( 0, 0, 0 );
|
---|
| 112 | srand( midnight.<a href="qtime.html#secsTo">secsTo</a>(QTime::<a href="qtime.html#currentTime">currentTime</a>()) );
|
---|
| 113 | }
|
---|
| 114 | <a href="qregion.html">QRegion</a> r( targetRect() );
|
---|
| 115 | target = QPoint( 200 + rand() % 190,
|
---|
| 116 | 10 + rand() % 255 );
|
---|
| 117 | <a href="qwidget.html#repaint">repaint</a>( r.<a href="qrect.html#unite">unite</a>( targetRect() ) );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void <a name="f99"></a>CannonField::setGameOver()
|
---|
| 121 | {
|
---|
| 122 | if ( gameEnded )
|
---|
| 123 | return;
|
---|
| 124 | if ( isShooting() )
|
---|
| 125 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 126 | gameEnded = TRUE;
|
---|
| 127 | <a href="qwidget.html#repaint">repaint</a>();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void <a name="f100"></a>CannonField::restartGame()
|
---|
| 131 | {
|
---|
| 132 | if ( isShooting() )
|
---|
| 133 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 134 | gameEnded = FALSE;
|
---|
| 135 | <a href="qwidget.html#repaint">repaint</a>();
|
---|
| 136 | emit canShoot( TRUE );
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void <a name="f101"></a>CannonField::moveShot()
|
---|
| 140 | {
|
---|
| 141 | <a href="qregion.html">QRegion</a> r( shotRect() );
|
---|
| 142 | timerCount++;
|
---|
| 143 |
|
---|
| 144 | <a href="qrect.html">QRect</a> shotR = shotRect();
|
---|
| 145 |
|
---|
| 146 | if ( shotR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) ) {
|
---|
| 147 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 148 | emit hit();
|
---|
| 149 | emit canShoot( TRUE );
|
---|
| 150 | } else if ( shotR.<a href="qrect.html#x">x</a>() > width() || shotR.<a href="qrect.html#y">y</a>() > height() ) {
|
---|
| 151 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 152 | emit missed();
|
---|
| 153 | emit canShoot( TRUE );
|
---|
| 154 | } else {
|
---|
| 155 | r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) );
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | <a href="qwidget.html#repaint">repaint</a>( r );
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
|
---|
| 163 | {
|
---|
| 164 | <a href="qrect.html">QRect</a> updateR = e-><a href="qpaintevent.html#rect">rect</a>();
|
---|
| 165 | <a href="qpainter.html">QPainter</a> p( this );
|
---|
| 166 |
|
---|
| 167 | if ( gameEnded ) {
|
---|
| 168 | p.<a href="qpainter.html#setPen">setPen</a>( black );
|
---|
| 169 | p.<a href="qpainter.html#setFont">setFont</a>( QFont( "Courier", 48, QFont::Bold ) );
|
---|
| 170 | p.<a href="qpainter.html#drawText">drawText</a>( <a href="qwidget.html#rect">rect</a>(), AlignCenter, "Game Over" );
|
---|
| 171 | }
|
---|
| 172 | if ( updateR.<a href="qrect.html#intersects">intersects</a>( cannonRect() ) )
|
---|
| 173 | paintCannon( &p );
|
---|
| 174 | if ( isShooting() && updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) )
|
---|
| 175 | paintShot( &p );
|
---|
| 176 | if ( !gameEnded && updateR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) )
|
---|
| 177 | paintTarget( &p );
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | void <a name="f102"></a>CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 182 | {
|
---|
| 183 | p-><a href="qpainter.html#setBrush">setBrush</a>( black );
|
---|
| 184 | p-><a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
| 185 | p-><a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | void <a name="f103"></a>CannonField::paintTarget( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 190 | {
|
---|
| 191 | p-><a href="qpainter.html#setBrush">setBrush</a>( red );
|
---|
| 192 | p-><a href="qpainter.html#setPen">setPen</a>( black );
|
---|
| 193 | p-><a href="qpainter.html#drawRect">drawRect</a>( targetRect() );
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | const <a href="qrect.html">QRect</a> barrelRect(33, -4, 15, 8);
|
---|
| 198 |
|
---|
| 199 | void <a name="f104"></a>CannonField::paintCannon( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 200 | {
|
---|
| 201 | <a href="qrect.html">QRect</a> cr = cannonRect();
|
---|
| 202 | <a href="qpixmap.html">QPixmap</a> pix( cr.<a href="qrect.html#size">size</a>() );
|
---|
| 203 | pix.<a href="qpixmap.html#fill">fill</a>( this, cr.<a href="qrect.html#topLeft">topLeft</a>() );
|
---|
| 204 |
|
---|
| 205 | <a href="qpainter.html">QPainter</a> tmp( &pix );
|
---|
| 206 | tmp.<a href="qpainter.html#setBrush">setBrush</a>( blue );
|
---|
| 207 | tmp.<a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
| 208 |
|
---|
| 209 | tmp.<a href="qpainter.html#translate">translate</a>( 0, pix.<a href="qpixmap.html#height">height</a>() - 1 );
|
---|
| 210 | tmp.<a href="qpainter.html#drawPie">drawPie</a>( QRect( -35,-35, 70, 70 ), 0, 90*16 );
|
---|
| 211 | tmp.<a href="qpainter.html#rotate">rotate</a>( -ang );
|
---|
| 212 | tmp.<a href="qpainter.html#drawRect">drawRect</a>( barrelRect );
|
---|
| 213 | tmp.<a href="qpainter.html#end">end</a>();
|
---|
| 214 |
|
---|
| 215 | p-><a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | QRect <a name="f105"></a>CannonField::cannonRect() const
|
---|
| 220 | {
|
---|
| 221 | <a href="qrect.html">QRect</a> r( 0, 0, 50, 50 );
|
---|
| 222 | r.<a href="qrect.html#moveBottomLeft">moveBottomLeft</a>( <a href="qwidget.html#rect">rect</a>().bottomLeft() );
|
---|
| 223 | return r;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | QRect <a name="f106"></a>CannonField::shotRect() const
|
---|
| 228 | {
|
---|
| 229 | const double gravity = 4;
|
---|
| 230 |
|
---|
| 231 | double time = timerCount / 4.0;
|
---|
| 232 | double velocity = shoot_f;
|
---|
| 233 | double radians = shoot_ang*3.14159265/180;
|
---|
| 234 |
|
---|
| 235 | double velx = velocity*cos( radians );
|
---|
| 236 | double vely = velocity*sin( radians );
|
---|
| 237 | double x0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*cos(radians);
|
---|
| 238 | double y0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*sin(radians);
|
---|
| 239 | double x = x0 + velx*time;
|
---|
| 240 | double y = y0 + vely*time - 0.5*gravity*time*time;
|
---|
| 241 |
|
---|
| 242 | <a href="qrect.html">QRect</a> r = QRect( 0, 0, 6, 6 );
|
---|
| 243 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) );
|
---|
| 244 | return r;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | QRect <a name="f107"></a>CannonField::targetRect() const
|
---|
| 249 | {
|
---|
| 250 | <a href="qrect.html">QRect</a> r( 0, 0, 20, 10 );
|
---|
| 251 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint(target.x(),height() - 1 - target.y()) );
|
---|
| 252 | return r;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 | bool <a name="f108"></a>CannonField::isShooting() const
|
---|
| 257 | {
|
---|
| 258 | return autoShootTimer-><a href="qtimer.html#isActive">isActive</a>();
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | QSizePolicy CannonField::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const
|
---|
| 263 | {
|
---|
| 264 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
---|
| 265 | }
|
---|
| 266 | </pre><!-- eof -->
|
---|
| 267 | <p><address><hr><div align=center>
|
---|
| 268 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 269 | <td>Copyright © 2007
|
---|
| 270 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 271 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 272 | </table></div></address></body>
|
---|
| 273 | </html>
|
---|