author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/parashoot/interface.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/games/parashoot/interface.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/parashoot/interface.cpp | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/noncore/games/parashoot/interface.cpp b/noncore/games/parashoot/interface.cpp new file mode 100644 index 0000000..84e5e60 --- a/dev/null +++ b/noncore/games/parashoot/interface.cpp | |||
@@ -0,0 +1,247 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "interface.h" | ||
22 | #include "man.h" | ||
23 | |||
24 | #include <qpe/resource.h> | ||
25 | |||
26 | #include <qlabel.h> | ||
27 | #include <qmessagebox.h> | ||
28 | #include <qapplication.h> | ||
29 | #include <qstyle.h> | ||
30 | #include <qpe/qpetoolbar.h> | ||
31 | #include <qtoolbutton.h> | ||
32 | |||
33 | ParaShoot::ParaShoot(QWidget* parent, const char* name, WFlags f) : | ||
34 | QMainWindow(parent,name,f), | ||
35 | canvas(232, 258), | ||
36 | fanfare("level_up"), | ||
37 | score(0) | ||
38 | { | ||
39 | canvas.setAdvancePeriod(80); | ||
40 | QPixmap bg = Resource::loadPixmap("parashoot/sky"); | ||
41 | canvas.setBackgroundPixmap(bg); | ||
42 | |||
43 | pb = new QCanvasView(&canvas, this); | ||
44 | pb->setFocus(); | ||
45 | |||
46 | setToolBarsMovable( FALSE ); | ||
47 | |||
48 | QPEToolBar* toolbar = new QPEToolBar(this); | ||
49 | toolbar->setHorizontalStretchable( TRUE ); | ||
50 | |||
51 | setCaption( tr("ParaShoot") ); | ||
52 | QPixmap newicon = Resource::loadPixmap("parashoot/manicon"); | ||
53 | setIcon(newicon); | ||
54 | new QToolButton(newicon, tr("New Game"), 0, | ||
55 | this, SLOT(newGame()), toolbar, "New Game"); | ||
56 | |||
57 | levelscore = new QLabel(toolbar); | ||
58 | levelscore->setBackgroundMode( PaletteButton ); | ||
59 | levelscore->setAlignment( AlignRight | AlignVCenter | ExpandTabs ); | ||
60 | toolbar->setStretchableWidget( levelscore ); | ||
61 | showScore(0,0); | ||
62 | |||
63 | setCentralWidget(pb); | ||
64 | |||
65 | autoDropTimer = new QTimer(this); | ||
66 | connect (autoDropTimer, SIGNAL(timeout()), this, SLOT(play()) ); | ||
67 | |||
68 | pauseTimer = new QTimer(this); | ||
69 | connect(pauseTimer, SIGNAL(timeout()), this, SLOT(wait()) ); | ||
70 | |||
71 | setFocusPolicy(StrongFocus); | ||
72 | |||
73 | newGame(); | ||
74 | } | ||
75 | |||
76 | |||
77 | void ParaShoot::resizeEvent(QResizeEvent *) | ||
78 | { | ||
79 | QSize s = centralWidget()->size(); | ||
80 | int fw = style().defaultFrameWidth(); | ||
81 | canvas.resize( s.width() - fw - 2, s.height() - fw - 2); | ||
82 | } | ||
83 | |||
84 | |||
85 | void ParaShoot::showScore( int score, int level ) | ||
86 | { | ||
87 | levelscore->setText(tr(" Level: %1 Score: %2 ").arg(score).arg(level) ); | ||
88 | } | ||
89 | |||
90 | |||
91 | void ParaShoot::newGame() | ||
92 | { | ||
93 | clear(); | ||
94 | if (pauseTimer->isActive()) | ||
95 | pauseTimer->stop(); | ||
96 | clear(); | ||
97 | Man::setManCount(0); | ||
98 | score = 0; | ||
99 | Bullet::setShotCount(0); | ||
100 | Bullet::setNobullets(0); | ||
101 | nomen = 2; | ||
102 | Bullet::setLimit(nomen); | ||
103 | level = 0; | ||
104 | updatespeed = 80; | ||
105 | showScore(0,0); | ||
106 | gamestopped = false; | ||
107 | Helicopter::deleteAll(); | ||
108 | waitover = true; | ||
109 | base = new Base(&canvas); | ||
110 | cannon = new Cannon(&canvas); | ||
111 | connect( cannon, SIGNAL(score(int)), this, SLOT(increaseScore(int))); | ||
112 | autoDropTimer->start(100); | ||
113 | } | ||
114 | |||
115 | |||
116 | void ParaShoot::clear() | ||
117 | { | ||
118 | autoDropTimer->stop(); | ||
119 | // QCanvasItem* item; | ||
120 | QCanvasItemList l = canvas.allItems(); | ||
121 | for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { | ||
122 | delete *it; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | void ParaShoot::gameOver() | ||
127 | { | ||
128 | QCanvasItem* item; | ||
129 | QCanvasItemList l = canvas.allItems(); | ||
130 | for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { | ||
131 | item = *it; | ||
132 | if ((item->rtti()==1500) || (item->rtti()==1600) || item->rtti()==1900) | ||
133 | item->setAnimated(false); | ||
134 | } | ||
135 | autoDropTimer->stop(); | ||
136 | Helicopter::silenceAll(); | ||
137 | |||
138 | int shots = Bullet::getShotCount(); | ||
139 | |||
140 | int shotsFired = cannon->shotsFired(); | ||
141 | if ( shotsFired == 0 ) | ||
142 | shotsFired = 1; | ||
143 | QCanvasText* gameover = new QCanvasText( | ||
144 | tr( " GAME OVER!\n" | ||
145 | " Your Score: %1\n" | ||
146 | " Parachuters Killed: %2\n" | ||
147 | " Accuracy: %3% " ).arg(score).arg(shots).arg(shots * 100 / shotsFired ), | ||
148 | &canvas); | ||
149 | gameover->setColor(red); | ||
150 | gameover->setFont( QFont("times", 18, QFont::Bold) ); | ||
151 | gameover->move(canvas.width()/2 -110, canvas.height()/2 -50); | ||
152 | gameover->setZ(500); | ||
153 | gameover->show(); | ||
154 | gamestopped = true; | ||
155 | waitover = false; | ||
156 | pauseTimer->start(3000); | ||
157 | } | ||
158 | |||
159 | void ParaShoot::wait() | ||
160 | { | ||
161 | waitover = true; | ||
162 | pauseTimer->stop(); | ||
163 | } | ||
164 | |||
165 | void ParaShoot::play() | ||
166 | { | ||
167 | if (Man::getManCount() < nomen ) { | ||
168 | new Man(&canvas); | ||
169 | } | ||
170 | if (Base::baseDestroyed()) { | ||
171 | gameOver(); | ||
172 | return; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | void ParaShoot::increaseScore(int x) | ||
177 | { | ||
178 | score += x; | ||
179 | if ( score / 150 != (score-x) / 150 ) | ||
180 | levelUp(); | ||
181 | showScore(level,score); | ||
182 | } | ||
183 | |||
184 | void ParaShoot::levelUp() | ||
185 | { | ||
186 | level++; | ||
187 | int stage = level % 3; | ||
188 | switch(stage) { | ||
189 | case 0: | ||
190 | nomen++; | ||
191 | Bullet::setLimit(nomen); | ||
192 | fanfare.play(); | ||
193 | break; | ||
194 | case 1: | ||
195 | new Helicopter(&canvas); | ||
196 | break; | ||
197 | case 2: | ||
198 | moveFaster(); | ||
199 | fanfare.play(); | ||
200 | break; | ||
201 | default: return; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void ParaShoot::moveFaster() | ||
206 | { | ||
207 | if (updatespeed > 50) | ||
208 | updatespeed = updatespeed-5; | ||
209 | else | ||
210 | updatespeed = updatespeed-3; | ||
211 | canvas.setAdvancePeriod(updatespeed); | ||
212 | } | ||
213 | |||
214 | void ParaShoot::keyPressEvent(QKeyEvent* event) | ||
215 | { | ||
216 | if (gamestopped) { | ||
217 | if (waitover) | ||
218 | newGame(); | ||
219 | else | ||
220 | return; | ||
221 | } else { | ||
222 | switch(event->key()) { | ||
223 | case Key_Up: | ||
224 | case Key_F1: | ||
225 | case Key_F9: | ||
226 | case Key_Space: | ||
227 | cannon->shoot(); | ||
228 | break; | ||
229 | case Key_Left: | ||
230 | cannon->pointCannon(Cannon::Left); | ||
231 | lastcannonkey=Key_Left; | ||
232 | break; | ||
233 | case Key_Right: | ||
234 | cannon->pointCannon(Cannon::Right); | ||
235 | lastcannonkey=Key_Right; | ||
236 | break; | ||
237 | default: | ||
238 | return; | ||
239 | } | ||
240 | } | ||
241 | } | ||
242 | |||
243 | void ParaShoot::keyReleaseEvent(QKeyEvent* event) | ||
244 | { | ||
245 | if ( lastcannonkey == event->key() ) | ||
246 | cannon->pointCannon(Cannon::NoDir); | ||
247 | } | ||