-rw-r--r-- | noncore/games/sfcave/opie-sfcave.control | 8 | ||||
-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 421 | ||||
-rw-r--r-- | noncore/games/sfcave/sfcave.h | 76 | ||||
-rw-r--r-- | noncore/games/sfcave/sfcave.pro | 9 |
4 files changed, 514 insertions, 0 deletions
diff --git a/noncore/games/sfcave/opie-sfcave.control b/noncore/games/sfcave/opie-sfcave.control new file mode 100644 index 0000000..c97baad --- a/dev/null +++ b/noncore/games/sfcave/opie-sfcave.control | |||
@@ -0,0 +1,8 @@ | |||
1 | Files: bin/sfcavee apps/Games/sfcave.desktop pics/sfcave | ||
2 | Priority: optional | ||
3 | Section: opie/games | ||
4 | Maintainer: Andy Qua <andy.qua@blueyonder.co.uk> | ||
5 | Architecture: arm | ||
6 | Version: $QPE_VERSION-$SUB_VERSION | ||
7 | Depends: opie-base ($QPE_VERSION) | ||
8 | Description: SFCave for the Zaurus. Fly the dot though the cave avoiding the walls. | ||
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp new file mode 100644 index 0000000..931f393 --- a/dev/null +++ b/noncore/games/sfcave/sfcave.cpp | |||
@@ -0,0 +1,421 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <math.h> | ||
4 | |||
5 | #ifdef QWS | ||
6 | #include <qpe/qpeapplication.h> | ||
7 | #else | ||
8 | #include <qapplication.h> | ||
9 | #endif | ||
10 | |||
11 | #include "sfcave.h" | ||
12 | |||
13 | #define CAPTION "SFCave 1.6 by AndyQ" | ||
14 | bool movel; | ||
15 | |||
16 | int main( int argc, char *argv[] ) | ||
17 | { | ||
18 | movel = true; | ||
19 | |||
20 | #ifdef QWS | ||
21 | QPEApplication a( argc, argv ); | ||
22 | #else | ||
23 | QApplication a( argc, argv ); | ||
24 | #endif | ||
25 | |||
26 | int speed = 3; | ||
27 | for ( int i = 0 ; i < argc ; ++i ) | ||
28 | { | ||
29 | if ( strcmp( argv[i], "-s" ) == 0 ) | ||
30 | { | ||
31 | if ( i+1 < argc ) | ||
32 | speed = atoi( argv[i+1] ); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | Main app( speed ); | ||
37 | a.setMainWidget( &app ); | ||
38 | app.show(); | ||
39 | app.start(); | ||
40 | a.exec(); | ||
41 | } | ||
42 | |||
43 | Main :: Main( int spd, QWidget *w, char *name ) | ||
44 | : QMainWindow( w, name ) | ||
45 | { | ||
46 | #ifdef QWS | ||
47 | showMaximized(); | ||
48 | #else | ||
49 | resize( 240, 284 ); | ||
50 | #endif | ||
51 | |||
52 | sWidth = width(); | ||
53 | sHeight = height(); | ||
54 | segSize = sWidth/(MAPSIZE-1)+1; | ||
55 | |||
56 | //printf( "width %d, height %d\n", sWidth, sHeight ); | ||
57 | //printf( "segSize = %d\n", segSize ); | ||
58 | |||
59 | setCaption( CAPTION ); | ||
60 | |||
61 | highestScore = 0; | ||
62 | speed = spd; // Change to 2 for PC | ||
63 | press = false; | ||
64 | |||
65 | offscreen = new QPixmap( sWidth, sHeight ); | ||
66 | offscreen->fill( Qt::black ); | ||
67 | |||
68 | setUp(); | ||
69 | crashLineLength = -1; | ||
70 | |||
71 | gameTimer = new QTimer( this, "game timer" ); | ||
72 | connect( gameTimer, SIGNAL( timeout() ), | ||
73 | this, SLOT( run() ) ); | ||
74 | } | ||
75 | |||
76 | Main :: ~Main() | ||
77 | { | ||
78 | } | ||
79 | |||
80 | void Main :: start() | ||
81 | { | ||
82 | gameTimer->start( 10 ); | ||
83 | |||
84 | } | ||
85 | |||
86 | int Main :: nextInt( int range ) | ||
87 | { | ||
88 | return rand() % range; | ||
89 | } | ||
90 | |||
91 | void Main :: setUp() | ||
92 | { | ||
93 | state = STATE_CRASHED; | ||
94 | prevState = STATE_CRASHED; | ||
95 | |||
96 | score = 0; | ||
97 | offset = 0; | ||
98 | maxHeight = 50; | ||
99 | nrFrames = 0; | ||
100 | dir = 1; | ||
101 | thrust = 0; | ||
102 | crashLineLength = 0; | ||
103 | |||
104 | user.setRect( 50, sWidth/2, 4, 4 ); | ||
105 | |||
106 | blockWidth = 20; | ||
107 | blockHeight = 70; | ||
108 | |||
109 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | ||
110 | { | ||
111 | trail[i].setX( -1 ); | ||
112 | trail[i].setY( 0 ); | ||
113 | } | ||
114 | |||
115 | mapTop[0] = (int)(nextInt(50)) + 5; | ||
116 | for ( int i = 1 ; i < MAPSIZE ; ++i ) | ||
117 | setPoint( i ); | ||
118 | |||
119 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
120 | blocks[i].setY( -1 ); | ||
121 | } | ||
122 | |||
123 | void Main :: run() | ||
124 | { | ||
125 | //running = true; | ||
126 | //setUp(); | ||
127 | switch ( state ) | ||
128 | { | ||
129 | case STATE_NEWGAME: | ||
130 | setUp(); | ||
131 | draw(); | ||
132 | state = STATE_RUNNING; | ||
133 | break; | ||
134 | case STATE_BOSS: | ||
135 | drawBoss(); | ||
136 | break; | ||
137 | |||
138 | case STATE_CRASHED: | ||
139 | draw(); | ||
140 | break; | ||
141 | |||
142 | case STATE_RUNNING: | ||
143 | { | ||
144 | if ( nrFrames % 5 == 0 ) | ||
145 | score ++; | ||
146 | if ( nrFrames % 2 == 0 ) | ||
147 | handleKeys(); | ||
148 | |||
149 | if ( ++nrFrames % 500 == 0 ) | ||
150 | { | ||
151 | if ( maxHeight < sHeight - 100 ) | ||
152 | { | ||
153 | maxHeight += 10; | ||
154 | |||
155 | // Reduce block height | ||
156 | if ( maxHeight > sHeight - 150 ) | ||
157 | blockHeight -= 5; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | if ( nrFrames % 100 == 0 ) | ||
162 | addBlock(); | ||
163 | |||
164 | // if ( false ) | ||
165 | if ( checkCollision() ) | ||
166 | { | ||
167 | if ( score > highestScore ) | ||
168 | highestScore = score; | ||
169 | |||
170 | state = STATE_CRASHED; | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | if ( movel ) | ||
175 | moveLandscape(); | ||
176 | //movel = false; | ||
177 | } | ||
178 | |||
179 | draw(); | ||
180 | break; | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | |||
185 | bool Main :: checkCollision() | ||
186 | { | ||
187 | if ( (user.y() + user.width()) >= mapBottom[10] || user.y() <= mapTop[10] ) | ||
188 | return true; | ||
189 | |||
190 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
191 | { | ||
192 | if ( blocks[i].y() != -1 ) | ||
193 | { | ||
194 | if ( blocks[i].intersects( user ) ) | ||
195 | return true; | ||
196 | } | ||
197 | } | ||
198 | return false; | ||
199 | } | ||
200 | |||
201 | void Main :: moveLandscape() | ||
202 | { | ||
203 | offset++; | ||
204 | |||
205 | if ( offset >= segSize ) | ||
206 | { | ||
207 | offset = 0; | ||
208 | for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) | ||
209 | { | ||
210 | mapTop[i] = mapTop[i+speed]; | ||
211 | mapBottom[i] = mapBottom[i+speed]; | ||
212 | } | ||
213 | |||
214 | for ( int i = speed ; i > 0 ; --i ) | ||
215 | setPoint( MAPSIZE-i ); | ||
216 | } | ||
217 | |||
218 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
219 | { | ||
220 | if ( blocks[i].y() != -1 ) | ||
221 | { | ||
222 | blocks[i].moveBy( -speed, 0 ); | ||
223 | if ( blocks[i].x() + blocks[i].width() < 0 ) | ||
224 | blocks[i].setY( -1 ); | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | void Main :: addBlock() | ||
230 | { | ||
231 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
232 | { | ||
233 | if ( blocks[i].y() == -1 ) | ||
234 | { | ||
235 | int x = sWidth; | ||
236 | |||
237 | int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); | ||
238 | |||
239 | blocks[i].setRect( x, y, blockWidth, blockHeight ); | ||
240 | // printf( "Added block @ %d %d\n", x, y ); | ||
241 | // printf( "mapTop = %d, mapBottom = %d", mapTop[50], mapBottom[50] ); | ||
242 | |||
243 | |||
244 | break; | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | |||
249 | void Main :: setPoint( int point ) | ||
250 | { | ||
251 | if ( nextInt(100) >= 80 ) | ||
252 | dir *= -1; | ||
253 | |||
254 | mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); | ||
255 | if ( mapTop[point] < 0 ) | ||
256 | { | ||
257 | mapTop[point] = 0; | ||
258 | dir *= -1; | ||
259 | } | ||
260 | else if ( mapTop[point] >= maxHeight ) | ||
261 | { | ||
262 | mapTop[point] = maxHeight; | ||
263 | dir *= -1; | ||
264 | } | ||
265 | |||
266 | mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); | ||
267 | mapBottom[point] = sHeight - (maxHeight - mapTop[point]); | ||
268 | } | ||
269 | |||
270 | void Main :: drawBoss() | ||
271 | { | ||
272 | offscreen->fill( Qt::black ); | ||
273 | |||
274 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); | ||
275 | } | ||
276 | |||
277 | void Main :: draw() | ||
278 | { | ||
279 | //printf( "Paint\n" ); | ||
280 | offscreen->fill( Qt::black ); | ||
281 | |||
282 | QPainter p( offscreen ); | ||
283 | p.setPen( Qt::white ); | ||
284 | |||
285 | for ( int i = 0 ; i < MAPSIZE -3; ++i ) | ||
286 | { | ||
287 | p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); | ||
288 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); | ||
289 | } | ||
290 | |||
291 | // Draw user | ||
292 | p.drawRect( user ); | ||
293 | |||
294 | // Draw trails | ||
295 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | ||
296 | if ( trail[i].x() >= 0 ) | ||
297 | p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); | ||
298 | |||
299 | // Draw blocks | ||
300 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
301 | if ( blocks[i].y() != -1 ) | ||
302 | { | ||
303 | p.fillRect( blocks[i], Qt::black ); | ||
304 | p.drawRect( blocks[i] ); | ||
305 | } | ||
306 | |||
307 | // draw score | ||
308 | QString s; | ||
309 | s.sprintf( "score %06d high score %06d", score, highestScore ); | ||
310 | p.drawText( 5, 10, s ); | ||
311 | |||
312 | |||
313 | if ( state == STATE_CRASHED ) | ||
314 | { | ||
315 | // add next crash line | ||
316 | |||
317 | if ( crashLineLength != -1 ) | ||
318 | { | ||
319 | for ( int i = 0 ; i < 36 ; ++i ) | ||
320 | { | ||
321 | int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); | ||
322 | int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); | ||
323 | } | ||
324 | } | ||
325 | |||
326 | if ( crashLineLength >= 15 || crashLineLength == -1 ) | ||
327 | p.drawText( 70, 140, QString( "Press down to start" ) ); | ||
328 | else | ||
329 | crashLineLength ++; | ||
330 | } | ||
331 | |||
332 | p.end(); | ||
333 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); | ||
334 | //printf( "endpaint\n" ); | ||
335 | } | ||
336 | |||
337 | void Main :: handleKeys() | ||
338 | { | ||
339 | // Find enpty trail and move others | ||
340 | bool done = false; | ||
341 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | ||
342 | { | ||
343 | if ( trail[i].x() < 0 ) | ||
344 | { | ||
345 | if ( !done ) | ||
346 | { | ||
347 | trail[i].setX( user.x() - 5 ); | ||
348 | trail[i].setY( user.y() ); | ||
349 | done = true; | ||
350 | } | ||
351 | } | ||
352 | else | ||
353 | { | ||
354 | trail[i].setX( trail[i].x() - (2) ); | ||
355 | } | ||
356 | } | ||
357 | |||
358 | if ( speed <= 3 ) | ||
359 | { | ||
360 | if ( press ) | ||
361 | thrust -= 0.6; | ||
362 | else | ||
363 | thrust += 0.8; | ||
364 | |||
365 | if ( thrust > 4.0 ) | ||
366 | thrust = 4.0; | ||
367 | else if ( thrust < -3.5 ) | ||
368 | thrust = -3.5; | ||
369 | } | ||
370 | else | ||
371 | { | ||
372 | if ( press ) | ||
373 | thrust -= 0.5; | ||
374 | else | ||
375 | thrust += 0.8; | ||
376 | |||
377 | if ( thrust > 5.0 ) | ||
378 | thrust = 5.0; | ||
379 | else if ( thrust < -3.5 ) | ||
380 | thrust = -3.5; | ||
381 | } | ||
382 | user.moveBy( 0, (int)thrust ); | ||
383 | } | ||
384 | |||
385 | void Main :: keyPressEvent( QKeyEvent *e ) | ||
386 | { | ||
387 | switch( e->key() ) | ||
388 | { | ||
389 | case Qt::Key_Up: | ||
390 | case Qt::Key_F9: | ||
391 | case Qt::Key_Space: | ||
392 | press = true; | ||
393 | break; | ||
394 | default: | ||
395 | e->ignore(); | ||
396 | break; | ||
397 | } | ||
398 | } | ||
399 | |||
400 | void Main :: keyReleaseEvent( QKeyEvent *e ) | ||
401 | { | ||
402 | switch( e->key() ) | ||
403 | { | ||
404 | case Qt::Key_Up: | ||
405 | case Qt::Key_F9: | ||
406 | case Qt::Key_Space: | ||
407 | press = false; | ||
408 | break; | ||
409 | case Qt::Key_R: | ||
410 | case Qt::Key_Down: | ||
411 | if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 ) | ||
412 | { | ||
413 | state = STATE_NEWGAME; | ||
414 | } | ||
415 | else | ||
416 | movel = true; | ||
417 | default: | ||
418 | e->ignore(); | ||
419 | break; | ||
420 | } | ||
421 | } | ||
diff --git a/noncore/games/sfcave/sfcave.h b/noncore/games/sfcave/sfcave.h new file mode 100644 index 0000000..18eeef9 --- a/dev/null +++ b/noncore/games/sfcave/sfcave.h | |||
@@ -0,0 +1,76 @@ | |||
1 | #include <qmainwindow.h> | ||
2 | #include <qpainter.h> | ||
3 | #include <qpixmap.h> | ||
4 | #include <qpoint.h> | ||
5 | #include <qrect.h> | ||
6 | #include <qtimer.h> | ||
7 | |||
8 | |||
9 | |||
10 | #define MAPSIZE 52 | ||
11 | #define BLOCKSIZE 5 | ||
12 | #define TRAILSIZE 30 | ||
13 | |||
14 | #define STATE_BOSS 0 | ||
15 | #define STATE_RUNNING 1 | ||
16 | #define STATE_CRASHED 2 | ||
17 | #define STATE_NEWGAME 3 | ||
18 | |||
19 | class Main : public QMainWindow | ||
20 | { | ||
21 | Q_OBJECT | ||
22 | |||
23 | public: | ||
24 | int sWidth; | ||
25 | int sHeight; | ||
26 | int segSize; | ||
27 | |||
28 | int blockWidth; | ||
29 | int blockHeight; | ||
30 | int state; | ||
31 | int prevState; | ||
32 | int speed; | ||
33 | int crashLineLength; | ||
34 | |||
35 | QPixmap *offscreen; | ||
36 | QTimer *gameTimer; | ||
37 | |||
38 | int score; | ||
39 | int highestScore; | ||
40 | |||
41 | int mapTop[52]; | ||
42 | int mapBottom[52]; | ||
43 | QRect blocks[5]; | ||
44 | QRect user; | ||
45 | QPoint trail[30]; | ||
46 | |||
47 | int offset; | ||
48 | int maxHeight; | ||
49 | int nrFrames; | ||
50 | int dir; | ||
51 | |||
52 | bool bossMode; | ||
53 | |||
54 | bool press; | ||
55 | double thrust; | ||
56 | bool running; | ||
57 | |||
58 | Main( int speed = 3, QWidget *p = 0, char *name = 0 ); | ||
59 | ~Main(); | ||
60 | void start(); | ||
61 | int nextInt( int range ); | ||
62 | void setUp(); | ||
63 | bool checkCollision(); | ||
64 | void moveLandscape(); | ||
65 | void addBlock(); | ||
66 | void setPoint( int point ); | ||
67 | void drawBoss(); | ||
68 | void draw(); | ||
69 | void handleKeys(); | ||
70 | |||
71 | void keyPressEvent( QKeyEvent *e ); | ||
72 | void keyReleaseEvent( QKeyEvent *e ); | ||
73 | |||
74 | private slots: | ||
75 | void run(); | ||
76 | }; | ||
diff --git a/noncore/games/sfcave/sfcave.pro b/noncore/games/sfcave/sfcave.pro new file mode 100644 index 0000000..b1c96b6 --- a/dev/null +++ b/noncore/games/sfcave/sfcave.pro | |||
@@ -0,0 +1,9 @@ | |||
1 | TEMPLATE= app | ||
2 | CONFIG += qt warn_on release | ||
3 | DESTDIR = $(OPIEDIR)/bin | ||
4 | SOURCES = sfcave.cpp | ||
5 | HEADERS = sfcave.h | ||
6 | TARGET = sfcave | ||
7 | INCLUDEPATH += $(OPIEDIR)/include | ||
8 | DEPENDPATH+= $(OPIEDIR)/include | ||
9 | LIBS += -lqpe | ||