|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /* |
|
43 * KAsteroids - Copyright (c) Martin R. Jones 1997 |
|
44 * |
|
45 * Part of the KDE project |
|
46 */ |
|
47 // --- toplevel.cpp --- |
|
48 #include <q3accel.h> |
|
49 #include <qlabel.h> |
|
50 #include <qlayout.h> |
|
51 #include <qlcdnumber.h> |
|
52 #include <qpushbutton.h> |
|
53 |
|
54 #include <qapplication.h> |
|
55 //Added by qt3to4: |
|
56 #include <Q3HBoxLayout> |
|
57 #include <QShowEvent> |
|
58 #include <Q3Frame> |
|
59 #include <QPixmap> |
|
60 #include <QHideEvent> |
|
61 #include <QKeyEvent> |
|
62 #include <Q3VBoxLayout> |
|
63 |
|
64 #include "toplevel.h" |
|
65 #include "ledmeter.h" |
|
66 |
|
67 |
|
68 #define SB_SCORE 1 |
|
69 #define SB_LEVEL 2 |
|
70 #define SB_SHIPS 3 |
|
71 |
|
72 struct SLevel |
|
73 { |
|
74 int nrocks; |
|
75 double rockSpeed; |
|
76 }; |
|
77 |
|
78 #define MAX_LEVELS 16 |
|
79 |
|
80 SLevel levels[MAX_LEVELS] = |
|
81 { |
|
82 { 1, 0.4 }, |
|
83 { 1, 0.6 }, |
|
84 { 2, 0.5 }, |
|
85 { 2, 0.7 }, |
|
86 { 2, 0.8 }, |
|
87 { 3, 0.6 }, |
|
88 { 3, 0.7 }, |
|
89 { 3, 0.8 }, |
|
90 { 4, 0.6 }, |
|
91 { 4, 0.7 }, |
|
92 { 4, 0.8 }, |
|
93 { 5, 0.7 }, |
|
94 { 5, 0.8 }, |
|
95 { 5, 0.9 }, |
|
96 { 5, 1.0 } |
|
97 }; |
|
98 |
|
99 const char *soundEvents[] = |
|
100 { |
|
101 "ShipDestroyed", |
|
102 "RockDestroyed", |
|
103 0 |
|
104 }; |
|
105 |
|
106 const char *soundDefaults[] = |
|
107 { |
|
108 "Explosion.wav", |
|
109 "ploop.wav", |
|
110 0 |
|
111 }; |
|
112 |
|
113 |
|
114 KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name ) |
|
115 : Q3MainWindow( parent, name, 0 ) |
|
116 { |
|
117 QWidget *border = new QWidget( this ); |
|
118 border->setBackgroundColor( Qt::black ); |
|
119 setCentralWidget( border ); |
|
120 |
|
121 Q3VBoxLayout *borderLayout = new Q3VBoxLayout( border ); |
|
122 borderLayout->addStretch( 1 ); |
|
123 |
|
124 QWidget *mainWin = new QWidget( border ); |
|
125 mainWin->setFixedSize(640, 480); |
|
126 borderLayout->addWidget( mainWin, 0, Qt::AlignHCenter ); |
|
127 |
|
128 borderLayout->addStretch( 1 ); |
|
129 |
|
130 view = new KAsteroidsView( mainWin ); |
|
131 view->setFocusPolicy( Qt::StrongFocus ); |
|
132 connect( view, SIGNAL( shipKilled() ), SLOT( slotShipKilled() ) ); |
|
133 connect( view, SIGNAL( rockHit(int) ), SLOT( slotRockHit(int) ) ); |
|
134 connect( view, SIGNAL( rocksRemoved() ), SLOT( slotRocksRemoved() ) ); |
|
135 connect( view, SIGNAL( updateVitals() ), SLOT( slotUpdateVitals() ) ); |
|
136 |
|
137 Q3VBoxLayout *vb = new Q3VBoxLayout( mainWin ); |
|
138 Q3HBoxLayout *hb = new Q3HBoxLayout; |
|
139 Q3HBoxLayout *hbd = new Q3HBoxLayout; |
|
140 vb->addLayout( hb ); |
|
141 |
|
142 QFont labelFont( "helvetica", 24 ); |
|
143 QColorGroup grp( Qt::darkGreen, Qt::black, QColor( 128, 128, 128 ), |
|
144 QColor( 64, 64, 64 ), Qt::black, Qt::darkGreen, Qt::black ); |
|
145 QPalette pal( grp, grp, grp ); |
|
146 |
|
147 mainWin->setPalette( pal ); |
|
148 |
|
149 hb->addSpacing( 10 ); |
|
150 |
|
151 QLabel *label; |
|
152 label = new QLabel( tr("Score"), mainWin ); |
|
153 label->setFont( labelFont ); |
|
154 label->setPalette( pal ); |
|
155 label->setFixedWidth( label->sizeHint().width() ); |
|
156 hb->addWidget( label ); |
|
157 |
|
158 scoreLCD = new QLCDNumber( 6, mainWin ); |
|
159 scoreLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
160 scoreLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
161 scoreLCD->setFixedWidth( 150 ); |
|
162 scoreLCD->setPalette( pal ); |
|
163 hb->addWidget( scoreLCD ); |
|
164 hb->addStretch( 10 ); |
|
165 |
|
166 label = new QLabel( tr("Level"), mainWin ); |
|
167 label->setFont( labelFont ); |
|
168 label->setPalette( pal ); |
|
169 label->setFixedWidth( label->sizeHint().width() ); |
|
170 hb->addWidget( label ); |
|
171 |
|
172 levelLCD = new QLCDNumber( 2, mainWin ); |
|
173 levelLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
174 levelLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
175 levelLCD->setFixedWidth( 70 ); |
|
176 levelLCD->setPalette( pal ); |
|
177 hb->addWidget( levelLCD ); |
|
178 hb->addStretch( 10 ); |
|
179 |
|
180 label = new QLabel( tr("Ships"), mainWin ); |
|
181 label->setFont( labelFont ); |
|
182 label->setFixedWidth( label->sizeHint().width() ); |
|
183 label->setPalette( pal ); |
|
184 hb->addWidget( label ); |
|
185 |
|
186 shipsLCD = new QLCDNumber( 1, mainWin ); |
|
187 shipsLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
188 shipsLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
189 shipsLCD->setFixedWidth( 40 ); |
|
190 shipsLCD->setPalette( pal ); |
|
191 hb->addWidget( shipsLCD ); |
|
192 |
|
193 hb->addStrut( 30 ); |
|
194 |
|
195 vb->addWidget( view, 10 ); |
|
196 |
|
197 // -- bottom layout: |
|
198 vb->addLayout( hbd ); |
|
199 |
|
200 QFont smallFont( "helvetica", 14 ); |
|
201 hbd->addSpacing( 10 ); |
|
202 |
|
203 QString sprites_prefix = ":/trolltech/examples/graphicsview/portedasteroids/sprites/"; |
|
204 /* |
|
205 label = new QLabel( tr( "T" ), mainWin ); |
|
206 label->setFont( smallFont ); |
|
207 label->setFixedWidth( label->sizeHint().width() ); |
|
208 label->setPalette( pal ); |
|
209 hbd->addWidget( label ); |
|
210 |
|
211 teleportsLCD = new QLCDNumber( 1, mainWin ); |
|
212 teleportsLCD->setFrameStyle( QFrame::NoFrame ); |
|
213 teleportsLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
214 teleportsLCD->setPalette( pal ); |
|
215 teleportsLCD->setFixedHeight( 20 ); |
|
216 hbd->addWidget( teleportsLCD ); |
|
217 |
|
218 hbd->addSpacing( 10 ); |
|
219 */ |
|
220 QPixmap pm( sprites_prefix + "powerups/brake.png" ); |
|
221 label = new QLabel( mainWin ); |
|
222 label->setPixmap( pm ); |
|
223 label->setFixedWidth( label->sizeHint().width() ); |
|
224 label->setPalette( pal ); |
|
225 hbd->addWidget( label ); |
|
226 |
|
227 brakesLCD = new QLCDNumber( 1, mainWin ); |
|
228 brakesLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
229 brakesLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
230 brakesLCD->setPalette( pal ); |
|
231 brakesLCD->setFixedHeight( 20 ); |
|
232 hbd->addWidget( brakesLCD ); |
|
233 |
|
234 hbd->addSpacing( 10 ); |
|
235 |
|
236 pm.load( sprites_prefix + "powerups/shield.png" ); |
|
237 label = new QLabel( mainWin ); |
|
238 label->setPixmap( pm ); |
|
239 label->setFixedWidth( label->sizeHint().width() ); |
|
240 label->setPalette( pal ); |
|
241 hbd->addWidget( label ); |
|
242 |
|
243 shieldLCD = new QLCDNumber( 1, mainWin ); |
|
244 shieldLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
245 shieldLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
246 shieldLCD->setPalette( pal ); |
|
247 shieldLCD->setFixedHeight( 20 ); |
|
248 hbd->addWidget( shieldLCD ); |
|
249 |
|
250 hbd->addSpacing( 10 ); |
|
251 |
|
252 pm.load( sprites_prefix + "powerups/shoot.png" ); |
|
253 label = new QLabel( mainWin ); |
|
254 label->setPixmap( pm ); |
|
255 label->setFixedWidth( label->sizeHint().width() ); |
|
256 label->setPalette( pal ); |
|
257 hbd->addWidget( label ); |
|
258 |
|
259 shootLCD = new QLCDNumber( 1, mainWin ); |
|
260 shootLCD->setFrameStyle( Q3Frame::NoFrame ); |
|
261 shootLCD->setSegmentStyle( QLCDNumber::Flat ); |
|
262 shootLCD->setPalette( pal ); |
|
263 shootLCD->setFixedHeight( 20 ); |
|
264 hbd->addWidget( shootLCD ); |
|
265 |
|
266 hbd->addStretch( 1 ); |
|
267 |
|
268 label = new QLabel( tr( "Fuel" ), mainWin ); |
|
269 label->setFont( smallFont ); |
|
270 label->setFixedWidth( label->sizeHint().width() + 10 ); |
|
271 label->setPalette( pal ); |
|
272 hbd->addWidget( label ); |
|
273 |
|
274 powerMeter = new KALedMeter( mainWin ); |
|
275 powerMeter->setFrameStyle( Q3Frame::Box | Q3Frame::Plain ); |
|
276 powerMeter->setRange( MAX_POWER_LEVEL ); |
|
277 powerMeter->addColorRange( 10, Qt::darkRed ); |
|
278 powerMeter->addColorRange( 20, QColor(160, 96, 0) ); |
|
279 powerMeter->addColorRange( 70, Qt::darkGreen ); |
|
280 powerMeter->setCount( 40 ); |
|
281 powerMeter->setPalette( pal ); |
|
282 powerMeter->setFixedSize( 200, 12 ); |
|
283 hbd->addWidget( powerMeter ); |
|
284 |
|
285 shipsRemain = 3; |
|
286 showHiscores = FALSE; |
|
287 |
|
288 actions.insert( Qt::Key_Up, Thrust ); |
|
289 actions.insert( Qt::Key_Left, RotateLeft ); |
|
290 actions.insert( Qt::Key_Right, RotateRight ); |
|
291 actions.insert( Qt::Key_Space, Shoot ); |
|
292 actions.insert( Qt::Key_Z, Teleport ); |
|
293 actions.insert( Qt::Key_X, Brake ); |
|
294 actions.insert( Qt::Key_S, Shield ); |
|
295 actions.insert( Qt::Key_P, Pause ); |
|
296 actions.insert( Qt::Key_L, Launch ); |
|
297 actions.insert( Qt::Key_N, NewGame ); |
|
298 |
|
299 view->showText( tr( "Press N to start playing" ), Qt::yellow ); |
|
300 } |
|
301 |
|
302 KAstTopLevel::~KAstTopLevel() |
|
303 { |
|
304 } |
|
305 |
|
306 void KAstTopLevel::playSound( const char * ) |
|
307 { |
|
308 } |
|
309 |
|
310 void KAstTopLevel::keyPressEvent( QKeyEvent *event ) |
|
311 { |
|
312 if ( event->isAutoRepeat() || !actions.contains( event->key() ) ) |
|
313 { |
|
314 event->ignore(); |
|
315 return; |
|
316 } |
|
317 |
|
318 Action a = actions[ event->key() ]; |
|
319 |
|
320 switch ( a ) |
|
321 { |
|
322 case RotateLeft: |
|
323 view->rotateLeft( TRUE ); |
|
324 break; |
|
325 |
|
326 case RotateRight: |
|
327 view->rotateRight( TRUE ); |
|
328 break; |
|
329 |
|
330 case Thrust: |
|
331 view->thrust( TRUE ); |
|
332 break; |
|
333 |
|
334 case Shoot: |
|
335 view->shoot( TRUE ); |
|
336 break; |
|
337 |
|
338 case Shield: |
|
339 view->setShield( TRUE ); |
|
340 break; |
|
341 |
|
342 case Teleport: |
|
343 view->teleport( TRUE ); |
|
344 break; |
|
345 |
|
346 case Brake: |
|
347 view->brake( TRUE ); |
|
348 break; |
|
349 |
|
350 default: |
|
351 event->ignore(); |
|
352 return; |
|
353 } |
|
354 event->accept(); |
|
355 } |
|
356 |
|
357 void KAstTopLevel::keyReleaseEvent( QKeyEvent *event ) |
|
358 { |
|
359 if ( event->isAutoRepeat() || !actions.contains( event->key() ) ) |
|
360 { |
|
361 event->ignore(); |
|
362 return; |
|
363 } |
|
364 |
|
365 Action a = actions[ event->key() ]; |
|
366 |
|
367 switch ( a ) |
|
368 { |
|
369 case RotateLeft: |
|
370 view->rotateLeft( FALSE ); |
|
371 break; |
|
372 |
|
373 case RotateRight: |
|
374 view->rotateRight( FALSE ); |
|
375 break; |
|
376 |
|
377 case Thrust: |
|
378 view->thrust( FALSE ); |
|
379 break; |
|
380 |
|
381 case Shoot: |
|
382 view->shoot( FALSE ); |
|
383 break; |
|
384 |
|
385 case Brake: |
|
386 view->brake( FALSE ); |
|
387 break; |
|
388 |
|
389 case Shield: |
|
390 view->setShield( FALSE ); |
|
391 break; |
|
392 |
|
393 case Teleport: |
|
394 view->teleport( FALSE ); |
|
395 break; |
|
396 |
|
397 case Launch: |
|
398 if ( waitShip ) |
|
399 { |
|
400 view->newShip(); |
|
401 waitShip = FALSE; |
|
402 view->hideText(); |
|
403 } |
|
404 else |
|
405 { |
|
406 event->ignore(); |
|
407 return; |
|
408 } |
|
409 break; |
|
410 |
|
411 case NewGame: |
|
412 slotNewGame(); |
|
413 break; |
|
414 /* |
|
415 case Pause: |
|
416 { |
|
417 view->pause( TRUE ); |
|
418 QMessageBox::information( this, |
|
419 tr("KAsteroids is paused"), |
|
420 tr("Paused") ); |
|
421 view->pause( FALSE ); |
|
422 } |
|
423 break; |
|
424 */ |
|
425 default: |
|
426 event->ignore(); |
|
427 return; |
|
428 } |
|
429 |
|
430 event->accept(); |
|
431 } |
|
432 |
|
433 void KAstTopLevel::showEvent( QShowEvent *e ) |
|
434 { |
|
435 Q3MainWindow::showEvent( e ); |
|
436 view->pause( FALSE ); |
|
437 view->setFocus(); |
|
438 } |
|
439 |
|
440 void KAstTopLevel::hideEvent( QHideEvent *e ) |
|
441 { |
|
442 Q3MainWindow::hideEvent( e ); |
|
443 view->pause( TRUE ); |
|
444 } |
|
445 |
|
446 void KAstTopLevel::slotNewGame() |
|
447 { |
|
448 score = 0; |
|
449 shipsRemain = SB_SHIPS; |
|
450 scoreLCD->display( 0 ); |
|
451 level = 0; |
|
452 levelLCD->display( level+1 ); |
|
453 shipsLCD->display( shipsRemain-1 ); |
|
454 view->newGame(); |
|
455 view->setRockSpeed( levels[0].rockSpeed ); |
|
456 view->addRocks( levels[0].nrocks ); |
|
457 // view->showText( tr( "Press L to launch." ), yellow ); |
|
458 view->newShip(); |
|
459 waitShip = FALSE; |
|
460 view->hideText(); |
|
461 isPaused = FALSE; |
|
462 } |
|
463 |
|
464 void KAstTopLevel::slotShipKilled() |
|
465 { |
|
466 shipsRemain--; |
|
467 shipsLCD->display( shipsRemain-1 ); |
|
468 |
|
469 playSound( "ShipDestroyed" ); |
|
470 |
|
471 if ( shipsRemain ) |
|
472 { |
|
473 waitShip = TRUE; |
|
474 view->showText( tr( "Ship Destroyed. Press L to launch."), Qt::yellow ); |
|
475 } |
|
476 else |
|
477 { |
|
478 view->showText( tr("Game Over!"), Qt::red ); |
|
479 view->endGame(); |
|
480 doStats(); |
|
481 // highscore->addEntry( score, level, showHiscores ); |
|
482 } |
|
483 } |
|
484 |
|
485 void KAstTopLevel::slotRockHit( int size ) |
|
486 { |
|
487 switch ( size ) |
|
488 { |
|
489 case 0: |
|
490 score += 10; |
|
491 break; |
|
492 |
|
493 case 1: |
|
494 score += 20; |
|
495 break; |
|
496 |
|
497 default: |
|
498 score += 40; |
|
499 } |
|
500 |
|
501 playSound( "RockDestroyed" ); |
|
502 |
|
503 scoreLCD->display( score ); |
|
504 } |
|
505 |
|
506 void KAstTopLevel::slotRocksRemoved() |
|
507 { |
|
508 level++; |
|
509 |
|
510 if ( level >= MAX_LEVELS ) |
|
511 level = MAX_LEVELS - 1; |
|
512 |
|
513 view->setRockSpeed( levels[level-1].rockSpeed ); |
|
514 view->addRocks( levels[level-1].nrocks ); |
|
515 |
|
516 levelLCD->display( level+1 ); |
|
517 } |
|
518 |
|
519 void KAstTopLevel::doStats() |
|
520 { |
|
521 QString r( "0.00" ); |
|
522 if ( view->shots() ) |
|
523 r = QString::number( (double)view->hits() / view->shots() * 100.0, |
|
524 'g', 2 ); |
|
525 |
|
526 /* multi-line text broken in Qt 3 |
|
527 QString s = tr( "Game Over\n\nShots fired:\t%1\n Hit:\t%2\n Missed:\t%3\nHit ratio:\t%4 %\n\nPress N for a new game" ) |
|
528 .arg(view->shots()).arg(view->hits()) |
|
529 .arg(view->shots() - view->hits()) |
|
530 .arg(r); |
|
531 */ |
|
532 |
|
533 view->showText( "Game Over. Press N for a new game.", Qt::yellow, FALSE ); |
|
534 } |
|
535 |
|
536 void KAstTopLevel::slotUpdateVitals() |
|
537 { |
|
538 brakesLCD->display( view->brakeCount() ); |
|
539 shieldLCD->display( view->shieldCount() ); |
|
540 shootLCD->display( view->shootCount() ); |
|
541 // teleportsLCD->display( view->teleportCount() ); |
|
542 powerMeter->setValue( view->power() ); |
|
543 } |