summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave/sfcave.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp
index 0160d34..929dc41 100644
--- a/noncore/games/sfcave/sfcave.cpp
+++ b/noncore/games/sfcave/sfcave.cpp
@@ -1,1164 +1,1164 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <math.h> 3#include <math.h>
4#include <time.h> 4#include <time.h>
5 5
6#ifdef QWS 6#ifdef QWS
7#include <qpe/qpeapplication.h> 7#include <qpe/qpeapplication.h>
8#include <qpe/config.h> 8#include <qpe/config.h>
9#else 9#else
10#include <qapplication.h> 10#include <qapplication.h>
11#endif 11#endif
12#include <qdir.h> 12#include <qdir.h>
13 13
14#include "helpwindow.h" 14#include "helpwindow.h"
15#include "sfcave.h" 15#include "sfcave.h"
16 16
17#define CAPTION "SFCave 1.10 by AndyQ" 17#define CAPTION "SFCave 1.11 by AndyQ"
18 18
19#define UP_THRUST 0.6 19#define UP_THRUST 0.6
20#define NO_THRUST 0.8 20#define NO_THRUST 0.8
21#define MAX_DOWN_THRUST 4.0 21#define MAX_DOWN_THRUST 4.0
22#define MAX_UP_THRUST -3.5 22#define MAX_UP_THRUST -3.5
23 23
24// States 24// States
25#define STATE_BOSS 0 25#define STATE_BOSS 0
26#define STATE_RUNNING 1 26#define STATE_RUNNING 1
27#define STATE_CRASHING 2 27#define STATE_CRASHING 2
28#define STATE_CRASHED 3 28#define STATE_CRASHED 3
29#define STATE_NEWGAME 4 29#define STATE_NEWGAME 4
30#define STATE_MENU 5 30#define STATE_MENU 5
31#define STATE_REPLAY 6 31#define STATE_REPLAY 6
32 32
33// Menus 33// Menus
34#define MENU_MAIN_MENU 0 34#define MENU_MAIN_MENU 0
35#define MENU_OPTIONS_MENU 1 35#define MENU_OPTIONS_MENU 1
36#define MENU_REPLAY_MENU 2 36#define MENU_REPLAY_MENU 2
37 37
38// Main Menu Options 38// Main Menu Options
39#define MENU_START_GAME 0 39#define MENU_START_GAME 0
40#define MENU_REPLAY 1 40#define MENU_REPLAY 1
41#define MENU_OPTIONS 2 41#define MENU_OPTIONS 2
42#define MENU_HELP 3 42#define MENU_HELP 3
43#define MENU_QUIT 4 43#define MENU_QUIT 4
44 44
45// Option Menu Options 45// Option Menu Options
46#define MENU_GAME_TYPE 0 46#define MENU_GAME_TYPE 0
47#define MENU_GAME_DIFFICULTY 1 47#define MENU_GAME_DIFFICULTY 1
48#define MENU_CLEAR_HIGHSCORES 2 48#define MENU_CLEAR_HIGHSCORES 2
49#define MENU_BACK 3 49#define MENU_BACK 3
50 50
51// Replay Menu Options 51// Replay Menu Options
52#define MENU_REPLAY_START 0 52#define MENU_REPLAY_START 0
53#define MENU_REPLAY_LOAD 1 53#define MENU_REPLAY_LOAD 1
54#define MENU_REPLAY_SAVE 2 54#define MENU_REPLAY_SAVE 2
55#define MENU_REPLAY_BACK 3 55#define MENU_REPLAY_BACK 3
56 56
57 57
58#define NR_GAME_DIFFICULTIES 3 58#define NR_GAME_DIFFICULTIES 3
59#define NR_GAME_TYPES 3 59#define NR_GAME_TYPES 3
60 60
61#define DIFICULTY_EASY 0 61#define DIFICULTY_EASY 0
62#define DIFICULTY_NORMAL 1 62#define DIFICULTY_NORMAL 1
63#define DIFICULTY_HARD 2 63#define DIFICULTY_HARD 2
64#define EASY "Easy" 64#define EASY "Easy"
65#define NORMAL "Normal" 65#define NORMAL "Normal"
66#define HARD "Hard" 66#define HARD "Hard"
67 67
68#define SFCAVE_GAME_TYPE 0 68#define SFCAVE_GAME_TYPE 0
69#define GATES_GAME_TYPE 1 69#define GATES_GAME_TYPE 1
70#define FLY_GAME_TYPE 2 70#define FLY_GAME_TYPE 2
71#define SFCAVE_GAME "SFCave" 71#define SFCAVE_GAME "SFCave"
72#define GATES_GAME "Gates" 72#define GATES_GAME "Gates"
73#define FLY_GAME "Fly" 73#define FLY_GAME "Fly"
74#define CURRENT_GAME_TYPE gameTypes[currentGameType] 74#define CURRENT_GAME_TYPE gameTypes[currentGameType]
75#define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty]; 75#define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty];
76 76
77QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD }; 77QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD };
78QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME }; 78QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME };
79 79
80QString SFCave::menuOptions[NR_MENUS][MAX_MENU_OPTIONS] = { { "Start Game", "Replays", "Options", "Help", "Quit", "", "", "" }, 80QString SFCave::menuOptions[NR_MENUS][MAX_MENU_OPTIONS] = { { "Start Game", "Replays", "Options", "Help", "Quit", "", "", "" },
81 { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "", "", "", "" }, 81 { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "", "", "", "" },
82 { "Play Reply", "Load Replay", "Save Replay", "Back", "", "", "", "" } }; 82 { "Play Reply", "Load Replay", "Save Replay", "Back", "", "", "", "" } };
83 83
84int SFCave::nrMenuOptions[NR_MENUS] = { 5, 4, 4 }; 84int SFCave::nrMenuOptions[NR_MENUS] = { 5, 4, 4 };
85int SFCave ::currentMenuOption[NR_MENUS] = { 0, 0, 0 }; 85int SFCave ::currentMenuOption[NR_MENUS] = { 0, 0, 0 };
86 86
87#define UP_THRUST 0.6 87#define UP_THRUST 0.6
88#define NO_THRUST 0.8 88#define NO_THRUST 0.8
89#define MAX_DOWN_THRUST 4.0 89#define MAX_DOWN_THRUST 4.0
90#define MAX_UP_THRUST -3.5 90#define MAX_UP_THRUST -3.5
91double SFCave::UpThrustVals[3][3] = {{ 0.6, 0.6, 0.6 }, // SFCave 91double SFCave::UpThrustVals[3][3] = {{ 0.6, 0.6, 0.6 }, // SFCave
92 { 0.6, 0.6, 0.8 }, // Gates 92 { 0.6, 0.6, 0.8 }, // Gates
93 { 0.4, 0.7, 1.0 } }; // Fly 93 { 0.4, 0.7, 1.0 } }; // Fly
94 94
95double SFCave::DownThrustVals[3][3] = {{ 0.8, 0.8, 0.8 }, // SFCave 95double SFCave::DownThrustVals[3][3] = {{ 0.8, 0.8, 0.8 }, // SFCave
96 { 0.8, 0.8, 1.0 }, // Gates 96 { 0.8, 0.8, 1.0 }, // Gates
97 { 0.4, 0.7, 1.0 } }; // Fly 97 { 0.4, 0.7, 1.0 } }; // Fly
98 98
99double SFCave::MaxUpThrustVals[3][3] = {{ -3.5, -3.5, -3.5 }, // SFCave 99double SFCave::MaxUpThrustVals[3][3] = {{ -3.5, -3.5, -3.5 }, // SFCave
100 { -3.5, -4.0, -5.0 }, // Gates 100 { -3.5, -4.0, -5.0 }, // Gates
101 { -3.5, -4.0, -5.0 } }; // Fly 101 { -3.5, -4.0, -5.0 } }; // Fly
102 102
103double SFCave::MaxDownThrustVals[3][3] = {{ 4.0, 4.0, 4.0 }, // SFCave 103double SFCave::MaxDownThrustVals[3][3] = {{ 4.0, 4.0, 4.0 }, // SFCave
104 { 4.0, 5.0, 5.5 }, // Gates 104 { 4.0, 5.0, 5.5 }, // Gates
105 { 3.5, 4.0, 5.0 } }; // Fly 105 { 3.5, 4.0, 5.0 } }; // Fly
106 106
107int SFCave::initialGateGaps[] = { 75, 50, 25 }; 107int SFCave::initialGateGaps[] = { 75, 50, 25 };
108 108
109 109
110bool movel; 110bool movel;
111 111
112 112
113int main( int argc, char *argv[] ) 113int main( int argc, char *argv[] )
114{ 114{
115 movel = true; 115 movel = true;
116#ifdef QWS 116#ifdef QWS
117 QPEApplication a( argc, argv ); 117 QPEApplication a( argc, argv );
118#else 118#else
119 QApplication a( argc, argv ); 119 QApplication a( argc, argv );
120#endif 120#endif
121 121
122 int speed = 3; 122 int speed = 3;
123 for ( int i = 0 ; i < argc ; ++i ) 123 for ( int i = 0 ; i < argc ; ++i )
124 { 124 {
125 if ( strcmp( argv[i], "-s" ) == 0 ) 125 if ( strcmp( argv[i], "-s" ) == 0 )
126 { 126 {
127 if ( i+1 < argc ) 127 if ( i+1 < argc )
128 speed = atoi( argv[i+1] ); 128 speed = atoi( argv[i+1] );
129 } 129 }
130 } 130 }
131 131
132 SFCave app( speed ); 132 SFCave app( speed );
133 a.setMainWidget( &app ); 133 a.setMainWidget( &app );
134 app.show(); 134 app.show();
135 app.start(); 135 app.start();
136 a.exec(); 136 a.exec();
137} 137}
138 138
139SFCave :: SFCave( int spd, QWidget *w, char *name ) 139SFCave :: SFCave( int spd, QWidget *w, char *name )
140 : QMainWindow( w, name ) 140 : QMainWindow( w, name )
141 141
142{ 142{
143 replayIt = 0; 143 replayIt = 0;
144#ifdef QWS 144#ifdef QWS
145 showMaximized(); 145 showMaximized();
146#else 146#else
147 resize( 240, 284 ); 147 resize( 240, 284 );
148#endif 148#endif
149 149
150 replayFile = QDir::home().path(); 150 replayFile = QDir::home().path();
151 replayFile += "/sfcave.replay"; 151 replayFile += "/sfcave.replay";
152 printf( "%s\n", (const char *)replayFile ); 152 printf( "%s\n", (const char *)replayFile );
153 153
154 sWidth = width(); 154 sWidth = width();
155 sHeight = height(); 155 sHeight = height();
156 segSize = sWidth/(MAPSIZE-1)+1; 156 segSize = sWidth/(MAPSIZE-1)+1;
157 157
158 currentMenuNr = 0; 158 currentMenuNr = 0;
159 currentGameType = 0; 159 currentGameType = 0;
160 currentGameDifficulty = 0; 160 currentGameDifficulty = 0;
161 161
162 setCaption( CAPTION ); 162 setCaption( CAPTION );
163 showScoreZones = false; 163 showScoreZones = false;
164 164
165#ifdef QWS 165#ifdef QWS
166 Config cfg( "sfcave" ); 166 Config cfg( "sfcave" );
167 cfg.setGroup( "settings" ); 167 cfg.setGroup( "settings" );
168 QString key = "highScore_"; 168 QString key = "highScore_";
169 169
170 for ( int i = 0 ; i < 3 ; ++i ) 170 for ( int i = 0 ; i < 3 ; ++i )
171 { 171 {
172 for ( int j = 0 ; j < 3 ; ++j ) 172 for ( int j = 0 ; j < 3 ; ++j )
173 highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); 173 highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 );
174 } 174 }
175 175
176 currentGameType = cfg.readNumEntry( "gameType", 0 ); 176 currentGameType = cfg.readNumEntry( "gameType", 0 );
177 currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); 177 currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 );
178#endif 178#endif
179 speed = spd; // Change to 2 for PC 179 speed = spd; // Change to 2 for PC
180 press = false; 180 press = false;
181 181
182 offscreen = new QPixmap( sWidth, sHeight ); 182 offscreen = new QPixmap( sWidth, sHeight );
183 offscreen->fill( Qt::black ); 183 offscreen->fill( Qt::black );
184 184
185// setUp(); 185// setUp();
186 crashLineLength = -1; 186 crashLineLength = -1;
187 state = STATE_MENU; 187 state = STATE_MENU;
188 prevState = STATE_MENU; 188 prevState = STATE_MENU;
189 189
190 gameTimer = new QTimer( this, "game timer" ); 190 gameTimer = new QTimer( this, "game timer" );
191 connect( gameTimer, SIGNAL( timeout() ), 191 connect( gameTimer, SIGNAL( timeout() ),
192 this, SLOT( run() ) ); 192 this, SLOT( run() ) );
193} 193}
194 194
195SFCave :: ~SFCave() 195SFCave :: ~SFCave()
196{ 196{
197} 197}
198 198
199void SFCave :: start() 199void SFCave :: start()
200{ 200{
201 gameTimer->start( 10 ); 201 gameTimer->start( 10 );
202 202
203} 203}
204 204
205void SFCave :: setSeed( int seed ) 205void SFCave :: setSeed( int seed )
206{ 206{
207 if ( seed == -1 ) 207 if ( seed == -1 )
208 currentSeed = ((unsigned long) time((time_t *) NULL)); 208 currentSeed = ((unsigned long) time((time_t *) NULL));
209 else 209 else
210 currentSeed = seed; 210 currentSeed = seed;
211 PutSeed( currentSeed ); 211 PutSeed( currentSeed );
212} 212}
213 213
214int SFCave :: nextInt( int range ) 214int SFCave :: nextInt( int range )
215{ 215{
216 int val = (int)(Random( ) * range); 216 int val = (int)(Random( ) * range);
217 217
218 return val; 218 return val;
219 219
220} 220}
221 221
222void SFCave :: setUp() 222void SFCave :: setUp()
223{ 223{
224 score = 0; 224 score = 0;
225 offset = 0; 225 offset = 0;
226 nrFrames = 0; 226 nrFrames = 0;
227 dir = 1; 227 dir = 1;
228 thrust = 0; 228 thrust = 0;
229 229
230 if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) 230 if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
231 { 231 {
232 thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 232 thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
233 noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 233 noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
234 maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 234 maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
235 maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 235 maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
236 } 236 }
237 else if ( CURRENT_GAME_TYPE == GATES_GAME ) 237 else if ( CURRENT_GAME_TYPE == GATES_GAME )
238 { 238 {
239 thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 239 thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
240 noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 240 noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
241 maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 241 maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
242 maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 242 maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
243 } 243 }
244 else 244 else
245 { 245 {
246 thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 246 thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
247 noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 247 noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
248 maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 248 maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
249 maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 249 maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
250 } 250 }
251 251
252 crashLineLength = 0; 252 crashLineLength = 0;
253 lastGateBottomY = 0; 253 lastGateBottomY = 0;
254 254
255 user.setRect( 50, sWidth/2, 4, 4 ); 255 user.setRect( 50, sWidth/2, 4, 4 );
256 256
257 blockWidth = 20; 257 blockWidth = 20;
258 blockHeight = 70; 258 blockHeight = 70;
259 gapHeight = initialGateGaps[currentGameDifficulty]; 259 gapHeight = initialGateGaps[currentGameDifficulty];
260 gateDistance = 75; 260 gateDistance = 75;
261 nextGate = nextInt( 50 ) + gateDistance; 261 nextGate = nextInt( 50 ) + gateDistance;
262 262
263 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 263 for ( int i = 0 ; i < TRAILSIZE ; ++i )
264 { 264 {
265 trail[i].setX( -1 ); 265 trail[i].setX( -1 );
266 trail[i].setY( 0 ); 266 trail[i].setY( 0 );
267 } 267 }
268 268
269 if ( CURRENT_GAME_TYPE != FLY_GAME ) 269 if ( CURRENT_GAME_TYPE != FLY_GAME )
270 { 270 {
271 maxHeight = 50; 271 maxHeight = 50;
272 272
273 mapTop[0] = (int)(nextInt(50)) + 5; 273 mapTop[0] = (int)(nextInt(50)) + 5;
274 mapBottom[0] = (int)(nextInt(50)) + 5; 274 mapBottom[0] = (int)(nextInt(50)) + 5;
275 for ( int i = 1 ; i < MAPSIZE ; ++i ) 275 for ( int i = 1 ; i < MAPSIZE ; ++i )
276 setPoint( i ); 276 setPoint( i );
277 } 277 }
278 else 278 else
279 { 279 {
280 maxHeight = 100; 280 maxHeight = 100;
281 281
282 for ( int i = 0 ; i < MAPSIZE ; ++i ) 282 for ( int i = 0 ; i < MAPSIZE ; ++i )
283 mapBottom[i] = sHeight - 10; 283 mapBottom[i] = sHeight - 10;
284 } 284 }
285 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 285 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
286 blocks[i].setY( -1 ); 286 blocks[i].setY( -1 );
287 287
288} 288}
289 289
290void SFCave :: run() 290void SFCave :: run()
291{ 291{
292 switch ( state ) 292 switch ( state )
293 { 293 {
294 case STATE_MENU: 294 case STATE_MENU:
295 displayMenu(); 295 displayMenu();
296 break; 296 break;
297 case STATE_NEWGAME: 297 case STATE_NEWGAME:
298 setSeed( -1 ); 298 setSeed( -1 );
299 setUp(); 299 setUp();
300 draw(); 300 draw();
301 state = STATE_RUNNING; 301 state = STATE_RUNNING;
302 replay = false; 302 replay = false;
303 replayList.clear(); 303 replayList.clear();
304 break; 304 break;
305 case STATE_REPLAY: 305 case STATE_REPLAY:
306 setSeed( currentSeed ); 306 setSeed( currentSeed );
307 setUp(); 307 setUp();
308 draw(); 308 draw();
309 state = STATE_RUNNING; 309 state = STATE_RUNNING;
310 replay = true; 310 replay = true;
311 if ( replayIt ) 311 if ( replayIt )
312 delete replayIt; 312 delete replayIt;
313 replayIt = new QListIterator<int>( replayList ); 313 replayIt = new QListIterator<int>( replayList );
314 case STATE_BOSS: 314 case STATE_BOSS:
315 drawBoss(); 315 drawBoss();
316 break; 316 break;
317 317
318 case STATE_CRASHING: 318 case STATE_CRASHING:
319 case STATE_CRASHED: 319 case STATE_CRASHED:
320 draw(); 320 draw();
321 break; 321 break;
322 322
323 case STATE_RUNNING: 323 case STATE_RUNNING:
324 { 324 {
325 if ( nrFrames % 2 == 0 ) 325 if ( nrFrames % 2 == 0 )
326 handleKeys(); 326 handleKeys();
327 327
328 // Apply Game rules 328 // Apply Game rules
329 nrFrames ++; 329 nrFrames ++;
330 330
331 if ( replay ) 331 if ( replay )
332 { 332 {
333 while( replayIt->current() && *(replayIt->current()) == nrFrames ) 333 while( replayIt->current() && *(replayIt->current()) == nrFrames )
334 { 334 {
335 press = !press; 335 press = !press;
336 ++(*replayIt); 336 ++(*replayIt);
337 } 337 }
338 } 338 }
339 339
340 if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) 340 if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
341 handleGameSFCave(); 341 handleGameSFCave();
342 else if ( CURRENT_GAME_TYPE == GATES_GAME ) 342 else if ( CURRENT_GAME_TYPE == GATES_GAME )
343 handleGameGates(); 343 handleGameGates();
344 else if ( CURRENT_GAME_TYPE == FLY_GAME ) 344 else if ( CURRENT_GAME_TYPE == FLY_GAME )
345 handleGameFly(); 345 handleGameFly();
346 346
347 draw(); 347 draw();
348 break; 348 break;
349 } 349 }
350 } 350 }
351} 351}
352 352
353void SFCave :: handleGameSFCave() 353void SFCave :: handleGameSFCave()
354{ 354{
355 // Update score 355 // Update score
356 if ( nrFrames % 5 == 0 ) 356 if ( nrFrames % 5 == 0 )
357 score ++; 357 score ++;
358 358
359 if ( nrFrames % 500 == 0 ) 359 if ( nrFrames % 500 == 0 )
360 { 360 {
361 if ( maxHeight < sHeight - 100 ) 361 if ( maxHeight < sHeight - 100 )
362 { 362 {
363 maxHeight += 10; 363 maxHeight += 10;
364 364
365 // Reduce block height 365 // Reduce block height
366 if ( maxHeight > sHeight - 150 ) 366 if ( maxHeight > sHeight - 150 )
367 blockHeight -= 5; 367 blockHeight -= 5;
368 } 368 }
369 } 369 }
370 370
371 if ( nrFrames % 100 == 0 ) 371 if ( nrFrames % 100 == 0 )
372 addBlock(); 372 addBlock();
373 373
374 if ( checkCollision() ) 374 if ( checkCollision() )
375 { 375 {
376 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 376 if ( score > highestScore[currentGameType][currentGameDifficulty] )
377 { 377 {
378 highestScore[currentGameType][currentGameDifficulty] = score; 378 highestScore[currentGameType][currentGameDifficulty] = score;
379 saveScore(); 379 saveScore();
380 } 380 }
381 state = STATE_CRASHING; 381 state = STATE_CRASHING;
382 } 382 }
383 else 383 else
384 { 384 {
385 moveLandscape(); 385 moveLandscape();
386 } 386 }
387 387
388} 388}
389 389
390 390
391void SFCave :: handleGameGates() 391void SFCave :: handleGameGates()
392{ 392{
393 // Update score 393 // Update score
394 if ( nrFrames % 5 == 0 ) 394 if ( nrFrames % 5 == 0 )
395 score ++; 395 score ++;
396 396
397 // Slightly random gap distance 397 // Slightly random gap distance
398 if ( nrFrames >= nextGate ) 398 if ( nrFrames >= nextGate )
399 { 399 {
400 nextGate = nrFrames + nextInt( 50 ) + gateDistance; 400 nextGate = nrFrames + nextInt( 50 ) + gateDistance;
401 addGate(); 401 addGate();
402 } 402 }
403 403
404 if ( nrFrames % 500 == 0 ) 404 if ( nrFrames % 500 == 0 )
405 { 405 {
406 if ( gapHeight > 75 ) 406 if ( gapHeight > 75 )
407 gapHeight -= 5; 407 gapHeight -= 5;
408 } 408 }
409 409
410 if ( checkCollision() ) 410 if ( checkCollision() )
411 { 411 {
412 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 412 if ( score > highestScore[currentGameType][currentGameDifficulty] )
413 { 413 {
414 highestScore[currentGameType][currentGameDifficulty] = score; 414 highestScore[currentGameType][currentGameDifficulty] = score;
415 saveScore(); 415 saveScore();
416 } 416 }
417 state = STATE_CRASHING; 417 state = STATE_CRASHING;
418 } 418 }
419 else 419 else
420 { 420 {
421 moveLandscape(); 421 moveLandscape();
422 } 422 }
423 423
424} 424}
425 425
426void SFCave :: handleGameFly() 426void SFCave :: handleGameFly()
427{ 427{
428 if ( nrFrames % 4 == 0 ) 428 if ( nrFrames % 4 == 0 )
429 { 429 {
430 // Update score 430 // Update score
431 // get distance between landscape and ship 431 // get distance between landscape and ship
432 int diff = mapBottom[10] - user.y(); 432 int diff = mapBottom[10] - user.y();
433 433
434 // the closer the difference is to 0 means more points 434 // the closer the difference is to 0 means more points
435 if ( diff < 10 ) 435 if ( diff < 10 )
436 score += 5; 436 score += 5;
437 else if ( diff < 20 ) 437 else if ( diff < 20 )
438 score += 3; 438 score += 3;
439 else if ( diff < 30 ) 439 else if ( diff < 30 )
440 score += 2; 440 score += 2;
441 else if ( diff < 40 ) 441 else if ( diff < 40 )
442 score += 1; 442 score += 1;
443 } 443 }
444 444
445 if ( checkFlyGameCollision() ) 445 if ( checkFlyGameCollision() )
446 { 446 {
447 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 447 if ( score > highestScore[currentGameType][currentGameDifficulty] )
448 { 448 {
449 highestScore[currentGameType][currentGameDifficulty] = score; 449 highestScore[currentGameType][currentGameDifficulty] = score;
450 saveScore(); 450 saveScore();
451 } 451 }
452 state = STATE_CRASHING; 452 state = STATE_CRASHING;
453 } 453 }
454 else 454 else
455 { 455 {
456 moveFlyGameLandscape(); 456 moveFlyGameLandscape();
457 } 457 }
458} 458}
459 459
460bool SFCave :: checkFlyGameCollision() 460bool SFCave :: checkFlyGameCollision()
461{ 461{
462 if ( (user.y() + user.width()) >= mapBottom[11] ) 462 if ( (user.y() + user.width()) >= mapBottom[11] )
463 return true; 463 return true;
464 464
465 return false; 465 return false;
466} 466}
467 467
468void SFCave :: moveFlyGameLandscape() 468void SFCave :: moveFlyGameLandscape()
469{ 469{
470 offset++; 470 offset++;
471 471
472 if ( offset >= segSize ) 472 if ( offset >= segSize )
473 { 473 {
474 offset = 0; 474 offset = 0;
475 for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) 475 for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
476 mapBottom[i] = mapBottom[i+speed]; 476 mapBottom[i] = mapBottom[i+speed];
477 477
478 for ( int i = speed ; i > 0 ; --i ) 478 for ( int i = speed ; i > 0 ; --i )
479 setFlyPoint( MAPSIZE-i ); 479 setFlyPoint( MAPSIZE-i );
480 } 480 }
481} 481}
482 482
483void SFCave :: setFlyPoint( int point ) 483void SFCave :: setFlyPoint( int point )
484{ 484{
485 static int fly_difficulty_levels[] = { 5, 10, 15 }; 485 static int fly_difficulty_levels[] = { 5, 10, 15 };
486 if ( nextInt(100) >= 75 ) 486 if ( nextInt(100) >= 75 )
487 dir *= -1; 487 dir *= -1;
488 488
489 int prevPoint = mapBottom[point-1]; 489 int prevPoint = mapBottom[point-1];
490 490
491 int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) ); 491 int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) );
492 492
493 if ( nextPoint > sHeight ) 493 if ( nextPoint > sHeight )
494 { 494 {
495 nextPoint = sHeight; 495 nextPoint = sHeight;
496 dir *= -1; 496 dir *= -1;
497 } 497 }
498 else if ( nextPoint < maxHeight ) 498 else if ( nextPoint < maxHeight )
499 { 499 {
500 nextPoint = maxHeight; 500 nextPoint = maxHeight;
501 dir *= 1; 501 dir *= 1;
502 } 502 }
503 503
504 mapBottom[point] = nextPoint; 504 mapBottom[point] = nextPoint;
505} 505}
506 506
507bool SFCave :: checkCollision() 507bool SFCave :: checkCollision()
508{ 508{
509 if ( (user.y() + user.width()) >= mapBottom[11] || user.y() <= mapTop[11] ) 509 if ( (user.y() + user.width()) >= mapBottom[11] || user.y() <= mapTop[11] )
510 return true; 510 return true;
511 511
512 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 512 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
513 { 513 {
514 if ( blocks[i].y() != -1 ) 514 if ( blocks[i].y() != -1 )
515 { 515 {
516 if ( blocks[i].intersects( user ) ) 516 if ( blocks[i].intersects( user ) )
517 return true; 517 return true;
518 } 518 }
519 } 519 }
520 return false; 520 return false;
521} 521}
522 522
523void SFCave :: moveLandscape() 523void SFCave :: moveLandscape()
524{ 524{
525 offset++; 525 offset++;
526 526
527 if ( offset >= segSize ) 527 if ( offset >= segSize )
528 { 528 {
529 offset = 0; 529 offset = 0;
530 for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) 530 for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
531 { 531 {
532 mapTop[i] = mapTop[i+speed]; 532 mapTop[i] = mapTop[i+speed];
533 mapBottom[i] = mapBottom[i+speed]; 533 mapBottom[i] = mapBottom[i+speed];
534 } 534 }
535 535
536 for ( int i = speed ; i > 0 ; --i ) 536 for ( int i = speed ; i > 0 ; --i )
537 setPoint( MAPSIZE-i ); 537 setPoint( MAPSIZE-i );
538 } 538 }
539 539
540 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 540 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
541 { 541 {
542 if ( blocks[i].y() != -1 ) 542 if ( blocks[i].y() != -1 )
543 { 543 {
544 blocks[i].moveBy( -speed, 0 ); 544 blocks[i].moveBy( -speed, 0 );
545 if ( blocks[i].x() + blocks[i].width() < 0 ) 545 if ( blocks[i].x() + blocks[i].width() < 0 )
546 blocks[i].setY( -1 ); 546 blocks[i].setY( -1 );
547 } 547 }
548 } 548 }
549} 549}
550 550
551void SFCave :: addBlock() 551void SFCave :: addBlock()
552{ 552{
553 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 553 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
554 { 554 {
555 if ( blocks[i].y() == -1 ) 555 if ( blocks[i].y() == -1 )
556 { 556 {
557 int x = sWidth; 557 int x = sWidth;
558 558
559 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); 559 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight));
560 560
561 blocks[i].setRect( x, y, blockWidth, blockHeight ); 561 blocks[i].setRect( x, y, blockWidth, blockHeight );
562 562
563 break; 563 break;
564 } 564 }
565 } 565 }
566} 566}
567 567
568void SFCave :: addGate() 568void SFCave :: addGate()
569{ 569{
570 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 570 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
571 { 571 {
572 if ( blocks[i].y() == -1 ) 572 if ( blocks[i].y() == -1 )
573 { 573 {
574 int x1 = sWidth; 574 int x1 = sWidth;
575 int y1 = mapTop[50]; 575 int y1 = mapTop[50];
576 int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); 576 int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight);
577 577
578 // See if height between last gate and this one is too big 578 // See if height between last gate and this one is too big
579 if ( b1Height - 100 > lastGateBottomY ) 579 if ( b1Height - 100 > lastGateBottomY )
580 b1Height -= 25; 580 b1Height -= 25;
581 else if ( b1Height + 100 < lastGateBottomY ) 581 else if ( b1Height + 100 < lastGateBottomY )
582 b1Height += 25; 582 b1Height += 25;
583 lastGateBottomY = b1Height; 583 lastGateBottomY = b1Height;
584 584
585 585
586 int x2 = sWidth; 586 int x2 = sWidth;
587 int y2 = y1 + b1Height + gapHeight; 587 int y2 = y1 + b1Height + gapHeight;
588 int b2Height = mapBottom[50] - y2; 588 int b2Height = mapBottom[50] - y2;
589 589
590 590
591 blocks[i].setRect( x1, y1, blockWidth, b1Height ); 591 blocks[i].setRect( x1, y1, blockWidth, b1Height );
592 blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); 592 blocks[i+1].setRect( x2, y2, blockWidth, b2Height );
593 593
594 break; 594 break;
595 } 595 }
596 } 596 }
597} 597}
598 598
599void SFCave :: setPoint( int point ) 599void SFCave :: setPoint( int point )
600{ 600{
601 if ( nextInt(100) >= 80 ) 601 if ( nextInt(100) >= 80 )
602 dir *= -1; 602 dir *= -1;
603 603
604 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); 604 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) );
605 if ( mapTop[point] < 0 ) 605 if ( mapTop[point] < 0 )
606 { 606 {
607 mapTop[point] = 0; 607 mapTop[point] = 0;
608 dir *= -1; 608 dir *= -1;
609 } 609 }
610 else if ( mapTop[point] >= maxHeight ) 610 else if ( mapTop[point] >= maxHeight )
611 { 611 {
612 mapTop[point] = maxHeight; 612 mapTop[point] = maxHeight;
613 dir *= -1; 613 dir *= -1;
614 } 614 }
615 615
616// mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); 616// mapBottom[point] = sHeight - (maxHeight - mapBottom[point]);
617 mapBottom[point] = sHeight - (maxHeight - mapTop[point]); 617 mapBottom[point] = sHeight - (maxHeight - mapTop[point]);
618} 618}
619 619
620void SFCave :: drawBoss() 620void SFCave :: drawBoss()
621{ 621{
622 offscreen->fill( Qt::black ); 622 offscreen->fill( Qt::black );
623 623
624 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 624 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
625} 625}
626 626
627void SFCave :: draw() 627void SFCave :: draw()
628{ 628{
629 //printf( "Paint\n" ); 629 //printf( "Paint\n" );
630 offscreen->fill( Qt::black ); 630 offscreen->fill( Qt::black );
631 631
632 QPainter p( offscreen ); 632 QPainter p( offscreen );
633 QFontMetrics fm = p.fontMetrics(); 633 QFontMetrics fm = p.fontMetrics();
634 p.setPen( Qt::white ); 634 p.setPen( Qt::white );
635 635
636 for ( int i = 0 ; i < MAPSIZE -3; ++i ) 636 for ( int i = 0 ; i < MAPSIZE -3; ++i )
637 { 637 {
638 // Only display top landscape if not running FLY_GAME 638 // Only display top landscape if not running FLY_GAME
639 if ( CURRENT_GAME_TYPE != FLY_GAME ) 639 if ( CURRENT_GAME_TYPE != FLY_GAME )
640 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); 640 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] );
641 641
642 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); 642 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] );
643 643
644 if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones ) 644 if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones )
645 { 645 {
646 p.setPen( Qt::red ); 646 p.setPen( Qt::red );
647 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 ); 647 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 );
648 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 ); 648 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 );
649 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 ); 649 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 );
650 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 ); 650 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 );
651 p.setPen( Qt::white ); 651 p.setPen( Qt::white );
652 } 652 }
653 } 653 }
654 654
655 // Uncomment this to show user segment (usful for checking collision boundary with landscape 655 // Uncomment this to show user segment (usful for checking collision boundary with landscape
656// p.setPen( Qt::red ); 656// p.setPen( Qt::red );
657// p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight ); 657// p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight );
658// p.setPen( Qt::white ); 658// p.setPen( Qt::white );
659 659
660 // Draw user 660 // Draw user
661 p.drawRect( user ); 661 p.drawRect( user );
662 662
663 // Draw trails 663 // Draw trails
664 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 664 for ( int i = 0 ; i < TRAILSIZE ; ++i )
665 if ( trail[i].x() >= 0 ) 665 if ( trail[i].x() >= 0 )
666 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); 666 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 );
667 667
668 // Draw blocks 668 // Draw blocks
669 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 669 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
670 if ( blocks[i].y() != -1 ) 670 if ( blocks[i].y() != -1 )
671 { 671 {
672 p.fillRect( blocks[i], Qt::black ); 672 p.fillRect( blocks[i], Qt::black );
673 p.drawRect( blocks[i] ); 673 p.drawRect( blocks[i] );
674 } 674 }
675 675
676 // draw score 676 // draw score
677 QString s; 677 QString s;
678 s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); 678 s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] );
679 p.drawText( 5, 10, s ); 679 p.drawText( 5, 10, s );
680 680
681 681
682 if ( state == STATE_CRASHING || state == STATE_CRASHED ) 682 if ( state == STATE_CRASHING || state == STATE_CRASHED )
683 { 683 {
684 // add next crash line 684 // add next crash line
685 685
686 if ( crashLineLength != -1 ) 686 if ( crashLineLength != -1 )
687 { 687 {
688 for ( int i = 0 ; i < 36 ; ++i ) 688 for ( int i = 0 ; i < 36 ; ++i )
689 { 689 {
690 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); 690 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) );
691 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); 691 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y );
692 } 692 }
693 } 693 }
694 694
695 if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) 695 if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) )
696 state = STATE_CRASHED; 696 state = STATE_CRASHED;
697 697
698 if ( state == STATE_CRASHED ) 698 if ( state == STATE_CRASHED )
699 { 699 {
700 QString text = "Press up or down to start"; 700 QString text = "Press up or down to start";
701 p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); 701 p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text );
702 702
703 text = "Press OK for menu"; 703 text = "Press OK for menu";
704 p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); 704 p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text );
705/* 705/*
706 text = "Press r to replay"; 706 text = "Press r to replay";
707 p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); 707 p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text );
708 708
709 text = "Press s to save the replay"; 709 text = "Press s to save the replay";
710 p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); 710 p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text );
711 711
712 text = "Press r to load a saved replay"; 712 text = "Press r to load a saved replay";
713 p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); 713 p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text );
714*/ 714*/
715 } 715 }
716 else 716 else
717 crashLineLength ++; 717 crashLineLength ++;
718 } 718 }
719 719
720 p.end(); 720 p.end();
721 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 721 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
722 //printf( "endpaint\n" ); 722 //printf( "endpaint\n" );
723} 723}
724 724
725void SFCave :: handleKeys() 725void SFCave :: handleKeys()
726{ 726{
727 // Find enpty trail and move others 727 // Find enpty trail and move others
728 bool done = false; 728 bool done = false;
729 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 729 for ( int i = 0 ; i < TRAILSIZE ; ++i )
730 { 730 {
731 if ( trail[i].x() < 0 ) 731 if ( trail[i].x() < 0 )
732 { 732 {
733 if ( !done ) 733 if ( !done )
734 { 734 {
735 trail[i].setX( user.x() - 5 ); 735 trail[i].setX( user.x() - 5 );
736 trail[i].setY( user.y() ); 736 trail[i].setY( user.y() );
737 done = true; 737 done = true;
738 } 738 }
739 } 739 }
740 else 740 else
741 { 741 {
742 trail[i].setX( trail[i].x() - (2) ); 742 trail[i].setX( trail[i].x() - (2) );
743 } 743 }
744 } 744 }
745 745
746 if ( speed <= 3 ) 746 if ( speed <= 3 )
747 { 747 {
748 if ( press ) 748 if ( press )
749 thrust -= thrustUp; 749 thrust -= thrustUp;
750 else 750 else
751 thrust += noThrust; 751 thrust += noThrust;
752 752
753 if ( thrust > maxDownThrust ) 753 if ( thrust > maxDownThrust )
754 thrust = maxDownThrust; 754 thrust = maxDownThrust;
755 else if ( thrust < maxUpThrust ) 755 else if ( thrust < maxUpThrust )
756 thrust = maxUpThrust; 756 thrust = maxUpThrust;
757 } 757 }
758 else 758 else
759 { 759 {
760 if ( press ) 760 if ( press )
761 thrust -= 0.5; 761 thrust -= 0.5;
762 else 762 else
763 thrust += 0.8; 763 thrust += 0.8;
764 764
765 if ( thrust > 5.0 ) 765 if ( thrust > 5.0 )
766 thrust = 5.0; 766 thrust = 5.0;
767 else if ( thrust < -3.5 ) 767 else if ( thrust < -3.5 )
768 thrust = -3.5; 768 thrust = -3.5;
769 } 769 }
770 user.moveBy( 0, (int)thrust ); 770 user.moveBy( 0, (int)thrust );
771} 771}
772 772
773void SFCave :: keyPressEvent( QKeyEvent *e ) 773void SFCave :: keyPressEvent( QKeyEvent *e )
774{ 774{
775 if ( state == STATE_MENU ) 775 if ( state == STATE_MENU )
776 handleMenuKeys( e ); 776 handleMenuKeys( e );
777 else 777 else
778 { 778 {
779 switch( e->key() ) 779 switch( e->key() )
780 { 780 {
781 case Qt::Key_Up: 781 case Qt::Key_Up:
782 case Qt::Key_F9: 782 case Qt::Key_F9:
783 case Qt::Key_Space: 783 case Qt::Key_Space:
784 if ( !replay && !press ) 784 if ( !replay && !press )
785 { 785 {
786 press = true; 786 press = true;
787 replayList.append( new int( nrFrames ) ); 787 replayList.append( new int( nrFrames ) );
788 } 788 }
789 break; 789 break;
790 case Qt::Key_M: 790 case Qt::Key_M:
791 case Qt::Key_Return: 791 case Qt::Key_Return:
792 case Qt::Key_Enter: 792 case Qt::Key_Enter:
793 if ( state == STATE_CRASHED ) 793 if ( state == STATE_CRASHED )
794 { 794 {
795 state = STATE_MENU; 795 state = STATE_MENU;
796 currentMenuNr = 0; 796 currentMenuNr = 0;
797 currentMenuOption[currentMenuNr] = 0; 797 currentMenuOption[currentMenuNr] = 0;
798 } 798 }
799 break; 799 break;
800 800
801 case Qt::Key_Z: 801 case Qt::Key_Z:
802 showScoreZones = !showScoreZones; 802 showScoreZones = !showScoreZones;
803 break; 803 break;
804 804
805 default: 805 default:
806 e->ignore(); 806 e->ignore();
807 break; 807 break;
808 } 808 }
809 } 809 }
810} 810}
811 811
812void SFCave :: keyReleaseEvent( QKeyEvent *e ) 812void SFCave :: keyReleaseEvent( QKeyEvent *e )
813{ 813{
814 if ( state == STATE_MENU ) 814 if ( state == STATE_MENU )
815 { 815 {
816 } 816 }
817 else 817 else
818 { 818 {
819 switch( e->key() ) 819 switch( e->key() )
820 { 820 {
821 case Qt::Key_F9: 821 case Qt::Key_F9:
822 case Qt::Key_Space: 822 case Qt::Key_Space:
823 case Qt::Key_Up: 823 case Qt::Key_Up:
824 if ( !replay && press ) 824 if ( !replay && press )
825 { 825 {
826 press = false; 826 press = false;
827 827
828 replayList.append( new int( nrFrames ) ); 828 replayList.append( new int( nrFrames ) );
829 } 829 }
830 break; 830 break;
831 831
832 case Qt::Key_R: 832 case Qt::Key_R:
833 if ( state == STATE_CRASHED ) 833 if ( state == STATE_CRASHED )
834 { 834 {
835 state = STATE_REPLAY; 835 state = STATE_REPLAY;
836 } 836 }
837 break; 837 break;
838 838
839 case Qt::Key_Down: 839 case Qt::Key_Down:
840 if ( state == STATE_CRASHED ) 840 if ( state == STATE_CRASHED )
841 state = STATE_NEWGAME; 841 state = STATE_NEWGAME;
842 break; 842 break;
843 843
844 case Qt::Key_S: 844 case Qt::Key_S:
845 if ( state == STATE_CRASHED ) 845 if ( state == STATE_CRASHED )
846 saveReplay(); 846 saveReplay();
847 break; 847 break;
848 848
849 case Qt::Key_L: 849 case Qt::Key_L:
850 if ( state == STATE_CRASHED ) 850 if ( state == STATE_CRASHED )
851 loadReplay(); 851 loadReplay();
852 break; 852 break;
853 default: 853 default:
854 e->ignore(); 854 e->ignore();
855 break; 855 break;
856 } 856 }
857 } 857 }
858 858
859} 859}
860 860
861 861
862void SFCave :: saveScore() 862void SFCave :: saveScore()
863{ 863{
864#ifdef QWS 864#ifdef QWS
865 Config cfg( "sfcave" ); 865 Config cfg( "sfcave" );
866 cfg.setGroup( "settings" ); 866 cfg.setGroup( "settings" );
867 QString key = "highScore_"; 867 QString key = "highScore_";
868 868
869 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); 869 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] );
870 key += CURRENT_GAME_TYPE; 870 key += CURRENT_GAME_TYPE;
871 cfg.writeEntry( key, highestScore[currentGameType] ); 871 cfg.writeEntry( key, highestScore[currentGameType] );
872#endif 872#endif
873} 873}
874 874
875void SFCave :: saveReplay() 875void SFCave :: saveReplay()
876{ 876{
877 FILE *out; 877 FILE *out;
878 out = fopen( (const char *)replayFile, "w" ); 878 out = fopen( (const char *)replayFile, "w" );
879 if ( !out ) 879 if ( !out )
880 { 880 {
881 printf( "Couldn't write to /home/root/sfcave.replay\n" ); 881 printf( "Couldn't write to /home/root/sfcave.replay\n" );
882 return; 882 return;
883 } 883 }
884 884
885 // Build up string of values 885 // Build up string of values
886 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... 886 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>.......
887 QString val; 887 QString val;
888 val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty ); 888 val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty );
889 889
890 QListIterator<int> it( replayList ); 890 QListIterator<int> it( replayList );
891 while( it.current() ) 891 while( it.current() )
892 { 892 {
893 QString tmp; 893 QString tmp;
894 tmp.sprintf( "%d ", (*it.current()) ); 894 tmp.sprintf( "%d ", (*it.current()) );
895 val += tmp; 895 val += tmp;
896 896
897 ++it; 897 ++it;
898 } 898 }
899 val += "\n"; 899 val += "\n";
900 900
901 QString line; 901 QString line;
902 line.sprintf( "%d\n", val.length() ); 902 line.sprintf( "%d\n", val.length() );
903 fwrite( (const char *)line, 1, line.length(), out ); 903 fwrite( (const char *)line, 1, line.length(), out );
904 904
905 fwrite( (const char *)val, 1, val.length(), out ); 905 fwrite( (const char *)val, 1, val.length(), out );
906 906
907 fclose( out ); 907 fclose( out );
908 908
909 printf( "Replay saved to %s\n", (const char *)replayFile ); 909 printf( "Replay saved to %s\n", (const char *)replayFile );
910 910
911} 911}
912 912
913void SFCave :: loadReplay() 913void SFCave :: loadReplay()
914{ 914{
915 FILE *in = fopen( (const char *)replayFile, "r" ); 915 FILE *in = fopen( (const char *)replayFile, "r" );
916 916
917 if ( in == 0 ) 917 if ( in == 0 )
918 { 918 {
919 printf( "Couldn't load replay file!\n" ); 919 printf( "Couldn't load replay file!\n" );
920 return; 920 return;
921 } 921 }
922 922
923 // Read next line - contains the size of the options 923 // Read next line - contains the size of the options
924 char line[10+1]; 924 char line[10+1];
925 fgets( line, 10, in ); 925 fgets( line, 10, in );
926 926
927 int length = -1; 927 int length = -1;
928 sscanf( line, "%d", &length ); 928 sscanf( line, "%d", &length );
929 char *data = new char[length+1]; 929 char *data = new char[length+1];
930 930
931 fread( data, 1, length, in ); 931 fread( data, 1, length, in );
932// printf( "data - %s", data ); 932// printf( "data - %s", data );
933 933
934 QString sep = " "; 934 QString sep = " ";
935 QStringList list = QStringList::split( sep, QString( data ) ); 935 QStringList list = QStringList::split( sep, QString( data ) );
936 936
937 // print it out 937 // print it out
938 QStringList::Iterator it = list.begin(); 938 QStringList::Iterator it = list.begin();
939 currentSeed = (*it).toInt(); 939 currentSeed = (*it).toInt();
940 ++it; 940 ++it;
941 currentGameType = (*it).toInt(); 941 currentGameType = (*it).toInt();
942 ++it; 942 ++it;
943 currentGameDifficulty = (*it).toInt(); 943 currentGameDifficulty = (*it).toInt();
944 ++it; 944 ++it;
945 945
946 replayList.clear(); 946 replayList.clear();
947 for ( ; it != list.end(); ++it ) 947 for ( ; it != list.end(); ++it )
948 { 948 {
949 int v = (*it).toInt(); 949 int v = (*it).toInt();
950 replayList.append( new int( v ) ); 950 replayList.append( new int( v ) );
951 } 951 }
952 952
953 delete data; 953 delete data;
954 954
955 fclose( in ); 955 fclose( in );
956 956
957 printf( "Replay loaded from %s\n", (const char *)replayFile ); 957 printf( "Replay loaded from %s\n", (const char *)replayFile );
958} 958}
959 959
960 960
961//--------------- MENU CODE --------------------- 961//--------------- MENU CODE ---------------------
962void SFCave :: handleMenuKeys( QKeyEvent *e ) 962void SFCave :: handleMenuKeys( QKeyEvent *e )
963{ 963{
964 switch( e->key() ) 964 switch( e->key() )
965 { 965 {
966 case Qt::Key_Down: 966 case Qt::Key_Down:
967 currentMenuOption[currentMenuNr] ++; 967 currentMenuOption[currentMenuNr] ++;
968 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) 968 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" )
969 currentMenuOption[currentMenuNr] = 0; 969 currentMenuOption[currentMenuNr] = 0;
970 break; 970 break;
971 case Qt::Key_Up: 971 case Qt::Key_Up:
972 currentMenuOption[currentMenuNr] --; 972 currentMenuOption[currentMenuNr] --;
973 if ( currentMenuOption[currentMenuNr] < 0 ) 973 if ( currentMenuOption[currentMenuNr] < 0 )
974 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; 974 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1;
975 break; 975 break;
976 976
977 case Qt::Key_Left: 977 case Qt::Key_Left:
978 if ( currentMenuNr == MENU_OPTIONS_MENU ) 978 if ( currentMenuNr == MENU_OPTIONS_MENU )
979 { 979 {
980 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 980 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
981 { 981 {
982 currentGameType --; 982 currentGameType --;
983 if ( currentGameType < 0 ) 983 if ( currentGameType < 0 )
984 currentGameType = NR_GAME_TYPES - 1; 984 currentGameType = NR_GAME_TYPES - 1;
985 } 985 }
986 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 986 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
987 { 987 {
988 currentGameDifficulty --; 988 currentGameDifficulty --;
989 if ( currentGameDifficulty < 0 ) 989 if ( currentGameDifficulty < 0 )
990 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; 990 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1;
991 } 991 }
992 } 992 }
993 break; 993 break;
994 994
995 case Qt::Key_Right: 995 case Qt::Key_Right:
996 if ( currentMenuNr == MENU_OPTIONS_MENU ) 996 if ( currentMenuNr == MENU_OPTIONS_MENU )
997 { 997 {
998 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 998 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
999 { 999 {
1000 currentGameType ++; 1000 currentGameType ++;
1001 if ( currentGameType == NR_GAME_TYPES ) 1001 if ( currentGameType == NR_GAME_TYPES )
1002 currentGameType = 0; 1002 currentGameType = 0;
1003 } 1003 }
1004 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 1004 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
1005 { 1005 {
1006 currentGameDifficulty ++; 1006 currentGameDifficulty ++;
1007 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) 1007 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES )
1008 currentGameDifficulty = 0; 1008 currentGameDifficulty = 0;
1009 } 1009 }
1010 } 1010 }
1011 break; 1011 break;
1012 1012
1013 case Qt::Key_Space: 1013 case Qt::Key_Space:
1014 case Qt::Key_Return: 1014 case Qt::Key_Return:
1015 case Qt::Key_Enter: 1015 case Qt::Key_Enter:
1016 dealWithMenuSelection(); 1016 dealWithMenuSelection();
1017 break; 1017 break;
1018 } 1018 }
1019} 1019}
1020 1020
1021void SFCave :: displayMenu() 1021void SFCave :: displayMenu()
1022{ 1022{
1023 offscreen->fill( Qt::black ); 1023 offscreen->fill( Qt::black );
1024 1024
1025 QPainter p( offscreen ); 1025 QPainter p( offscreen );
1026 p.setPen( Qt::white ); 1026 p.setPen( Qt::white );
1027 1027
1028 QFont f( "Helvetica", 16 ); 1028 QFont f( "Helvetica", 16 );
1029 p.setFont( f ); 1029 p.setFont( f );
1030 1030
1031 QFontMetrics fm = p.fontMetrics(); 1031 QFontMetrics fm = p.fontMetrics();
1032 1032
1033 QString text = "SFCave"; 1033 QString text = "SFCave";
1034 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); 1034 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );
1035 1035
1036 text = "Written by Andy Qua"; 1036 text = "Written by Andy Qua";
1037 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text ); 1037 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text );
1038 1038
1039 // Draw options 1039 // Draw options
1040 int pos = 140; 1040 int pos = 140;
1041 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) 1041 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 )
1042 { 1042 {
1043 if ( currentMenuOption[currentMenuNr] == i ) 1043 if ( currentMenuOption[currentMenuNr] == i )
1044 p.setPen( Qt::yellow ); 1044 p.setPen( Qt::yellow );
1045 else 1045 else
1046 p.setPen( Qt::white ); 1046 p.setPen( Qt::white );
1047 1047
1048 QString text; 1048 QString text;
1049 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 ) 1049 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 )
1050 { 1050 {
1051 QString val; 1051 QString val;
1052 if ( i == MENU_GAME_TYPE ) 1052 if ( i == MENU_GAME_TYPE )
1053 val = gameTypes[currentGameType]; 1053 val = gameTypes[currentGameType];
1054 else 1054 else
1055 val = dificultyOption[currentGameDifficulty]; 1055 val = dificultyOption[currentGameDifficulty];
1056 1056
1057 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val ); 1057 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val );
1058 } 1058 }
1059 else 1059 else
1060 text = menuOptions[currentMenuNr][i]; 1060 text = menuOptions[currentMenuNr][i];
1061 1061
1062 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text ); 1062 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text );
1063 } 1063 }
1064 1064
1065 p.end(); 1065 p.end();
1066 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 1066 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
1067} 1067}
1068 1068
1069void SFCave :: dealWithMenuSelection() 1069void SFCave :: dealWithMenuSelection()
1070{ 1070{
1071 switch( currentMenuNr ) 1071 switch( currentMenuNr )
1072 { 1072 {
1073 case MENU_MAIN_MENU: 1073 case MENU_MAIN_MENU:
1074 { 1074 {
1075 switch( currentMenuOption[currentMenuNr] ) 1075 switch( currentMenuOption[currentMenuNr] )
1076 { 1076 {
1077 case MENU_START_GAME: 1077 case MENU_START_GAME:
1078 state = STATE_NEWGAME; 1078 state = STATE_NEWGAME;
1079 break; 1079 break;
1080 1080
1081 case MENU_REPLAY: 1081 case MENU_REPLAY:
1082 currentMenuNr = MENU_REPLAY_MENU; 1082 currentMenuNr = MENU_REPLAY_MENU;
1083 currentMenuOption[currentMenuNr] = 0; 1083 currentMenuOption[currentMenuNr] = 0;
1084 break; 1084 break;
1085 1085
1086 case MENU_OPTIONS: 1086 case MENU_OPTIONS:
1087 currentMenuNr = MENU_OPTIONS_MENU; 1087 currentMenuNr = MENU_OPTIONS_MENU;
1088 currentMenuOption[currentMenuNr] = 0; 1088 currentMenuOption[currentMenuNr] = 0;
1089 break; 1089 break;
1090 1090
1091 case MENU_HELP: 1091 case MENU_HELP:
1092 { 1092 {
1093 // Display Help Menu 1093 // Display Help Menu
1094 HelpWindow *dlg = new HelpWindow( this ); 1094 HelpWindow *dlg = new HelpWindow( this );
1095 dlg->exec(); 1095 dlg->exec();
1096 delete dlg; 1096 delete dlg;
1097 break; 1097 break;
1098 } 1098 }
1099 1099
1100 case MENU_QUIT: 1100 case MENU_QUIT:
1101 QApplication::exit(); 1101 QApplication::exit();
1102 break; 1102 break;
1103 } 1103 }
1104 1104
1105 break; 1105 break;
1106 } 1106 }
1107 1107
1108 case MENU_OPTIONS_MENU: 1108 case MENU_OPTIONS_MENU:
1109 { 1109 {
1110 switch( currentMenuOption[currentMenuNr] ) 1110 switch( currentMenuOption[currentMenuNr] )
1111 { 1111 {
1112 case MENU_GAME_TYPE: 1112 case MENU_GAME_TYPE:
1113 break; 1113 break;
1114 1114
1115 case MENU_GAME_DIFFICULTY: 1115 case MENU_GAME_DIFFICULTY:
1116 break; 1116 break;
1117 1117
1118 case MENU_CLEAR_HIGHSCORES: 1118 case MENU_CLEAR_HIGHSCORES:
1119 for ( int i = 0 ; i < 3 ; ++i ) 1119 for ( int i = 0 ; i < 3 ; ++i )
1120 highestScore[currentGameType][i] = 0; 1120 highestScore[currentGameType][i] = 0;
1121 break; 1121 break;
1122 1122
1123 case MENU_BACK: 1123 case MENU_BACK:
1124 currentMenuNr = MENU_MAIN_MENU; 1124 currentMenuNr = MENU_MAIN_MENU;
1125 1125
1126#ifdef QWS 1126#ifdef QWS
1127 Config cfg( "sfcave" ); 1127 Config cfg( "sfcave" );
1128 cfg.setGroup( "settings" ); 1128 cfg.setGroup( "settings" );
1129 cfg.writeEntry( "difficulty", currentGameDifficulty ); 1129 cfg.writeEntry( "difficulty", currentGameDifficulty );
1130 cfg.writeEntry( "gameType", currentGameType ); 1130 cfg.writeEntry( "gameType", currentGameType );
1131#endif 1131#endif
1132 break; 1132 break;
1133 } 1133 }
1134 1134
1135 break; 1135 break;
1136 } 1136 }
1137 1137
1138 case MENU_REPLAY_MENU: 1138 case MENU_REPLAY_MENU:
1139 { 1139 {
1140 switch( currentMenuOption[currentMenuNr] ) 1140 switch( currentMenuOption[currentMenuNr] )
1141 { 1141 {
1142 case MENU_REPLAY_START: 1142 case MENU_REPLAY_START:
1143 if ( currentSeed != 0 ) 1143 if ( currentSeed != 0 )
1144 state = STATE_REPLAY; 1144 state = STATE_REPLAY;
1145 // Display No Replay 1145 // Display No Replay
1146 break; 1146 break;
1147 1147
1148 case MENU_REPLAY_LOAD: 1148 case MENU_REPLAY_LOAD:
1149 loadReplay(); 1149 loadReplay();
1150 break; 1150 break;
1151 1151
1152 case MENU_REPLAY_SAVE: 1152 case MENU_REPLAY_SAVE:
1153 saveReplay(); 1153 saveReplay();
1154 break; 1154 break;
1155 1155
1156 case MENU_REPLAY_BACK: 1156 case MENU_REPLAY_BACK:
1157 currentMenuNr = MENU_MAIN_MENU; 1157 currentMenuNr = MENU_MAIN_MENU;
1158 break; 1158 break;
1159 1159
1160 } 1160 }
1161 } 1161 }
1162 } 1162 }
1163} 1163}
1164 1164