[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:2376 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>t14/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>t14/cannon.cpp Example File</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <pre>/****************************************************************
|
---|
| 36 | **
|
---|
| 37 | ** Implementation CannonField class, Qt tutorial 14
|
---|
| 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="f121"></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 | barrelPressed = FALSE;
|
---|
| 65 | <a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) );
|
---|
| 66 | newTarget();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | void <a name="f122"></a>CannonField::setAngle( int degrees )
|
---|
| 71 | {
|
---|
| 72 | if ( degrees < 5 )
|
---|
| 73 | degrees = 5;
|
---|
| 74 | if ( degrees > 70 )
|
---|
| 75 | degrees = 70;
|
---|
| 76 | if ( ang == degrees )
|
---|
| 77 | return;
|
---|
| 78 | ang = degrees;
|
---|
| 79 | <a href="qwidget.html#repaint">repaint</a>( cannonRect(), FALSE );
|
---|
| 80 | emit angleChanged( ang );
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | void <a name="f123"></a>CannonField::setForce( int newton )
|
---|
| 85 | {
|
---|
| 86 | if ( newton < 0 )
|
---|
| 87 | newton = 0;
|
---|
| 88 | if ( f == newton )
|
---|
| 89 | return;
|
---|
| 90 | f = newton;
|
---|
| 91 | emit forceChanged( f );
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | void <a name="f124"></a>CannonField::shoot()
|
---|
| 96 | {
|
---|
| 97 | if ( isShooting() )
|
---|
| 98 | return;
|
---|
| 99 | timerCount = 0;
|
---|
| 100 | shoot_ang = ang;
|
---|
| 101 | shoot_f = f;
|
---|
| 102 | autoShootTimer-><a href="qtimer.html#start">start</a>( 50 );
|
---|
| 103 | emit canShoot( FALSE );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | void <a name="f125"></a>CannonField::newTarget()
|
---|
| 108 | {
|
---|
| 109 | static bool first_time = TRUE;
|
---|
| 110 | if ( first_time ) {
|
---|
| 111 | first_time = FALSE;
|
---|
| 112 | <a href="qtime.html">QTime</a> midnight( 0, 0, 0 );
|
---|
| 113 | srand( midnight.<a href="qtime.html#secsTo">secsTo</a>(QTime::<a href="qtime.html#currentTime">currentTime</a>()) );
|
---|
| 114 | }
|
---|
| 115 | <a href="qregion.html">QRegion</a> r( targetRect() );
|
---|
| 116 | target = QPoint( 200 + rand() % 190,
|
---|
| 117 | 10 + rand() % 255 );
|
---|
| 118 | <a href="qwidget.html#repaint">repaint</a>( r.<a href="qrect.html#unite">unite</a>( targetRect() ) );
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void <a name="f126"></a>CannonField::setGameOver()
|
---|
| 122 | {
|
---|
| 123 | if ( gameEnded )
|
---|
| 124 | return;
|
---|
| 125 | if ( isShooting() )
|
---|
| 126 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 127 | gameEnded = TRUE;
|
---|
| 128 | <a href="qwidget.html#repaint">repaint</a>();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void <a name="f127"></a>CannonField::restartGame()
|
---|
| 132 | {
|
---|
| 133 | if ( isShooting() )
|
---|
| 134 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 135 | gameEnded = FALSE;
|
---|
| 136 | <a href="qwidget.html#repaint">repaint</a>();
|
---|
| 137 | emit canShoot( TRUE );
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void <a name="f128"></a>CannonField::moveShot()
|
---|
| 141 | {
|
---|
| 142 | <a href="qregion.html">QRegion</a> r( shotRect() );
|
---|
| 143 | timerCount++;
|
---|
| 144 |
|
---|
| 145 | <a href="qrect.html">QRect</a> shotR = shotRect();
|
---|
| 146 |
|
---|
| 147 | if ( shotR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) ) {
|
---|
| 148 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 149 | emit hit();
|
---|
| 150 | emit canShoot( TRUE );
|
---|
| 151 | } else if ( shotR.<a href="qrect.html#x">x</a>() > width() || shotR.<a href="qrect.html#y">y</a>() > height() ||
|
---|
| 152 | shotR.<a href="qrect.html#intersects">intersects</a>(barrierRect()) ) {
|
---|
| 153 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
| 154 | emit missed();
|
---|
| 155 | emit canShoot( TRUE );
|
---|
| 156 | } else {
|
---|
| 157 | r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) );
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | <a href="qwidget.html#repaint">repaint</a>( r );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | void CannonField::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
|
---|
| 165 | {
|
---|
| 166 | if ( e-><a href="qmouseevent.html#button">button</a>() != LeftButton )
|
---|
| 167 | return;
|
---|
| 168 | if ( barrelHit( e-><a href="qmouseevent.html#pos">pos</a>() ) )
|
---|
| 169 | barrelPressed = TRUE;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | void CannonField::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
|
---|
| 174 | {
|
---|
| 175 | if ( !barrelPressed )
|
---|
| 176 | return;
|
---|
| 177 | <a href="qpoint.html">QPoint</a> pnt = e-><a href="qmouseevent.html#pos">pos</a>();
|
---|
| 178 | if ( pnt.<a href="qpoint.html#x">x</a>() <= 0 )
|
---|
| 179 | pnt.<a href="qpoint.html#setX">setX</a>( 1 );
|
---|
| 180 | if ( pnt.<a href="qpoint.html#y">y</a>() >= <a href="qwidget.html#height">height</a>() )
|
---|
| 181 | pnt.<a href="qpoint.html#setY">setY</a>( <a href="qwidget.html#height">height</a>() - 1 );
|
---|
| 182 | double rad = atan(((double)<a href="qwidget.html#rect">rect</a>().bottom()-pnt.<a href="qpoint.html#y">y</a>())/pnt.<a href="qpoint.html#x">x</a>());
|
---|
| 183 | setAngle( qRound ( rad*180/3.14159265 ) );
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | void CannonField::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
|
---|
| 188 | {
|
---|
| 189 | if ( e-><a href="qmouseevent.html#button">button</a>() == LeftButton )
|
---|
| 190 | barrelPressed = FALSE;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
|
---|
| 195 | {
|
---|
| 196 | <a href="qrect.html">QRect</a> updateR = e-><a href="qpaintevent.html#rect">rect</a>();
|
---|
| 197 | <a href="qpainter.html">QPainter</a> p( this );
|
---|
| 198 |
|
---|
| 199 | if ( gameEnded ) {
|
---|
| 200 | p.<a href="qpainter.html#setPen">setPen</a>( black );
|
---|
| 201 | p.<a href="qpainter.html#setFont">setFont</a>( QFont( "Courier", 48, QFont::Bold ) );
|
---|
| 202 | p.<a href="qpainter.html#drawText">drawText</a>( <a href="qwidget.html#rect">rect</a>(), AlignCenter, "Game Over" );
|
---|
| 203 | }
|
---|
| 204 | if ( updateR.<a href="qrect.html#intersects">intersects</a>( cannonRect() ) )
|
---|
| 205 | paintCannon( &p );
|
---|
| 206 | if ( updateR.<a href="qrect.html#intersects">intersects</a>( barrierRect() ) )
|
---|
| 207 | paintBarrier( &p );
|
---|
| 208 | if ( isShooting() && updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) )
|
---|
| 209 | paintShot( &p );
|
---|
| 210 | if ( !gameEnded && updateR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) )
|
---|
| 211 | paintTarget( &p );
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void <a name="f129"></a>CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 215 | {
|
---|
| 216 | p-><a href="qpainter.html#setBrush">setBrush</a>( black );
|
---|
| 217 | p-><a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
| 218 | p-><a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | void <a name="f130"></a>CannonField::paintTarget( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 223 | {
|
---|
| 224 | p-><a href="qpainter.html#setBrush">setBrush</a>( red );
|
---|
| 225 | p-><a href="qpainter.html#setPen">setPen</a>( black );
|
---|
| 226 | p-><a href="qpainter.html#drawRect">drawRect</a>( targetRect() );
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | void <a name="f131"></a>CannonField::paintBarrier( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 230 | {
|
---|
| 231 | p-><a href="qpainter.html#setBrush">setBrush</a>( yellow );
|
---|
| 232 | p-><a href="qpainter.html#setPen">setPen</a>( black );
|
---|
| 233 | p-><a href="qpainter.html#drawRect">drawRect</a>( barrierRect() );
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | const <a href="qrect.html">QRect</a> barrelRect(33, -4, 15, 8);
|
---|
| 237 |
|
---|
| 238 | void <a name="f132"></a>CannonField::paintCannon( <a href="qpainter.html">QPainter</a> *p )
|
---|
| 239 | {
|
---|
| 240 | <a href="qrect.html">QRect</a> cr = cannonRect();
|
---|
| 241 | <a href="qpixmap.html">QPixmap</a> pix( cr.<a href="qrect.html#size">size</a>() );
|
---|
| 242 | pix.<a href="qpixmap.html#fill">fill</a>( this, cr.<a href="qrect.html#topLeft">topLeft</a>() );
|
---|
| 243 |
|
---|
| 244 | <a href="qpainter.html">QPainter</a> tmp( &pix );
|
---|
| 245 | tmp.<a href="qpainter.html#setBrush">setBrush</a>( blue );
|
---|
| 246 | tmp.<a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
| 247 |
|
---|
| 248 | tmp.<a href="qpainter.html#translate">translate</a>( 0, pix.<a href="qpixmap.html#height">height</a>() - 1 );
|
---|
| 249 | tmp.<a href="qpainter.html#drawPie">drawPie</a>( QRect( -35,-35, 70, 70 ), 0, 90*16 );
|
---|
| 250 | tmp.<a href="qpainter.html#rotate">rotate</a>( -ang );
|
---|
| 251 | tmp.<a href="qpainter.html#drawRect">drawRect</a>( barrelRect );
|
---|
| 252 | tmp.<a href="qpainter.html#end">end</a>();
|
---|
| 253 |
|
---|
| 254 | p-><a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | QRect <a name="f133"></a>CannonField::cannonRect() const
|
---|
| 259 | {
|
---|
| 260 | <a href="qrect.html">QRect</a> r( 0, 0, 50, 50 );
|
---|
| 261 | r.<a href="qrect.html#moveBottomLeft">moveBottomLeft</a>( <a href="qwidget.html#rect">rect</a>().bottomLeft() );
|
---|
| 262 | return r;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | QRect <a name="f134"></a>CannonField::shotRect() const
|
---|
| 267 | {
|
---|
| 268 | const double gravity = 4;
|
---|
| 269 |
|
---|
| 270 | double time = timerCount / 4.0;
|
---|
| 271 | double velocity = shoot_f;
|
---|
| 272 | double radians = shoot_ang*3.14159265/180;
|
---|
| 273 |
|
---|
| 274 | double velx = velocity*cos( radians );
|
---|
| 275 | double vely = velocity*sin( radians );
|
---|
| 276 | double x0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*cos(radians);
|
---|
| 277 | double y0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*sin(radians);
|
---|
| 278 | double x = x0 + velx*time;
|
---|
| 279 | double y = y0 + vely*time - 0.5*gravity*time*time;
|
---|
| 280 |
|
---|
| 281 | <a href="qrect.html">QRect</a> r = QRect( 0, 0, 6, 6 );
|
---|
| 282 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) );
|
---|
| 283 | return r;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 | QRect <a name="f135"></a>CannonField::targetRect() const
|
---|
| 288 | {
|
---|
| 289 | <a href="qrect.html">QRect</a> r( 0, 0, 20, 10 );
|
---|
| 290 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint(target.x(),height() - 1 - target.y()) );
|
---|
| 291 | return r;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
| 295 | QRect <a name="f136"></a>CannonField::barrierRect() const
|
---|
| 296 | {
|
---|
| 297 | return QRect( 145, height() - 100, 15, 100 );
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | bool <a name="f137"></a>CannonField::barrelHit( const <a href="qpoint.html">QPoint</a> &p ) const
|
---|
| 302 | {
|
---|
| 303 | <a href="qwmatrix.html">QWMatrix</a> mtx;
|
---|
| 304 | mtx.<a href="qwmatrix.html#translate">translate</a>( 0, height() - 1 );
|
---|
| 305 | mtx.<a href="qwmatrix.html#rotate">rotate</a>( -ang );
|
---|
| 306 | mtx = mtx.<a href="qwmatrix.html#invert">invert</a>();
|
---|
| 307 | return barrelRect.<a href="qrect.html#contains">contains</a>( mtx.<a href="qwmatrix.html#map">map</a>(p) );
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | bool <a name="f138"></a>CannonField::isShooting() const
|
---|
| 312 | {
|
---|
| 313 | return autoShootTimer-><a href="qtimer.html#isActive">isActive</a>();
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | QSize CannonField::<a href="qwidget.html#sizeHint">sizeHint</a>() const
|
---|
| 318 | {
|
---|
| 319 | return QSize( 400, 300 );
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 |
|
---|
| 323 | QSizePolicy CannonField::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const
|
---|
| 324 | {
|
---|
| 325 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
---|
| 326 | }
|
---|
| 327 | </pre><!-- eof -->
|
---|
| 328 | <p><address><hr><div align=center>
|
---|
| 329 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 330 | <td>Copyright © 2007
|
---|
| 331 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 332 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 333 | </table></div></address></body>
|
---|
| 334 | </html>
|
---|