summaryrefslogtreecommitdiff
authorandyq <andyq>2002-12-20 22:57:16 (UTC)
committer andyq <andyq>2002-12-20 22:57:16 (UTC)
commit12a964a2f8837ee981284f3ee902baae0c7b1f77 (patch) (unidiff)
treec89f1e29e40f8aa1d0b1f2940b7227ce6c75bf01
parent48efd0d81aad0c3d0a64baabbc46fde593eb086c (diff)
downloadopie-12a964a2f8837ee981284f3ee902baae0c7b1f77.zip
opie-12a964a2f8837ee981284f3ee902baae0c7b1f77.tar.gz
opie-12a964a2f8837ee981284f3ee902baae0c7b1f77.tar.bz2
Fixed small bugette with cursor keys
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave/sfcave.cpp19
1 files changed, 8 insertions, 11 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp
index 5633a8f..c1e4532 100644
--- a/noncore/games/sfcave/sfcave.cpp
+++ b/noncore/games/sfcave/sfcave.cpp
@@ -1,501 +1,498 @@
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.12 by AndyQ" 17#define CAPTION "SFCave 1.12 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 int dist[] = { 100, 60, 40 };
231 if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) 230 if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
232 { 231 {
233 thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 232 thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
234 noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 233 noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
235 maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 234 maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
236 maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; 235 maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];;
237 236
238 if ( currentCameDifficulty == DIFICULTY_EASY ) 237 if ( currentGameDifficulty == DIFICULTY_EASY )
239 gateDistance = 100; 238 gateDistance = 100;
240 else if ( currentCameDifficulty == DIFICULTY_EASY ) 239 else if ( currentGameDifficulty == DIFICULTY_EASY )
241 gateDistance = 60; 240 gateDistance = 60;
242 else 241 else
243 gateDistance = 40; 242 gateDistance = 40;
244
245 printf( "GD = %d\n", gateDistance );
246 } 243 }
247 else if ( CURRENT_GAME_TYPE == GATES_GAME ) 244 else if ( CURRENT_GAME_TYPE == GATES_GAME )
248 { 245 {
249 thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 246 thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
250 noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 247 noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
251 maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 248 maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
252 maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; 249 maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];;
253 gateDistance = 75; 250 gateDistance = 75;
254 nextGate = nextInt( 50 ) + gateDistance; 251 nextGate = nextInt( 50 ) + gateDistance;
255 } 252 }
256 else 253 else
257 { 254 {
258 thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 255 thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
259 noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 256 noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
260 maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 257 maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
261 maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; 258 maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty];
262 } 259 }
263 260
264 crashLineLength = 0; 261 crashLineLength = 0;
265 lastGateBottomY = 0; 262 lastGateBottomY = 0;
266 263
267 user.setRect( 50, sWidth/2, 4, 4 ); 264 user.setRect( 50, sWidth/2, 4, 4 );
268 265
269 blockWidth = 20; 266 blockWidth = 20;
270 blockHeight = 70; 267 blockHeight = 70;
271 gapHeight = initialGateGaps[currentGameDifficulty]; 268 gapHeight = initialGateGaps[currentGameDifficulty];
272 269
273 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 270 for ( int i = 0 ; i < TRAILSIZE ; ++i )
274 { 271 {
275 trail[i].setX( -1 ); 272 trail[i].setX( -1 );
276 trail[i].setY( 0 ); 273 trail[i].setY( 0 );
277 } 274 }
278 275
279 if ( CURRENT_GAME_TYPE != FLY_GAME ) 276 if ( CURRENT_GAME_TYPE != FLY_GAME )
280 { 277 {
281 maxHeight = 50; 278 maxHeight = 50;
282 279
283 mapTop[0] = (int)(nextInt(50)) + 5; 280 mapTop[0] = (int)(nextInt(50)) + 5;
284 mapBottom[0] = (int)(nextInt(50)) + 5; 281 mapBottom[0] = (int)(nextInt(50)) + 5;
285 for ( int i = 1 ; i < MAPSIZE ; ++i ) 282 for ( int i = 1 ; i < MAPSIZE ; ++i )
286 setPoint( i ); 283 setPoint( i );
287 } 284 }
288 else 285 else
289 { 286 {
290 maxHeight = 100; 287 maxHeight = 100;
291 288
292 for ( int i = 0 ; i < MAPSIZE ; ++i ) 289 for ( int i = 0 ; i < MAPSIZE ; ++i )
293 mapBottom[i] = sHeight - 10; 290 mapBottom[i] = sHeight - 10;
294 } 291 }
295 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 292 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
296 blocks[i].setY( -1 ); 293 blocks[i].setY( -1 );
297 294
298} 295}
299 296
300void SFCave :: run() 297void SFCave :: run()
301{ 298{
302 switch ( state ) 299 switch ( state )
303 { 300 {
304 case STATE_MENU: 301 case STATE_MENU:
305 displayMenu(); 302 displayMenu();
306 break; 303 break;
307 case STATE_NEWGAME: 304 case STATE_NEWGAME:
308 setSeed( -1 ); 305 setSeed( -1 );
309 setUp(); 306 setUp();
310 draw(); 307 draw();
311 state = STATE_RUNNING; 308 state = STATE_RUNNING;
312 replay = false; 309 replay = false;
313 replayList.clear(); 310 replayList.clear();
314 break; 311 break;
315 case STATE_REPLAY: 312 case STATE_REPLAY:
316 setSeed( currentSeed ); 313 setSeed( currentSeed );
317 setUp(); 314 setUp();
318 draw(); 315 draw();
319 state = STATE_RUNNING; 316 state = STATE_RUNNING;
320 replay = true; 317 replay = true;
321 if ( replayIt ) 318 if ( replayIt )
322 delete replayIt; 319 delete replayIt;
323 replayIt = new QListIterator<int>( replayList ); 320 replayIt = new QListIterator<int>( replayList );
324 case STATE_BOSS: 321 case STATE_BOSS:
325 drawBoss(); 322 drawBoss();
326 break; 323 break;
327 324
328 case STATE_CRASHING: 325 case STATE_CRASHING:
329 case STATE_CRASHED: 326 case STATE_CRASHED:
330 draw(); 327 draw();
331 break; 328 break;
332 329
333 case STATE_RUNNING: 330 case STATE_RUNNING:
334 { 331 {
335 if ( nrFrames % 2 == 0 ) 332 if ( nrFrames % 2 == 0 )
336 handleKeys(); 333 handleKeys();
337 334
338 // Apply Game rules 335 // Apply Game rules
339 nrFrames ++; 336 nrFrames ++;
340 337
341 if ( replay ) 338 if ( replay )
342 { 339 {
343 while( replayIt->current() && *(replayIt->current()) == nrFrames ) 340 while( replayIt->current() && *(replayIt->current()) == nrFrames )
344 { 341 {
345 press = !press; 342 press = !press;
346 ++(*replayIt); 343 ++(*replayIt);
347 } 344 }
348 } 345 }
349 346
350 if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) 347 if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
351 handleGameSFCave(); 348 handleGameSFCave();
352 else if ( CURRENT_GAME_TYPE == GATES_GAME ) 349 else if ( CURRENT_GAME_TYPE == GATES_GAME )
353 handleGameGates(); 350 handleGameGates();
354 else if ( CURRENT_GAME_TYPE == FLY_GAME ) 351 else if ( CURRENT_GAME_TYPE == FLY_GAME )
355 handleGameFly(); 352 handleGameFly();
356 353
357 draw(); 354 draw();
358 break; 355 break;
359 } 356 }
360 } 357 }
361} 358}
362 359
363void SFCave :: handleGameSFCave() 360void SFCave :: handleGameSFCave()
364{ 361{
365 // Update score 362 // Update score
366 if ( nrFrames % 5 == 0 ) 363 if ( nrFrames % 5 == 0 )
367 score ++; 364 score ++;
368 365
369 if ( nrFrames % 500 == 0 ) 366 if ( nrFrames % 500 == 0 )
370 { 367 {
371 if ( maxHeight < sHeight - 100 ) 368 if ( maxHeight < sHeight - 100 )
372 { 369 {
373 maxHeight += 10; 370 maxHeight += 10;
374 371
375 // Reduce block height 372 // Reduce block height
376 if ( maxHeight > sHeight - 150 ) 373 if ( maxHeight > sHeight - 150 )
377 blockHeight -= 5; 374 blockHeight -= 5;
378 } 375 }
379 } 376 }
380 377
381 if ( nrFrames % gateDistance == 0 ) 378 if ( nrFrames % gateDistance == 0 )
382 addBlock(); 379 addBlock();
383 380
384 if ( checkCollision() ) 381 if ( checkCollision() )
385 { 382 {
386 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 383 if ( score > highestScore[currentGameType][currentGameDifficulty] )
387 { 384 {
388 highestScore[currentGameType][currentGameDifficulty] = score; 385 highestScore[currentGameType][currentGameDifficulty] = score;
389 saveScore(); 386 saveScore();
390 } 387 }
391 state = STATE_CRASHING; 388 state = STATE_CRASHING;
392 } 389 }
393 else 390 else
394 { 391 {
395 moveLandscape(); 392 moveLandscape();
396 } 393 }
397 394
398} 395}
399 396
400 397
401void SFCave :: handleGameGates() 398void SFCave :: handleGameGates()
402{ 399{
403 // Update score 400 // Update score
404 if ( nrFrames % 5 == 0 ) 401 if ( nrFrames % 5 == 0 )
405 score ++; 402 score ++;
406 403
407 // Slightly random gap distance 404 // Slightly random gap distance
408 if ( nrFrames >= nextGate ) 405 if ( nrFrames >= nextGate )
409 { 406 {
410 nextGate = nrFrames + nextInt( 50 ) + gateDistance; 407 nextGate = nrFrames + nextInt( 50 ) + gateDistance;
411 addGate(); 408 addGate();
412 } 409 }
413 410
414 if ( nrFrames % 500 == 0 ) 411 if ( nrFrames % 500 == 0 )
415 { 412 {
416 if ( gapHeight > 75 ) 413 if ( gapHeight > 75 )
417 gapHeight -= 5; 414 gapHeight -= 5;
418 } 415 }
419 416
420 if ( checkCollision() ) 417 if ( checkCollision() )
421 { 418 {
422 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 419 if ( score > highestScore[currentGameType][currentGameDifficulty] )
423 { 420 {
424 highestScore[currentGameType][currentGameDifficulty] = score; 421 highestScore[currentGameType][currentGameDifficulty] = score;
425 saveScore(); 422 saveScore();
426 } 423 }
427 state = STATE_CRASHING; 424 state = STATE_CRASHING;
428 } 425 }
429 else 426 else
430 { 427 {
431 moveLandscape(); 428 moveLandscape();
432 } 429 }
433 430
434} 431}
435 432
436void SFCave :: handleGameFly() 433void SFCave :: handleGameFly()
437{ 434{
438 if ( nrFrames % 4 == 0 ) 435 if ( nrFrames % 4 == 0 )
439 { 436 {
440 // Update score 437 // Update score
441 // get distance between landscape and ship 438 // get distance between landscape and ship
442 int diff = mapBottom[10] - user.y(); 439 int diff = mapBottom[10] - user.y();
443 440
444 // the closer the difference is to 0 means more points 441 // the closer the difference is to 0 means more points
445 if ( diff < 10 ) 442 if ( diff < 10 )
446 score += 5; 443 score += 5;
447 else if ( diff < 20 ) 444 else if ( diff < 20 )
448 score += 3; 445 score += 3;
449 else if ( diff < 30 ) 446 else if ( diff < 30 )
450 score += 2; 447 score += 2;
451 else if ( diff < 40 ) 448 else if ( diff < 40 )
452 score += 1; 449 score += 1;
453 } 450 }
454 451
455 if ( checkFlyGameCollision() ) 452 if ( checkFlyGameCollision() )
456 { 453 {
457 if ( score > highestScore[currentGameType][currentGameDifficulty] ) 454 if ( score > highestScore[currentGameType][currentGameDifficulty] )
458 { 455 {
459 highestScore[currentGameType][currentGameDifficulty] = score; 456 highestScore[currentGameType][currentGameDifficulty] = score;
460 saveScore(); 457 saveScore();
461 } 458 }
462 state = STATE_CRASHING; 459 state = STATE_CRASHING;
463 } 460 }
464 else 461 else
465 { 462 {
466 moveFlyGameLandscape(); 463 moveFlyGameLandscape();
467 } 464 }
468} 465}
469 466
470bool SFCave :: checkFlyGameCollision() 467bool SFCave :: checkFlyGameCollision()
471{ 468{
472 if ( (user.y() + user.width()) >= mapBottom[11] ) 469 if ( (user.y() + user.width()) >= mapBottom[11] )
473 return true; 470 return true;
474 471
475 return false; 472 return false;
476} 473}
477 474
478void SFCave :: moveFlyGameLandscape() 475void SFCave :: moveFlyGameLandscape()
479{ 476{
480 offset++; 477 offset++;
481 478
482 if ( offset >= segSize ) 479 if ( offset >= segSize )
483 { 480 {
484 offset = 0; 481 offset = 0;
485 for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) 482 for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
486 mapBottom[i] = mapBottom[i+speed]; 483 mapBottom[i] = mapBottom[i+speed];
487 484
488 for ( int i = speed ; i > 0 ; --i ) 485 for ( int i = speed ; i > 0 ; --i )
489 setFlyPoint( MAPSIZE-i ); 486 setFlyPoint( MAPSIZE-i );
490 } 487 }
491} 488}
492 489
493void SFCave :: setFlyPoint( int point ) 490void SFCave :: setFlyPoint( int point )
494{ 491{
495 static int fly_difficulty_levels[] = { 5, 10, 15 }; 492 static int fly_difficulty_levels[] = { 5, 10, 15 };
496 if ( nextInt(100) >= 75 ) 493 if ( nextInt(100) >= 75 )
497 dir *= -1; 494 dir *= -1;
498 495
499 int prevPoint = mapBottom[point-1]; 496 int prevPoint = mapBottom[point-1];
500 497
501 int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) ); 498 int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) );
@@ -538,558 +535,558 @@ void SFCave :: moveLandscape()
538 { 535 {
539 offset = 0; 536 offset = 0;
540 for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) 537 for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
541 { 538 {
542 mapTop[i] = mapTop[i+speed]; 539 mapTop[i] = mapTop[i+speed];
543 mapBottom[i] = mapBottom[i+speed]; 540 mapBottom[i] = mapBottom[i+speed];
544 } 541 }
545 542
546 for ( int i = speed ; i > 0 ; --i ) 543 for ( int i = speed ; i > 0 ; --i )
547 setPoint( MAPSIZE-i ); 544 setPoint( MAPSIZE-i );
548 } 545 }
549 546
550 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 547 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
551 { 548 {
552 if ( blocks[i].y() != -1 ) 549 if ( blocks[i].y() != -1 )
553 { 550 {
554 blocks[i].moveBy( -speed, 0 ); 551 blocks[i].moveBy( -speed, 0 );
555 if ( blocks[i].x() + blocks[i].width() < 0 ) 552 if ( blocks[i].x() + blocks[i].width() < 0 )
556 blocks[i].setY( -1 ); 553 blocks[i].setY( -1 );
557 } 554 }
558 } 555 }
559} 556}
560 557
561void SFCave :: addBlock() 558void SFCave :: addBlock()
562{ 559{
563 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 560 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
564 { 561 {
565 if ( blocks[i].y() == -1 ) 562 if ( blocks[i].y() == -1 )
566 { 563 {
567 int x = sWidth; 564 int x = sWidth;
568 565
569 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); 566 int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight));
570 567
571 blocks[i].setRect( x, y, blockWidth, blockHeight ); 568 blocks[i].setRect( x, y, blockWidth, blockHeight );
572 569
573 break; 570 break;
574 } 571 }
575 } 572 }
576} 573}
577 574
578void SFCave :: addGate() 575void SFCave :: addGate()
579{ 576{
580 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 577 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
581 { 578 {
582 if ( blocks[i].y() == -1 ) 579 if ( blocks[i].y() == -1 )
583 { 580 {
584 int x1 = sWidth; 581 int x1 = sWidth;
585 int y1 = mapTop[50]; 582 int y1 = mapTop[50];
586 int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); 583 int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight);
587 584
588 // See if height between last gate and this one is too big 585 // See if height between last gate and this one is too big
589 if ( b1Height - 100 > lastGateBottomY ) 586 if ( b1Height - 100 > lastGateBottomY )
590 b1Height -= 25; 587 b1Height -= 25;
591 else if ( b1Height + 100 < lastGateBottomY ) 588 else if ( b1Height + 100 < lastGateBottomY )
592 b1Height += 25; 589 b1Height += 25;
593 lastGateBottomY = b1Height; 590 lastGateBottomY = b1Height;
594 591
595 592
596 int x2 = sWidth; 593 int x2 = sWidth;
597 int y2 = y1 + b1Height + gapHeight; 594 int y2 = y1 + b1Height + gapHeight;
598 int b2Height = mapBottom[50] - y2; 595 int b2Height = mapBottom[50] - y2;
599 596
600 597
601 blocks[i].setRect( x1, y1, blockWidth, b1Height ); 598 blocks[i].setRect( x1, y1, blockWidth, b1Height );
602 blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); 599 blocks[i+1].setRect( x2, y2, blockWidth, b2Height );
603 600
604 break; 601 break;
605 } 602 }
606 } 603 }
607} 604}
608 605
609void SFCave :: setPoint( int point ) 606void SFCave :: setPoint( int point )
610{ 607{
611 if ( nextInt(100) >= 80 ) 608 if ( nextInt(100) >= 80 )
612 dir *= -1; 609 dir *= -1;
613 610
614 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); 611 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) );
615 if ( mapTop[point] < 0 ) 612 if ( mapTop[point] < 0 )
616 { 613 {
617 mapTop[point] = 0; 614 mapTop[point] = 0;
618 dir *= -1; 615 dir *= -1;
619 } 616 }
620 else if ( mapTop[point] >= maxHeight ) 617 else if ( mapTop[point] >= maxHeight )
621 { 618 {
622 mapTop[point] = maxHeight; 619 mapTop[point] = maxHeight;
623 dir *= -1; 620 dir *= -1;
624 } 621 }
625 622
626// mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); 623// mapBottom[point] = sHeight - (maxHeight - mapBottom[point]);
627 mapBottom[point] = sHeight - (maxHeight - mapTop[point]); 624 mapBottom[point] = sHeight - (maxHeight - mapTop[point]);
628} 625}
629 626
630void SFCave :: drawBoss() 627void SFCave :: drawBoss()
631{ 628{
632 offscreen->fill( Qt::black ); 629 offscreen->fill( Qt::black );
633 630
634 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 631 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
635} 632}
636 633
637void SFCave :: draw() 634void SFCave :: draw()
638{ 635{
639 //printf( "Paint\n" ); 636 //printf( "Paint\n" );
640 offscreen->fill( Qt::black ); 637 offscreen->fill( Qt::black );
641 638
642 QPainter p( offscreen ); 639 QPainter p( offscreen );
643 QFontMetrics fm = p.fontMetrics(); 640 QFontMetrics fm = p.fontMetrics();
644 p.setPen( Qt::white ); 641 p.setPen( Qt::white );
645 642
646 for ( int i = 0 ; i < MAPSIZE -3; ++i ) 643 for ( int i = 0 ; i < MAPSIZE -3; ++i )
647 { 644 {
648 // Only display top landscape if not running FLY_GAME 645 // Only display top landscape if not running FLY_GAME
649 if ( CURRENT_GAME_TYPE != FLY_GAME ) 646 if ( CURRENT_GAME_TYPE != FLY_GAME )
650 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); 647 p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] );
651 648
652 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); 649 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] );
653 650
654 if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones ) 651 if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones )
655 { 652 {
656 p.setPen( Qt::red ); 653 p.setPen( Qt::red );
657 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 ); 654 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 );
658 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 ); 655 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 );
659 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 ); 656 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 );
660 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 ); 657 p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 );
661 p.setPen( Qt::white ); 658 p.setPen( Qt::white );
662 } 659 }
663 } 660 }
664 661
665 // Uncomment this to show user segment (usful for checking collision boundary with landscape 662 // Uncomment this to show user segment (usful for checking collision boundary with landscape
666// p.setPen( Qt::red ); 663// p.setPen( Qt::red );
667// p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight ); 664// p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight );
668// p.setPen( Qt::white ); 665// p.setPen( Qt::white );
669 666
670 // Draw user 667 // Draw user
671 p.drawRect( user ); 668 p.drawRect( user );
672 669
673 // Draw trails 670 // Draw trails
674 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 671 for ( int i = 0 ; i < TRAILSIZE ; ++i )
675 if ( trail[i].x() >= 0 ) 672 if ( trail[i].x() >= 0 )
676 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); 673 p.drawRect( trail[i].x(), trail[i].y(), 2, 2 );
677 674
678 // Draw blocks 675 // Draw blocks
679 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 676 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
680 if ( blocks[i].y() != -1 ) 677 if ( blocks[i].y() != -1 )
681 { 678 {
682 p.fillRect( blocks[i], Qt::black ); 679 p.fillRect( blocks[i], Qt::black );
683 p.drawRect( blocks[i] ); 680 p.drawRect( blocks[i] );
684 } 681 }
685 682
686 // draw score 683 // draw score
687 QString s; 684 QString s;
688 s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); 685 s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] );
689 p.drawText( 5, 10, s ); 686 p.drawText( 5, 10, s );
690 687
691 688
692 if ( state == STATE_CRASHING || state == STATE_CRASHED ) 689 if ( state == STATE_CRASHING || state == STATE_CRASHED )
693 { 690 {
694 // add next crash line 691 // add next crash line
695 692
696 if ( crashLineLength != -1 ) 693 if ( crashLineLength != -1 )
697 { 694 {
698 for ( int i = 0 ; i < 36 ; ++i ) 695 for ( int i = 0 ; i < 36 ; ++i )
699 { 696 {
700 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); 697 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) );
701 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); 698 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y );
702 } 699 }
703 } 700 }
704 701
705 if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) 702 if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) )
706 state = STATE_CRASHED; 703 state = STATE_CRASHED;
707 704
708 if ( state == STATE_CRASHED ) 705 if ( state == STATE_CRASHED )
709 { 706 {
710 QString text = "Press up or down to start"; 707 QString text = "Press up or down to start";
711 p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); 708 p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text );
712 709
713 text = "Press OK for menu"; 710 text = "Press OK for menu";
714 p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); 711 p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text );
715/* 712/*
716 text = "Press r to replay"; 713 text = "Press r to replay";
717 p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); 714 p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text );
718 715
719 text = "Press s to save the replay"; 716 text = "Press s to save the replay";
720 p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); 717 p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text );
721 718
722 text = "Press r to load a saved replay"; 719 text = "Press r to load a saved replay";
723 p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); 720 p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text );
724*/ 721*/
725 } 722 }
726 else 723 else
727 crashLineLength ++; 724 crashLineLength ++;
728 } 725 }
729 726
730 p.end(); 727 p.end();
731 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 728 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
732 //printf( "endpaint\n" ); 729 //printf( "endpaint\n" );
733} 730}
734 731
735void SFCave :: handleKeys() 732void SFCave :: handleKeys()
736{ 733{
737 // Find enpty trail and move others 734 // Find enpty trail and move others
738 bool done = false; 735 bool done = false;
739 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 736 for ( int i = 0 ; i < TRAILSIZE ; ++i )
740 { 737 {
741 if ( trail[i].x() < 0 ) 738 if ( trail[i].x() < 0 )
742 { 739 {
743 if ( !done ) 740 if ( !done )
744 { 741 {
745 trail[i].setX( user.x() - 5 ); 742 trail[i].setX( user.x() - 5 );
746 trail[i].setY( user.y() ); 743 trail[i].setY( user.y() );
747 done = true; 744 done = true;
748 } 745 }
749 } 746 }
750 else 747 else
751 { 748 {
752 trail[i].setX( trail[i].x() - (2) ); 749 trail[i].setX( trail[i].x() - (2) );
753 } 750 }
754 } 751 }
755 752
756 if ( speed <= 3 ) 753 if ( speed <= 3 )
757 { 754 {
758 if ( press ) 755 if ( press )
759 thrust -= thrustUp; 756 thrust -= thrustUp;
760 else 757 else
761 thrust += noThrust; 758 thrust += noThrust;
762 759
763 if ( thrust > maxDownThrust ) 760 if ( thrust > maxDownThrust )
764 thrust = maxDownThrust; 761 thrust = maxDownThrust;
765 else if ( thrust < maxUpThrust ) 762 else if ( thrust < maxUpThrust )
766 thrust = maxUpThrust; 763 thrust = maxUpThrust;
767 } 764 }
768 else 765 else
769 { 766 {
770 if ( press ) 767 if ( press )
771 thrust -= 0.5; 768 thrust -= 0.5;
772 else 769 else
773 thrust += 0.8; 770 thrust += 0.8;
774 771
775 if ( thrust > 5.0 ) 772 if ( thrust > 5.0 )
776 thrust = 5.0; 773 thrust = 5.0;
777 else if ( thrust < -3.5 ) 774 else if ( thrust < -3.5 )
778 thrust = -3.5; 775 thrust = -3.5;
779 } 776 }
780 user.moveBy( 0, (int)thrust ); 777 user.moveBy( 0, (int)thrust );
781} 778}
782 779
783void SFCave :: keyPressEvent( QKeyEvent *e ) 780void SFCave :: keyPressEvent( QKeyEvent *e )
784{ 781{
785 if ( state == STATE_MENU ) 782 if ( state == STATE_MENU )
786 handleMenuKeys( e ); 783 handleMenuKeys( e );
787 else 784 else
788 { 785 {
789 switch( e->key() ) 786 switch( e->key() )
790 { 787 {
791 case Qt::Key_Up: 788 case Qt::Key_Up:
792 case Qt::Key_F9: 789 case Qt::Key_F9:
793 case Qt::Key_Space: 790 case Qt::Key_Space:
794 if ( !replay && !press ) 791 if ( state == STATE_RUNNING && !replay && !press )
795 { 792 {
796 press = true; 793 press = true;
797 replayList.append( new int( nrFrames ) ); 794 replayList.append( new int( nrFrames ) );
798 } 795 }
796 else if ( state == STATE_CRASHED && e->key() == Key_Up )
797 state = STATE_NEWGAME;
799 break; 798 break;
800 case Qt::Key_M: 799 case Qt::Key_M:
801 case Qt::Key_Return: 800 case Qt::Key_Return:
802 case Qt::Key_Enter: 801 case Qt::Key_Enter:
803 if ( state == STATE_CRASHED ) 802 if ( state == STATE_CRASHED )
804 { 803 {
805 state = STATE_MENU; 804 state = STATE_MENU;
806 currentMenuNr = 0; 805 currentMenuNr = 0;
807 currentMenuOption[currentMenuNr] = 0; 806 currentMenuOption[currentMenuNr] = 0;
808 } 807 }
809 break; 808 break;
810 809
811 case Qt::Key_Z: 810 case Qt::Key_Z:
812 showScoreZones = !showScoreZones; 811 showScoreZones = !showScoreZones;
813 break; 812 break;
814 813
815 default: 814 default:
816 e->ignore(); 815 e->ignore();
817 break; 816 break;
818 } 817 }
819 } 818 }
820} 819}
821 820
822void SFCave :: keyReleaseEvent( QKeyEvent *e ) 821void SFCave :: keyReleaseEvent( QKeyEvent *e )
823{ 822{
824 if ( state == STATE_MENU ) 823 if ( state == STATE_MENU )
825 { 824 {
826 } 825 }
827 else 826 else
828 { 827 {
829 switch( e->key() ) 828 switch( e->key() )
830 { 829 {
831 case Qt::Key_F9: 830 case Qt::Key_F9:
832 case Qt::Key_Space: 831 case Qt::Key_Space:
833 case Qt::Key_Up: 832 case Qt::Key_Up:
834 if ( !replay && press ) 833 press = false;
835 { 834 if ( state == STATE_RUNNING && !replay && press )
836 press = false;
837
838 replayList.append( new int( nrFrames ) ); 835 replayList.append( new int( nrFrames ) );
839 } 836
840 break; 837 break;
841 838
842 case Qt::Key_R: 839 case Qt::Key_R:
843 if ( state == STATE_CRASHED ) 840 if ( state == STATE_CRASHED )
844 { 841 {
845 state = STATE_REPLAY; 842 state = STATE_REPLAY;
846 } 843 }
847 break; 844 break;
848 845
849 case Qt::Key_Down: 846 case Qt::Key_Down:
850 if ( state == STATE_CRASHED ) 847 if ( state == STATE_CRASHED )
851 state = STATE_NEWGAME; 848 state = STATE_NEWGAME;
852 break; 849 break;
853 850
854 case Qt::Key_S: 851 case Qt::Key_S:
855 if ( state == STATE_CRASHED ) 852 if ( state == STATE_CRASHED )
856 saveReplay(); 853 saveReplay();
857 break; 854 break;
858 855
859 case Qt::Key_L: 856 case Qt::Key_L:
860 if ( state == STATE_CRASHED ) 857 if ( state == STATE_CRASHED )
861 loadReplay(); 858 loadReplay();
862 break; 859 break;
863 default: 860 default:
864 e->ignore(); 861 e->ignore();
865 break; 862 break;
866 } 863 }
867 } 864 }
868 865
869} 866}
870 867
871 868
872void SFCave :: saveScore() 869void SFCave :: saveScore()
873{ 870{
874#ifdef QWS 871#ifdef QWS
875 Config cfg( "sfcave" ); 872 Config cfg( "sfcave" );
876 cfg.setGroup( "settings" ); 873 cfg.setGroup( "settings" );
877 QString key = "highScore_"; 874 QString key = "highScore_";
878 875
879 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); 876 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] );
880 key += CURRENT_GAME_TYPE; 877 key += CURRENT_GAME_TYPE;
881 cfg.writeEntry( key, highestScore[currentGameType] ); 878 cfg.writeEntry( key, highestScore[currentGameType] );
882#endif 879#endif
883} 880}
884 881
885void SFCave :: saveReplay() 882void SFCave :: saveReplay()
886{ 883{
887 FILE *out; 884 FILE *out;
888 out = fopen( (const char *)replayFile, "w" ); 885 out = fopen( (const char *)replayFile, "w" );
889 if ( !out ) 886 if ( !out )
890 { 887 {
891 printf( "Couldn't write to /home/root/sfcave.replay\n" ); 888 printf( "Couldn't write to /home/root/sfcave.replay\n" );
892 return; 889 return;
893 } 890 }
894 891
895 // Build up string of values 892 // Build up string of values
896 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... 893 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>.......
897 QString val; 894 QString val;
898 val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty ); 895 val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty );
899 896
900 QListIterator<int> it( replayList ); 897 QListIterator<int> it( replayList );
901 while( it.current() ) 898 while( it.current() )
902 { 899 {
903 QString tmp; 900 QString tmp;
904 tmp.sprintf( "%d ", (*it.current()) ); 901 tmp.sprintf( "%d ", (*it.current()) );
905 val += tmp; 902 val += tmp;
906 903
907 ++it; 904 ++it;
908 } 905 }
909 val += "\n"; 906 val += "\n";
910 907
911 QString line; 908 QString line;
912 line.sprintf( "%d\n", val.length() ); 909 line.sprintf( "%d\n", val.length() );
913 fwrite( (const char *)line, 1, line.length(), out ); 910 fwrite( (const char *)line, 1, line.length(), out );
914 911
915 fwrite( (const char *)val, 1, val.length(), out ); 912 fwrite( (const char *)val, 1, val.length(), out );
916 913
917 fclose( out ); 914 fclose( out );
918 915
919 printf( "Replay saved to %s\n", (const char *)replayFile ); 916 printf( "Replay saved to %s\n", (const char *)replayFile );
920 917
921} 918}
922 919
923void SFCave :: loadReplay() 920void SFCave :: loadReplay()
924{ 921{
925 FILE *in = fopen( (const char *)replayFile, "r" ); 922 FILE *in = fopen( (const char *)replayFile, "r" );
926 923
927 if ( in == 0 ) 924 if ( in == 0 )
928 { 925 {
929 printf( "Couldn't load replay file!\n" ); 926 printf( "Couldn't load replay file!\n" );
930 return; 927 return;
931 } 928 }
932 929
933 // Read next line - contains the size of the options 930 // Read next line - contains the size of the options
934 char line[10+1]; 931 char line[10+1];
935 fgets( line, 10, in ); 932 fgets( line, 10, in );
936 933
937 int length = -1; 934 int length = -1;
938 sscanf( line, "%d", &length ); 935 sscanf( line, "%d", &length );
939 char *data = new char[length+1]; 936 char *data = new char[length+1];
940 937
941 fread( data, 1, length, in ); 938 fread( data, 1, length, in );
942// printf( "data - %s", data ); 939// printf( "data - %s", data );
943 940
944 QString sep = " "; 941 QString sep = " ";
945 QStringList list = QStringList::split( sep, QString( data ) ); 942 QStringList list = QStringList::split( sep, QString( data ) );
946 943
947 // print it out 944 // print it out
948 QStringList::Iterator it = list.begin(); 945 QStringList::Iterator it = list.begin();
949 currentSeed = (*it).toInt(); 946 currentSeed = (*it).toInt();
950 ++it; 947 ++it;
951 currentGameType = (*it).toInt(); 948 currentGameType = (*it).toInt();
952 ++it; 949 ++it;
953 currentGameDifficulty = (*it).toInt(); 950 currentGameDifficulty = (*it).toInt();
954 ++it; 951 ++it;
955 952
956 replayList.clear(); 953 replayList.clear();
957 for ( ; it != list.end(); ++it ) 954 for ( ; it != list.end(); ++it )
958 { 955 {
959 int v = (*it).toInt(); 956 int v = (*it).toInt();
960 replayList.append( new int( v ) ); 957 replayList.append( new int( v ) );
961 } 958 }
962 959
963 delete data; 960 delete data;
964 961
965 fclose( in ); 962 fclose( in );
966 963
967 printf( "Replay loaded from %s\n", (const char *)replayFile ); 964 printf( "Replay loaded from %s\n", (const char *)replayFile );
968} 965}
969 966
970 967
971//--------------- MENU CODE --------------------- 968//--------------- MENU CODE ---------------------
972void SFCave :: handleMenuKeys( QKeyEvent *e ) 969void SFCave :: handleMenuKeys( QKeyEvent *e )
973{ 970{
974 switch( e->key() ) 971 switch( e->key() )
975 { 972 {
976 case Qt::Key_Down: 973 case Qt::Key_Down:
977 currentMenuOption[currentMenuNr] ++; 974 currentMenuOption[currentMenuNr] ++;
978 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) 975 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" )
979 currentMenuOption[currentMenuNr] = 0; 976 currentMenuOption[currentMenuNr] = 0;
980 break; 977 break;
981 case Qt::Key_Up: 978 case Qt::Key_Up:
982 currentMenuOption[currentMenuNr] --; 979 currentMenuOption[currentMenuNr] --;
983 if ( currentMenuOption[currentMenuNr] < 0 ) 980 if ( currentMenuOption[currentMenuNr] < 0 )
984 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; 981 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1;
985 break; 982 break;
986 983
987 case Qt::Key_Left: 984 case Qt::Key_Left:
988 if ( currentMenuNr == MENU_OPTIONS_MENU ) 985 if ( currentMenuNr == MENU_OPTIONS_MENU )
989 { 986 {
990 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 987 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
991 { 988 {
992 currentGameType --; 989 currentGameType --;
993 if ( currentGameType < 0 ) 990 if ( currentGameType < 0 )
994 currentGameType = NR_GAME_TYPES - 1; 991 currentGameType = NR_GAME_TYPES - 1;
995 } 992 }
996 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 993 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
997 { 994 {
998 currentGameDifficulty --; 995 currentGameDifficulty --;
999 if ( currentGameDifficulty < 0 ) 996 if ( currentGameDifficulty < 0 )
1000 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; 997 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1;
1001 } 998 }
1002 } 999 }
1003 break; 1000 break;
1004 1001
1005 case Qt::Key_Right: 1002 case Qt::Key_Right:
1006 if ( currentMenuNr == MENU_OPTIONS_MENU ) 1003 if ( currentMenuNr == MENU_OPTIONS_MENU )
1007 { 1004 {
1008 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 1005 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
1009 { 1006 {
1010 currentGameType ++; 1007 currentGameType ++;
1011 if ( currentGameType == NR_GAME_TYPES ) 1008 if ( currentGameType == NR_GAME_TYPES )
1012 currentGameType = 0; 1009 currentGameType = 0;
1013 } 1010 }
1014 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 1011 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
1015 { 1012 {
1016 currentGameDifficulty ++; 1013 currentGameDifficulty ++;
1017 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) 1014 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES )
1018 currentGameDifficulty = 0; 1015 currentGameDifficulty = 0;
1019 } 1016 }
1020 } 1017 }
1021 break; 1018 break;
1022 1019
1023 case Qt::Key_Space: 1020 case Qt::Key_Space:
1024 case Qt::Key_Return: 1021 case Qt::Key_Return:
1025 case Qt::Key_Enter: 1022 case Qt::Key_Enter:
1026 dealWithMenuSelection(); 1023 dealWithMenuSelection();
1027 break; 1024 break;
1028 } 1025 }
1029} 1026}
1030 1027
1031void SFCave :: displayMenu() 1028void SFCave :: displayMenu()
1032{ 1029{
1033 offscreen->fill( Qt::black ); 1030 offscreen->fill( Qt::black );
1034 1031
1035 QPainter p( offscreen ); 1032 QPainter p( offscreen );
1036 p.setPen( Qt::white ); 1033 p.setPen( Qt::white );
1037 1034
1038 QFont f( "Helvetica", 16 ); 1035 QFont f( "Helvetica", 16 );
1039 p.setFont( f ); 1036 p.setFont( f );
1040 1037
1041 QFontMetrics fm = p.fontMetrics(); 1038 QFontMetrics fm = p.fontMetrics();
1042 1039
1043 QString text = "SFCave"; 1040 QString text = "SFCave";
1044 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); 1041 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );
1045 1042
1046 text = "Written by Andy Qua"; 1043 text = "Written by Andy Qua";
1047 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text ); 1044 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text );
1048 1045
1049 // Draw options 1046 // Draw options
1050 int pos = 140; 1047 int pos = 140;
1051 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) 1048 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 )
1052 { 1049 {
1053 if ( currentMenuOption[currentMenuNr] == i ) 1050 if ( currentMenuOption[currentMenuNr] == i )
1054 p.setPen( Qt::yellow ); 1051 p.setPen( Qt::yellow );
1055 else 1052 else
1056 p.setPen( Qt::white ); 1053 p.setPen( Qt::white );
1057 1054
1058 QString text; 1055 QString text;
1059 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 ) 1056 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 )
1060 { 1057 {
1061 QString val; 1058 QString val;
1062 if ( i == MENU_GAME_TYPE ) 1059 if ( i == MENU_GAME_TYPE )
1063 val = gameTypes[currentGameType]; 1060 val = gameTypes[currentGameType];
1064 else 1061 else
1065 val = dificultyOption[currentGameDifficulty]; 1062 val = dificultyOption[currentGameDifficulty];
1066 1063
1067 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val ); 1064 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val );
1068 } 1065 }
1069 else 1066 else
1070 text = menuOptions[currentMenuNr][i]; 1067 text = menuOptions[currentMenuNr][i];
1071 1068
1072 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text ); 1069 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text );
1073 } 1070 }
1074 1071
1075 p.end(); 1072 p.end();
1076 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 1073 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
1077} 1074}
1078 1075
1079void SFCave :: dealWithMenuSelection() 1076void SFCave :: dealWithMenuSelection()
1080{ 1077{
1081 switch( currentMenuNr ) 1078 switch( currentMenuNr )
1082 { 1079 {
1083 case MENU_MAIN_MENU: 1080 case MENU_MAIN_MENU:
1084 { 1081 {
1085 switch( currentMenuOption[currentMenuNr] ) 1082 switch( currentMenuOption[currentMenuNr] )
1086 { 1083 {
1087 case MENU_START_GAME: 1084 case MENU_START_GAME:
1088 state = STATE_NEWGAME; 1085 state = STATE_NEWGAME;
1089 break; 1086 break;
1090 1087
1091 case MENU_REPLAY: 1088 case MENU_REPLAY:
1092 currentMenuNr = MENU_REPLAY_MENU; 1089 currentMenuNr = MENU_REPLAY_MENU;
1093 currentMenuOption[currentMenuNr] = 0; 1090 currentMenuOption[currentMenuNr] = 0;
1094 break; 1091 break;
1095 1092