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