source: trunk/doc/html/t13-cannon-cpp.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 9.6 KB
RevLine 
[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"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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 &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
43#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
44#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
45#include &lt;<a href="qdatetime-h.html">qdatetime.h</a>&gt;
46
47#include &lt;math.h&gt;
48#include &lt;stdlib.h&gt;
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
69void <a name="f95"></a>CannonField::setAngle( int degrees )
70{
71 if ( degrees &lt; 5 )
72 degrees = 5;
73 if ( degrees &gt; 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
83void <a name="f96"></a>CannonField::setForce( int newton )
84{
85 if ( newton &lt; 0 )
86 newton = 0;
87 if ( f == newton )
88 return;
89 f = newton;
90 emit forceChanged( f );
91}
92
93
94void <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-&gt;<a href="qtimer.html#start">start</a>( 50 );
102 emit canShoot( FALSE );
103}
104
105
106void <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
120void <a name="f99"></a>CannonField::setGameOver()
121{
122 if ( gameEnded )
123 return;
124 if ( isShooting() )
125 autoShootTimer-&gt;<a href="qtimer.html#stop">stop</a>();
126 gameEnded = TRUE;
127 <a href="qwidget.html#repaint">repaint</a>();
128}
129
130void <a name="f100"></a>CannonField::restartGame()
131{
132 if ( isShooting() )
133 autoShootTimer-&gt;<a href="qtimer.html#stop">stop</a>();
134 gameEnded = FALSE;
135 <a href="qwidget.html#repaint">repaint</a>();
136 emit canShoot( TRUE );
137}
138
139void <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-&gt;<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>() &gt; width() || shotR.<a href="qrect.html#y">y</a>() &gt; height() ) {
151 autoShootTimer-&gt;<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
162void 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-&gt;<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( &amp;p );
174 if ( isShooting() &amp;&amp; updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) )
175 paintShot( &amp;p );
176 if ( !gameEnded &amp;&amp; updateR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) )
177 paintTarget( &amp;p );
178}
179
180
181void <a name="f102"></a>CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p )
182{
183 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( black );
184 p-&gt;<a href="qpainter.html#setPen">setPen</a>( NoPen );
185 p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
186}
187
188
189void <a name="f103"></a>CannonField::paintTarget( <a href="qpainter.html">QPainter</a> *p )
190{
191 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( red );
192 p-&gt;<a href="qpainter.html#setPen">setPen</a>( black );
193 p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( targetRect() );
194}
195
196
197const <a href="qrect.html">QRect</a> barrelRect(33, -4, 15, 8);
198
199void <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( &amp;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-&gt;<a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
216}
217
218
219QRect <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
227QRect <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
248QRect <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
256bool <a name="f108"></a>CannonField::isShooting() const
257{
258 return autoShootTimer-&gt;<a href="qtimer.html#isActive">isActive</a>();
259}
260
261
262QSizePolicy 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 &copy; 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>
Note: See TracBrowser for help on using the repository browser.