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:1708 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>t12/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>t12/cannon.cpp Example File</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <pre>/****************************************************************
|
---|
36 | **
|
---|
37 | ** Implementation CannonField class, Qt tutorial 12
|
---|
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="f74"></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 | <a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) );
|
---|
64 | newTarget();
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | void <a name="f75"></a>CannonField::setAngle( int degrees )
|
---|
69 | {
|
---|
70 | if ( degrees < 5 )
|
---|
71 | degrees = 5;
|
---|
72 | if ( degrees > 70 )
|
---|
73 | degrees = 70;
|
---|
74 | if ( ang == degrees )
|
---|
75 | return;
|
---|
76 | ang = degrees;
|
---|
77 | <a href="qwidget.html#repaint">repaint</a>( cannonRect(), FALSE );
|
---|
78 | emit angleChanged( ang );
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | void <a name="f76"></a>CannonField::setForce( int newton )
|
---|
83 | {
|
---|
84 | if ( newton < 0 )
|
---|
85 | newton = 0;
|
---|
86 | if ( f == newton )
|
---|
87 | return;
|
---|
88 | f = newton;
|
---|
89 | emit forceChanged( f );
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | void <a name="f77"></a>CannonField::shoot()
|
---|
94 | {
|
---|
95 | if ( autoShootTimer-><a href="qtimer.html#isActive">isActive</a>() )
|
---|
96 | return;
|
---|
97 | timerCount = 0;
|
---|
98 | shoot_ang = ang;
|
---|
99 | shoot_f = f;
|
---|
100 | autoShootTimer-><a href="qtimer.html#start">start</a>( 50 );
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void <a name="f78"></a>CannonField::newTarget()
|
---|
105 | {
|
---|
106 | static bool first_time = TRUE;
|
---|
107 | if ( first_time ) {
|
---|
108 | first_time = FALSE;
|
---|
109 | <a href="qtime.html">QTime</a> midnight( 0, 0, 0 );
|
---|
110 | srand( midnight.<a href="qtime.html#secsTo">secsTo</a>(QTime::<a href="qtime.html#currentTime">currentTime</a>()) );
|
---|
111 | }
|
---|
112 | <a href="qregion.html">QRegion</a> r( targetRect() );
|
---|
113 | target = QPoint( 200 + rand() % 190,
|
---|
114 | 10 + rand() % 255 );
|
---|
115 | <a href="qwidget.html#repaint">repaint</a>( r.<a href="qrect.html#unite">unite</a>( targetRect() ) );
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | void <a name="f79"></a>CannonField::moveShot()
|
---|
120 | {
|
---|
121 | <a href="qregion.html">QRegion</a> r( shotRect() );
|
---|
122 | timerCount++;
|
---|
123 |
|
---|
124 | <a href="qrect.html">QRect</a> shotR = shotRect();
|
---|
125 |
|
---|
126 | if ( shotR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) ) {
|
---|
127 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
128 | emit hit();
|
---|
129 | } else if ( shotR.<a href="qrect.html#x">x</a>() > width() || shotR.<a href="qrect.html#y">y</a>() > height() ) {
|
---|
130 | autoShootTimer-><a href="qtimer.html#stop">stop</a>();
|
---|
131 | emit missed();
|
---|
132 | } else {
|
---|
133 | r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) );
|
---|
134 | }
|
---|
135 |
|
---|
136 | <a href="qwidget.html#repaint">repaint</a>( r );
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
|
---|
141 | {
|
---|
142 | <a href="qrect.html">QRect</a> updateR = e-><a href="qpaintevent.html#rect">rect</a>();
|
---|
143 | <a href="qpainter.html">QPainter</a> p( this );
|
---|
144 |
|
---|
145 | if ( updateR.<a href="qrect.html#intersects">intersects</a>( cannonRect() ) )
|
---|
146 | paintCannon( &p );
|
---|
147 | if ( autoShootTimer-><a href="qtimer.html#isActive">isActive</a>() &&
|
---|
148 | updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) )
|
---|
149 | paintShot( &p );
|
---|
150 | if ( updateR.<a href="qrect.html#intersects">intersects</a>( targetRect() ) )
|
---|
151 | paintTarget( &p );
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void <a name="f80"></a>CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p )
|
---|
156 | {
|
---|
157 | p-><a href="qpainter.html#setBrush">setBrush</a>( black );
|
---|
158 | p-><a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
159 | p-><a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | void <a name="f81"></a>CannonField::paintTarget( <a href="qpainter.html">QPainter</a> *p )
|
---|
164 | {
|
---|
165 | p-><a href="qpainter.html#setBrush">setBrush</a>( red );
|
---|
166 | p-><a href="qpainter.html#setPen">setPen</a>( black );
|
---|
167 | p-><a href="qpainter.html#drawRect">drawRect</a>( targetRect() );
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | const <a href="qrect.html">QRect</a> barrelRect(33, -4, 15, 8);
|
---|
172 |
|
---|
173 | void <a name="f82"></a>CannonField::paintCannon( <a href="qpainter.html">QPainter</a> *p )
|
---|
174 | {
|
---|
175 | <a href="qrect.html">QRect</a> cr = cannonRect();
|
---|
176 | <a href="qpixmap.html">QPixmap</a> pix( cr.<a href="qrect.html#size">size</a>() );
|
---|
177 | pix.<a href="qpixmap.html#fill">fill</a>( this, cr.<a href="qrect.html#topLeft">topLeft</a>() );
|
---|
178 |
|
---|
179 | <a href="qpainter.html">QPainter</a> tmp( &pix );
|
---|
180 | tmp.<a href="qpainter.html#setBrush">setBrush</a>( blue );
|
---|
181 | tmp.<a href="qpainter.html#setPen">setPen</a>( NoPen );
|
---|
182 |
|
---|
183 | tmp.<a href="qpainter.html#translate">translate</a>( 0, pix.<a href="qpixmap.html#height">height</a>() - 1 );
|
---|
184 | tmp.<a href="qpainter.html#drawPie">drawPie</a>( QRect( -35,-35, 70, 70 ), 0, 90*16 );
|
---|
185 | tmp.<a href="qpainter.html#rotate">rotate</a>( -ang );
|
---|
186 | tmp.<a href="qpainter.html#drawRect">drawRect</a>( barrelRect );
|
---|
187 | tmp.<a href="qpainter.html#end">end</a>();
|
---|
188 |
|
---|
189 | p-><a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | QRect <a name="f83"></a>CannonField::cannonRect() const
|
---|
194 | {
|
---|
195 | <a href="qrect.html">QRect</a> r( 0, 0, 50, 50 );
|
---|
196 | r.<a href="qrect.html#moveBottomLeft">moveBottomLeft</a>( <a href="qwidget.html#rect">rect</a>().bottomLeft() );
|
---|
197 | return r;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | QRect <a name="f84"></a>CannonField::shotRect() const
|
---|
202 | {
|
---|
203 | const double gravity = 4;
|
---|
204 |
|
---|
205 | double time = timerCount / 4.0;
|
---|
206 | double velocity = shoot_f;
|
---|
207 | double radians = shoot_ang*3.14159265/180;
|
---|
208 |
|
---|
209 | double velx = velocity*cos( radians );
|
---|
210 | double vely = velocity*sin( radians );
|
---|
211 | double x0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*cos(radians);
|
---|
212 | double y0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*sin(radians);
|
---|
213 | double x = x0 + velx*time;
|
---|
214 | double y = y0 + vely*time - 0.5*gravity*time*time;
|
---|
215 |
|
---|
216 | <a href="qrect.html">QRect</a> r = QRect( 0, 0, 6, 6 );
|
---|
217 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) );
|
---|
218 | return r;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | QRect <a name="f85"></a>CannonField::targetRect() const
|
---|
223 | {
|
---|
224 | <a href="qrect.html">QRect</a> r( 0, 0, 20, 10 );
|
---|
225 | r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint(target.x(),height() - 1 - target.y()) );
|
---|
226 | return r;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | QSizePolicy CannonField::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const
|
---|
231 | {
|
---|
232 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
---|
233 | }
|
---|
234 | </pre><!-- eof -->
|
---|
235 | <p><address><hr><div align=center>
|
---|
236 | <table width=100% cellspacing=0 border=0><tr>
|
---|
237 | <td>Copyright © 2007
|
---|
238 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
239 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
240 | </table></div></address></body>
|
---|
241 | </html>
|
---|