summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave/sfcave.cpp936
-rw-r--r--noncore/games/sfcave/sfcave.h42
2 files changed, 652 insertions, 326 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp
index 931f393..8b9c844 100644
--- a/noncore/games/sfcave/sfcave.cpp
+++ b/noncore/games/sfcave/sfcave.cpp
@@ -6,2 +6,3 @@
6#include <qpe/qpeapplication.h> 6#include <qpe/qpeapplication.h>
7#include <qpe/config.h>
7#else 8#else
@@ -12,66 +13,127 @@
12 13
13#define CAPTION "SFCave 1.6 by AndyQ" 14#define CAPTION "SFCave 1.7 by AndyQ"
15
16// States
17#define STATE_BOSS 0
18#define STATE_RUNNING 1
19#define STATE_CRASHED 2
20#define STATE_NEWGAME 3
21#define STATE_MENU 4
22
23// Menus
24#define MENU_MAIN_MENU 0
25#define MENU_OPTIONS_MENU 1
26
27// Main Menu Options
28#define MENU_START_GAME 0
29#define MENU_OPTIONS 1
30#define MENU_QUIT 2
31
32// Option Menu Options
33#define MENU_GAME_TYPE 0
34#define MENU_GAME_DIFFICULTY 1
35#define MENU_BACK 2
36
37QString SFCave::menuOptions[2][5] = { { "Start Game", "Options", "Quit", "", "" },
38 { "Game Type - %s", "Game Difficulty - %s", "Back", "", "" } };
39
40
41#define NR_GAME_DIFFICULTIES 3
42#define NR_GAME_TYPES 3
43
44#define EASY "Easy"
45#define NORMAL "Normal"
46#define HARD "Hard"
47
48#define SFCAVE_GAME_TYPE 0
49#define GATES_GAME_TYPE 1
50#define FLY_GAME_TYPE 2
51#define SFCAVE_GAME "SFCave"
52#define GATES_GAME "Gates"
53#define FLY_GAME "Fly"
54QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD };
55QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME };
56
57#define CURRENT_GAME_TYPE gameTypes[currentGameType]
58#define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty];
59
14bool movel; 60bool movel;
15 61
62
16int main( int argc, char *argv[] ) 63int main( int argc, char *argv[] )
17{ 64{
18 movel = true; 65 movel = true;
19 66
20#ifdef QWS 67#ifdef QWS
21 QPEApplication a( argc, argv ); 68 QPEApplication a( argc, argv );
22#else 69#else
23 QApplication a( argc, argv ); 70 QApplication a( argc, argv );
24#endif 71#endif
25 72
26 int speed = 3; 73 int speed = 3;
27 for ( int i = 0 ; i < argc ; ++i ) 74 for ( int i = 0 ; i < argc ; ++i )
28 { 75 {
29 if ( strcmp( argv[i], "-s" ) == 0 ) 76 if ( strcmp( argv[i], "-s" ) == 0 )
30 { 77 {
31 if ( i+1 < argc ) 78 if ( i+1 < argc )
32 speed = atoi( argv[i+1] ); 79 speed = atoi( argv[i+1] );
33 } 80 }
34 } 81 }
35 82
36 Main app( speed ); 83 SFCave app( speed );
37 a.setMainWidget( &app ); 84 a.setMainWidget( &app );
38 app.show(); 85 app.show();
39 app.start(); 86 app.start();
40 a.exec(); 87 a.exec();
41} 88}
42 89
43Main :: Main( int spd, QWidget *w, char *name ) 90SFCave :: SFCave( int spd, QWidget *w, char *name )
44 : QMainWindow( w, name ) 91 : QMainWindow( w, name )
92
45{ 93{
46#ifdef QWS 94#ifdef QWS
47 showMaximized(); 95 showMaximized();
48#else 96#else
49 resize( 240, 284 ); 97 resize( 240, 284 );
50#endif 98#endif
51 99
52 sWidth = width(); 100 sWidth = width();
53 sHeight = height(); 101 sHeight = height();
54 segSize = sWidth/(MAPSIZE-1)+1; 102 segSize = sWidth/(MAPSIZE-1)+1;
55 103
56 //printf( "width %d, height %d\n", sWidth, sHeight ); 104 currentMenuNr = 0;
57 //printf( "segSize = %d\n", segSize ); 105 nrMenuOptions[0] = 3;
106 nrMenuOptions[1] = 3;
107 currentMenuOption[0] = 0;
108 currentMenuOption[1] = 0;
109 currentGameType = 0;
110 currentGameDifficulty = 0;
58 111
59 setCaption( CAPTION ); 112 setCaption( CAPTION );
60 113
61 highestScore = 0; 114#ifdef QWS
62 speed = spd; // Change to 2 for PC 115 Config cfg( "sfcave" );
63 press = false; 116 cfg.setGroup( "settings" );
117 QString key = "highScore_";
118 highestScore[SFCAVE_GAME_TYPE] = cfg.readNumEntry( key + SFCAVE_GAME, 0 );
119 highestScore[GATES_GAME_TYPE] = cfg.readNumEntry( key + GATES_GAME, 0 );
120 highestScore[FLY_GAME_TYPE] = cfg.readNumEntry( key + FLY_GAME, 0 );
121 currentGameType = cfg.readNumEntry( "gameType", 0 );
122 currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 );
123#endif
124 speed = spd; // Change to 2 for PC
125 press = false;
64 126
65 offscreen = new QPixmap( sWidth, sHeight ); 127 offscreen = new QPixmap( sWidth, sHeight );
66 offscreen->fill( Qt::black ); 128 offscreen->fill( Qt::black );
67 129
68 setUp(); 130 setUp();
69 crashLineLength = -1; 131 crashLineLength = -1;
70 132
71 gameTimer = new QTimer( this, "game timer" ); 133 gameTimer = new QTimer( this, "game timer" );
72 connect( gameTimer, SIGNAL( timeout() ), 134 connect( gameTimer, SIGNAL( timeout() ),
73 this, SLOT( run() ) ); 135 this, SLOT( run() ) );
74} 136}
75 137
76Main :: ~Main() 138SFCave :: ~SFCave()
77{ 139{
@@ -79,3 +141,3 @@ Main :: ~Main()
79 141
80void Main :: start() 142void SFCave :: start()
81{ 143{
@@ -85,231 +147,296 @@ void Main :: start()
85 147
86int Main :: nextInt( int range ) 148int SFCave :: nextInt( int range )
87{ 149{
88 return rand() % range; 150 return rand() % range;
89} 151}
90 152
91void Main :: setUp() 153void SFCave :: setUp()
92{ 154{
93 state = STATE_CRASHED; 155 state = STATE_MENU;
94 prevState = STATE_CRASHED; 156 prevState = STATE_MENU;
95 157
96 score = 0; 158 score = 0;
97 offset = 0; 159 offset = 0;
98 maxHeight = 50; 160 maxHeight = 50;
99 nrFrames = 0; 161 nrFrames = 0;
100 dir = 1; 162 dir = 1;
101 thrust = 0; 163 thrust = 0;
102 crashLineLength = 0; 164 crashLineLength = 0;
103 165
104 user.setRect( 50, sWidth/2, 4, 4 ); 166 user.setRect( 50, sWidth/2, 4, 4 );
105 167
106 blockWidth = 20; 168 blockWidth = 20;
107 blockHeight = 70; 169 blockHeight = 70;
170 gapHeight = 125;
171 gateDistance = 75;
172 nextGate = nextInt( 50 ) + gateDistance;
108 173
109 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 174 for ( int i = 0 ; i < TRAILSIZE ; ++i )
110 { 175 {
111 trail[i].setX( -1 ); 176 trail[i].setX( -1 );
112 trail[i].setY( 0 ); 177 trail[i].setY( 0 );
113 } 178 }
114 179
115 mapTop[0] = (int)(nextInt(50)) + 5; 180 mapTop[0] = (int)(nextInt(50)) + 5;
116 for ( int i = 1 ; i < MAPSIZE ; ++i ) 181 for ( int i = 1 ; i < MAPSIZE ; ++i )
117 setPoint( i ); 182 setPoint( i );
118 183
119 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 184 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
120 blocks[i].setY( -1 ); 185 blocks[i].setY( -1 );
121} 186}
122 187
123void Main :: run() 188void SFCave :: run()
124{ 189{
125 //running = true; 190 //running = true;
126 //setUp(); 191 //setUp();
127 switch ( state ) 192 switch ( state )
128 { 193 {
129 case STATE_NEWGAME: 194 case STATE_MENU:
130 setUp(); 195 displayMenu();
131 draw(); 196 break;
132 state = STATE_RUNNING; 197 case STATE_NEWGAME:
133 break; 198 setUp();
134 case STATE_BOSS: 199 draw();
135 drawBoss(); 200 state = STATE_RUNNING;
136 break; 201 break;
137 202 case STATE_BOSS:
138 case STATE_CRASHED: 203 drawBoss();
139 draw(); 204 break;
140 break; 205
141 206 case STATE_CRASHED:
142 case STATE_RUNNING: 207 draw();
143 { 208 break;
144 if ( nrFrames % 5 == 0 ) 209
145 score ++; 210 case STATE_RUNNING:
146 if ( nrFrames % 2 == 0 ) 211 {
147 handleKeys(); 212 if ( nrFrames % 5 == 0 )
148 213 score ++;
149 if ( ++nrFrames % 500 == 0 ) 214 if ( nrFrames % 2 == 0 )
150 { 215 handleKeys();
151 if ( maxHeight < sHeight - 100 ) 216
152 { 217 // Apply Game rules
153 maxHeight += 10; 218 nrFrames ++;
154 219 if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
155 // Reduce block height 220 {
156 if ( maxHeight > sHeight - 150 ) 221 if ( nrFrames % 500 == 0 )
157 blockHeight -= 5; 222 {
158 } 223 if ( maxHeight < sHeight - 100 )
159 } 224 {
160 225 maxHeight += 10;
161 if ( nrFrames % 100 == 0 ) 226
162 addBlock(); 227 // Reduce block height
163 228 if ( maxHeight > sHeight - 150 )
164 // if ( false ) 229 blockHeight -= 5;
165 if ( checkCollision() ) 230 }
166 { 231 }
167 if ( score > highestScore ) 232
168 highestScore = score; 233 if ( nrFrames % 100 == 0 )
169 234 addBlock();
170 state = STATE_CRASHED; 235 }
171 } 236 else if ( CURRENT_GAME_TYPE == GATES_GAME )
172 else 237 {
173 { 238 // Slightly random gap distance
174 if ( movel ) 239 if ( nrFrames >= nextGate )
175 moveLandscape(); 240 {
176 //movel = false; 241 nextGate = nrFrames + nextInt( 50 ) + gateDistance;
177 } 242 addGate();
178 243 }
179 draw(); 244
180 break; 245 if ( nrFrames % 500 == 0 )
181 } 246 {
182 } 247 if ( gapHeight > 75 )
248 gapHeight -= 5;
249 }
250 }
251 else if ( CURRENT_GAME_TYPE == FLY_GAME )
252 {
253 }
254
255
256 if ( checkCollision() )
257 {
258 if ( score > highestScore[currentGameType] )
259 highestScore[currentGameType] = score;
260
261#ifdef QWS
262 Config cfg( "sfcave" );
263 cfg.setGroup( "settings" );
264 QString key = "highScore_";
265 key += CURRENT_GAME_TYPE;
266 cfg.writeEntry( key, highestScore[currentGameType] );
267#endif
268
269 state = STATE_CRASHED;
270 }
271 else
272 {
273 if ( movel )
274 moveLandscape();
275 //movel = false;
276 }
277
278 draw();
279 break;
280 }
281 }
282}
283
284bool SFCave :: checkCollision()
285{
286 if ( (user.y() + user.width()) >= mapBottom[10] || user.y() <= mapTop[10] )
287 return true;
288
289 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
290 {
291 if ( blocks[i].y() != -1 )
292 {
293 if ( blocks[i].intersects( user ) )
294 return true;
295 }
296 }
297 return false;
183} 298}
184 299
185bool Main :: checkCollision() 300void SFCave :: moveLandscape()
186{ 301{
187 if ( (user.y() + user.width()) >= mapBottom[10] || user.y() <= mapTop[10] ) 302 offset++;
188 return true; 303
189 304 if ( offset >= segSize )
190 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 305 {
191 { 306 offset = 0;
192 if ( blocks[i].y() != -1 ) 307 for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
193 { 308 {
194 if ( blocks[i].intersects( user ) ) 309 mapTop[i] = mapTop[i+speed];
195 return true; 310 mapBottom[i] = mapBottom[i+speed];
196 } 311 }
197 } 312
198 return false; 313 for ( int i = speed ; i > 0 ; --i )
314 setPoint( MAPSIZE-i );
315 }
316
317 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
318 {
319 if ( blocks[i].y() != -1 )
320 {
321 blocks[i].moveBy( -speed, 0 );
322 if ( blocks[i].x() + blocks[i].width() < 0 )
323 blocks[i].setY( -1 );
324 }
325 }
199} 326}
200 327
201void Main :: moveLandscape() 328void SFCave :: addBlock()
202{ 329{
203 offset++; 330 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
204 331 {
205 if ( offset >= segSize ) 332 if ( blocks[i].y() == -1 )
206 { 333 {
207 offset = 0; 334 int x = sWidth;
208 for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) 335
209 { 336 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight));
210 mapTop[i] = mapTop[i+speed]; 337
211 mapBottom[i] = mapBottom[i+speed]; 338 blocks[i].setRect( x, y, blockWidth, blockHeight );
212 } 339
213 340 break;
214 for ( int i = speed ; i > 0 ; --i ) 341 }
215 setPoint( MAPSIZE-i ); 342 }
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} 343}
228 344
229void Main :: addBlock() 345void SFCave :: addGate()
230{ 346{
231 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 347 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
232 { 348 {
233 if ( blocks[i].y() == -1 ) 349 if ( blocks[i].y() == -1 )
234 { 350 {
235 int x = sWidth; 351 int x1 = sWidth;
352 int y1 = mapTop[50];
353 int b1Height = nextInt(mapBottom[50] - mapTop[50] - gateDistance);
236 354
237 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); 355 // See if height between last gate and this one is too big
356 if ( b1Height - 200 > lastGateBottomY )
357 b1Height -= 25;
358 else if ( b1Height + 200 < lastGateBottomY )
359 b1Height += 25;
360 lastGateBottomY = b1Height;
238 361
239 blocks[i].setRect( x, y, blockWidth, blockHeight ); 362 int x2 = sWidth;
240 // printf( "Added block @ %d %d\n", x, y ); 363 int y2 = y1 + b1Height + gateDistance;
241 // printf( "mapTop = %d, mapBottom = %d", mapTop[50], mapBottom[50] ); 364 int b2Height = mapBottom[50] - y2;
365
242 366
367 blocks[i].setRect( x1, y1, blockWidth, b1Height );
368 blocks[i+1].setRect( x2, y2, blockWidth, b2Height );
243 369
244 break; 370 break;
245 } 371 }
246 } 372 }
247} 373}
248 374
249void Main :: setPoint( int point ) 375void SFCave :: setPoint( int point )
250{ 376{
251 if ( nextInt(100) >= 80 ) 377 if ( nextInt(100) >= 80 )
252 dir *= -1; 378 dir *= -1;
253 379
254 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); 380 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) );
255 if ( mapTop[point] < 0 ) 381 if ( mapTop[point] < 0 )
256 { 382 {
257 mapTop[point] = 0; 383 mapTop[point] = 0;
258 dir *= -1; 384 dir *= -1;
259 } 385 }
260 else if ( mapTop[point] >= maxHeight ) 386 else if ( mapTop[point] >= maxHeight )
261 { 387 {
262 mapTop[point] = maxHeight; 388 mapTop[point] = maxHeight;
263 dir *= -1; 389 dir *= -1;
264 } 390 }
265 391
266 mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); 392 mapBottom[point] = sHeight - (maxHeight - mapBottom[point]);
267 mapBottom[point] = sHeight - (maxHeight - mapTop[point]); 393 mapBottom[point] = sHeight - (maxHeight - mapTop[point]);
268} 394}
269 395
270void Main :: drawBoss() 396void SFCave :: drawBoss()
271{ 397{
272 offscreen->fill( Qt::black ); 398 offscreen->fill( Qt::black );
273 399
274 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 400 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
275} 401}
276 402
277void Main :: draw() 403void SFCave :: draw()
278{ 404{
279 //printf( "Paint\n" ); 405 //printf( "Paint\n" );
280 offscreen->fill( Qt::black ); 406 offscreen->fill( Qt::black );
281 407
282 QPainter p( offscreen ); 408 QPainter p( offscreen );
283 p.setPen( Qt::white ); 409 QFontMetrics fm = p.fontMetrics();
284 410 p.setPen( Qt::white );
285 for ( int i = 0 ; i < MAPSIZE -3; ++i ) 411
286 { 412 for ( int i = 0 ; i < MAPSIZE -3; ++i )
287 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); 413 {
288 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); 414 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] );
289 } 415 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] );
290 416 }
291 // Draw user 417
292 p.drawRect( user ); 418 // Draw user
293 419 p.drawRect( user );
294 // Draw trails 420
295 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 421 // Draw trails
296 if ( trail[i].x() >= 0 ) 422 for ( int i = 0 ; i < TRAILSIZE ; ++i )
297 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); 423 if ( trail[i].x() >= 0 )
298 424 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 );
299 // Draw blocks 425
300 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 426 // Draw blocks
301 if ( blocks[i].y() != -1 ) 427 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
302 { 428 if ( blocks[i].y() != -1 )
303 p.fillRect( blocks[i], Qt::black ); 429 {
304 p.drawRect( blocks[i] ); 430 p.fillRect( blocks[i], Qt::black );
305 } 431 p.drawRect( blocks[i] );
306 432 }
307 // draw score 433
308 QString s; 434 // draw score
309 s.sprintf( "score %06d high score %06d", score, highestScore ); 435 QString s;
310 p.drawText( 5, 10, s ); 436 s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType] );
311 437 p.drawText( 5, 10, s );
312 438
313 if ( state == STATE_CRASHED ) 439
314 { 440 if ( state == STATE_CRASHED )
441 {
315 // add next crash line 442 // add next crash line
@@ -326,96 +453,281 @@ void Main :: draw()
326 if ( crashLineLength >= 15 || crashLineLength == -1 ) 453 if ( crashLineLength >= 15 || crashLineLength == -1 )
327 p.drawText( 70, 140, QString( "Press down to start" ) ); 454 {
455 QString text = "Press up or down to start";
456 p.drawText( (sWidth/2) - (fm.width( text )/2), 140, text );
457
458 text = "or press OK for menu";
459 p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text );
460// p.drawText( 70, 140, QString( "Press down to start" ) );
461 }
328 else 462 else
329 crashLineLength ++; 463 crashLineLength ++;
330 } 464 }
331 465
332 p.end(); 466 p.end();
333 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 467 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
334 //printf( "endpaint\n" ); 468 //printf( "endpaint\n" );
335} 469}
336 470
337void Main :: handleKeys() 471void SFCave :: handleKeys()
338{ 472{
339 // Find enpty trail and move others 473 // Find enpty trail and move others
340 bool done = false; 474 bool done = false;
341 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 475 for ( int i = 0 ; i < TRAILSIZE ; ++i )
342 { 476 {
343 if ( trail[i].x() < 0 ) 477 if ( trail[i].x() < 0 )
344 { 478 {
345 if ( !done ) 479 if ( !done )
346 { 480 {
347 trail[i].setX( user.x() - 5 ); 481 trail[i].setX( user.x() - 5 );
348 trail[i].setY( user.y() ); 482 trail[i].setY( user.y() );
349 done = true; 483 done = true;
350 } 484 }
351 } 485 }
352 else 486 else
353 { 487 {
354 trail[i].setX( trail[i].x() - (2) ); 488 trail[i].setX( trail[i].x() - (2) );
355 } 489 }
356 } 490 }
357 491
358 if ( speed <= 3 ) 492 if ( speed <= 3 )
359 { 493 {
360 if ( press ) 494 if ( press )
361 thrust -= 0.6; 495 thrust -= 0.6;
362 else 496 else
363 thrust += 0.8; 497 thrust += 0.8;
364 498
365 if ( thrust > 4.0 ) 499 if ( thrust > 4.0 )
366 thrust = 4.0; 500 thrust = 4.0;
367 else if ( thrust < -3.5 ) 501 else if ( thrust < -3.5 )
368 thrust = -3.5; 502 thrust = -3.5;
369 } 503 }
370 else 504 else
371 { 505 {
372 if ( press ) 506 if ( press )
373 thrust -= 0.5; 507 thrust -= 0.5;
374 else 508 else
375 thrust += 0.8; 509 thrust += 0.8;
376 510
377 if ( thrust > 5.0 ) 511 if ( thrust > 5.0 )
378 thrust = 5.0; 512 thrust = 5.0;
379 else if ( thrust < -3.5 ) 513 else if ( thrust < -3.5 )
380 thrust = -3.5; 514 thrust = -3.5;
381 } 515 }
382 user.moveBy( 0, (int)thrust ); 516 user.moveBy( 0, (int)thrust );
383} 517}
384 518
385void Main :: keyPressEvent( QKeyEvent *e ) 519void SFCave :: keyPressEvent( QKeyEvent *e )
386{ 520{
387 switch( e->key() ) 521 if ( state == STATE_MENU )
388 { 522 {
389 case Qt::Key_Up: 523 switch( e->key() )
390 case Qt::Key_F9: 524 {
391 case Qt::Key_Space: 525 case Qt::Key_Down:
392 press = true; 526 currentMenuOption[currentMenuNr] ++;
393 break; 527 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" )
394 default: 528 currentMenuOption[currentMenuNr] = 0;
395 e->ignore(); 529 break;
396 break; 530 case Qt::Key_Up:
397 } 531 currentMenuOption[currentMenuNr] --;
532 if ( currentMenuOption[currentMenuNr] < 0 )
533 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1;
534 break;
535
536 case Qt::Key_Left:
537 if ( currentMenuNr == MENU_OPTIONS_MENU )
538 {
539 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
540 {
541 currentGameType --;
542 if ( currentGameType < 0 )
543 currentGameType = NR_GAME_TYPES - 1;
544 }
545 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
546 {
547 currentGameDifficulty --;
548 if ( currentGameDifficulty < 0 )
549 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1;
550 }
551 }
552 break;
553
554 case Qt::Key_Right:
555 if ( currentMenuNr == MENU_OPTIONS_MENU )
556 {
557 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
558 {
559 currentGameType ++;
560 if ( currentGameType == NR_GAME_TYPES )
561 currentGameType = 0;
562 }
563 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
564 {
565 currentGameDifficulty ++;
566 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES )
567 currentGameDifficulty = 0;
568 }
569 }
570 break;
571
572 case Qt::Key_Space:
573 case Qt::Key_Return:
574 case Qt::Key_Enter:
575 dealWithMenuSelection();
576 break;
577 }
578 }
579 else
580 {
581 switch( e->key() )
582 {
583 case Qt::Key_Up:
584 case Qt::Key_F9:
585 case Qt::Key_Space:
586 press = true;
587 break;
588 case Qt::Key_M:
589 case Qt::Key_Return:
590 case Qt::Key_Enter:
591 if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 )
592 state = STATE_MENU;
593 default:
594 e->ignore();
595 break;
596 }
597 }
398} 598}
399 599
400void Main :: keyReleaseEvent( QKeyEvent *e ) 600void SFCave :: keyReleaseEvent( QKeyEvent *e )
401{ 601{
402 switch( e->key() ) 602 if ( state == STATE_MENU )
403 { 603 {
404 case Qt::Key_Up: 604 }
405 case Qt::Key_F9: 605 else
406 case Qt::Key_Space: 606 {
407 press = false; 607 switch( e->key() )
608 {
609 case Qt::Key_F9:
610 case Qt::Key_Space:
611 press = false;
612 break;
613
614 case Qt::Key_Up:
615 press = false;
616
617 case Qt::Key_R:
618 case Qt::Key_Down:
619 if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 )
620 {
621 state = STATE_NEWGAME;
622 }
623 else
624 movel = true;
625 break;
626
627 default:
628 e->ignore();
629 break;
630 }
631 }
632
633}
634
635void SFCave :: displayMenu()
636{
637 offscreen->fill( Qt::black );
638
639 QPainter p( offscreen );
640 p.setPen( Qt::white );
641
642 QFont f( "Helvetica", 16 );
643 p.setFont( f );
644
645 QFontMetrics fm = p.fontMetrics();
646
647 QString text = "SFCave";
648 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );
649
650 text = "Written by Andy Qua";
651 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text );
652
653 // Draw options
654 int pos = 170;
655 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 )
656 {
657 if ( currentMenuOption[currentMenuNr] == i )
658 p.setPen( Qt::yellow );
659 else
660 p.setPen( Qt::white );
661
662 QString text;
663 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 )
664 {
665 QString val;
666 if ( i == MENU_GAME_TYPE )
667 val = gameTypes[currentGameType];
668 else
669 val = dificultyOption[currentGameDifficulty];
670
671 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val );
672 }
673 else
674 text = menuOptions[currentMenuNr][i];
675
676 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text );
677 }
678
679 p.end();
680 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
681}
682
683void SFCave :: dealWithMenuSelection()
684{
685 switch( currentMenuNr )
686 {
687 case MENU_MAIN_MENU:
688 {
689 switch( currentMenuOption[currentMenuNr] )
690 {
691 case MENU_START_GAME:
692 state = STATE_NEWGAME;
693 break;
694
695 case MENU_OPTIONS:
696 currentMenuNr = MENU_OPTIONS_MENU;
697 currentMenuOption[currentMenuNr] = 0;
698 break;
699
700 case MENU_QUIT:
701 QApplication::exit();
702 break;
703 }
704
408 break; 705 break;
409 case Qt::Key_R: 706 }
410 case Qt::Key_Down: 707
411 if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 ) 708 case MENU_OPTIONS_MENU:
412 { 709 {
413 state = STATE_NEWGAME; 710 switch( currentMenuOption[currentMenuNr] )
414 } 711 {
415 else 712 case MENU_GAME_TYPE:
416 movel = true; 713 break;
417 default: 714
418 e->ignore(); 715 case MENU_GAME_DIFFICULTY:
716 break;
717
718 case MENU_BACK:
719 currentMenuNr = MENU_MAIN_MENU;
720
721#ifdef QWS
722 Config cfg( "sfcave" );
723 cfg.setGroup( "settings" );
724 cfg.writeEntry( "difficulty", currentGameDifficulty );
725 cfg.writeEntry( "gameType", currentGameType );
726#endif
727 break;
728 }
729
419 break; 730 break;
420 } 731 }
421} 732 }
733} \ No newline at end of file
diff --git a/noncore/games/sfcave/sfcave.h b/noncore/games/sfcave/sfcave.h
index 18eeef9..b19d147 100644
--- a/noncore/games/sfcave/sfcave.h
+++ b/noncore/games/sfcave/sfcave.h
@@ -10,11 +10,6 @@
10#define MAPSIZE 52 10#define MAPSIZE 52
11#define BLOCKSIZE 5 11#define BLOCKSIZE 6
12#define TRAILSIZE 30 12#define TRAILSIZE 30
13 13
14#define STATE_BOSS 0 14class SFCave : public QMainWindow
15#define STATE_RUNNING 1
16#define STATE_CRASHED 2
17#define STATE_NEWGAME 3
18
19class Main : public QMainWindow
20{ 15{
@@ -29,2 +24,3 @@ public:
29 int blockHeight; 24 int blockHeight;
25 int gapHeight;
30 int state; 26 int state;
@@ -34,2 +30,16 @@ public:
34 30
31 int gateDistance;
32 int nextGate;
33 int lastGateBottomY;
34
35 static QString menuOptions[2][5];
36 int currentMenuNr;
37 int nrMenuOptions[2];
38 int currentMenuOption[2];
39
40 static QString dificultyOption[3];
41 static QString gameTypes[3];
42 int currentGameType;
43 int currentGameDifficulty;
44
35 QPixmap *offscreen; 45 QPixmap *offscreen;
@@ -38,9 +48,9 @@ public:
38 int score; 48 int score;
39 int highestScore; 49 int highestScore[3];
40 50
41 int mapTop[52]; 51 int mapTop[MAPSIZE];
42 int mapBottom[52]; 52 int mapBottom[MAPSIZE];
43 QRect blocks[5]; 53 QRect blocks[BLOCKSIZE];
44 QRect user; 54 QRect user;
45 QPoint trail[30]; 55 QPoint trail[TRAILSIZE];
46 56
@@ -57,4 +67,4 @@ public:
57 67
58 Main( int speed = 3, QWidget *p = 0, char *name = 0 ); 68 SFCave( int speed = 3, QWidget *p = 0, char *name = 0 );
59 ~Main(); 69 ~SFCave();
60 void start(); 70 void start();
@@ -65,2 +75,3 @@ public:
65 void addBlock(); 75 void addBlock();
76 void addGate();
66 void setPoint( int point ); 77 void setPoint( int point );
@@ -70,2 +81,5 @@ public:
70 81
82 void displayMenu();
83 void dealWithMenuSelection();
84
71 void keyPressEvent( QKeyEvent *e ); 85 void keyPressEvent( QKeyEvent *e );