-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 19 |
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 @@ -134,208 +134,205 @@ int main( int argc, char *argv[] ) app.show(); app.start(); a.exec(); } SFCave :: SFCave( int spd, QWidget *w, char *name ) : QMainWindow( w, name ) { replayIt = 0; #ifdef QWS showMaximized(); #else resize( 240, 284 ); #endif replayFile = QDir::home().path(); replayFile += "/sfcave.replay"; printf( "%s\n", (const char *)replayFile ); sWidth = width(); sHeight = height(); segSize = sWidth/(MAPSIZE-1)+1; currentMenuNr = 0; currentGameType = 0; currentGameDifficulty = 0; setCaption( CAPTION ); showScoreZones = false; #ifdef QWS Config cfg( "sfcave" ); cfg.setGroup( "settings" ); QString key = "highScore_"; for ( int i = 0 ; i < 3 ; ++i ) { for ( int j = 0 ; j < 3 ; ++j ) highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); } currentGameType = cfg.readNumEntry( "gameType", 0 ); currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); #endif speed = spd; // Change to 2 for PC press = false; offscreen = new QPixmap( sWidth, sHeight ); offscreen->fill( Qt::black ); // setUp(); crashLineLength = -1; state = STATE_MENU; prevState = STATE_MENU; gameTimer = new QTimer( this, "game timer" ); connect( gameTimer, SIGNAL( timeout() ), this, SLOT( run() ) ); } SFCave :: ~SFCave() { } void SFCave :: start() { gameTimer->start( 10 ); } void SFCave :: setSeed( int seed ) { if ( seed == -1 ) currentSeed = ((unsigned long) time((time_t *) NULL)); else currentSeed = seed; PutSeed( currentSeed ); } int SFCave :: nextInt( int range ) { int val = (int)(Random( ) * range); return val; } void SFCave :: setUp() { score = 0; offset = 0; nrFrames = 0; dir = 1; thrust = 0; - int dist[] = { 100, 60, 40 }; if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) { thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; - if ( currentCameDifficulty == DIFICULTY_EASY ) + if ( currentGameDifficulty == DIFICULTY_EASY ) gateDistance = 100; - else if ( currentCameDifficulty == DIFICULTY_EASY ) + else if ( currentGameDifficulty == DIFICULTY_EASY ) gateDistance = 60; else gateDistance = 40; - - printf( "GD = %d\n", gateDistance ); } else if ( CURRENT_GAME_TYPE == GATES_GAME ) { thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; gateDistance = 75; nextGate = nextInt( 50 ) + gateDistance; } else { thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; } crashLineLength = 0; lastGateBottomY = 0; user.setRect( 50, sWidth/2, 4, 4 ); blockWidth = 20; blockHeight = 70; gapHeight = initialGateGaps[currentGameDifficulty]; for ( int i = 0 ; i < TRAILSIZE ; ++i ) { trail[i].setX( -1 ); trail[i].setY( 0 ); } if ( CURRENT_GAME_TYPE != FLY_GAME ) { maxHeight = 50; mapTop[0] = (int)(nextInt(50)) + 5; mapBottom[0] = (int)(nextInt(50)) + 5; for ( int i = 1 ; i < MAPSIZE ; ++i ) setPoint( i ); } else { maxHeight = 100; for ( int i = 0 ; i < MAPSIZE ; ++i ) mapBottom[i] = sHeight - 10; } for ( int i = 0 ; i < BLOCKSIZE ; ++i ) blocks[i].setY( -1 ); } void SFCave :: run() { switch ( state ) { case STATE_MENU: displayMenu(); break; case STATE_NEWGAME: setSeed( -1 ); setUp(); draw(); state = STATE_RUNNING; replay = false; replayList.clear(); break; case STATE_REPLAY: setSeed( currentSeed ); setUp(); draw(); state = STATE_RUNNING; replay = true; if ( replayIt ) delete replayIt; replayIt = new QListIterator<int>( replayList ); case STATE_BOSS: drawBoss(); break; case STATE_CRASHING: case STATE_CRASHED: draw(); break; case STATE_RUNNING: { if ( nrFrames % 2 == 0 ) handleKeys(); // Apply Game rules nrFrames ++; if ( replay ) @@ -698,238 +695,238 @@ void SFCave :: draw() for ( int i = 0 ; i < 36 ; ++i ) { int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); } } if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) state = STATE_CRASHED; if ( state == STATE_CRASHED ) { QString text = "Press up or down to start"; p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); text = "Press OK for menu"; p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); /* text = "Press r to replay"; p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); text = "Press s to save the replay"; p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); text = "Press r to load a saved replay"; p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); */ } else crashLineLength ++; } p.end(); bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); //printf( "endpaint\n" ); } void SFCave :: handleKeys() { // Find enpty trail and move others bool done = false; for ( int i = 0 ; i < TRAILSIZE ; ++i ) { if ( trail[i].x() < 0 ) { if ( !done ) { trail[i].setX( user.x() - 5 ); trail[i].setY( user.y() ); done = true; } } else { trail[i].setX( trail[i].x() - (2) ); } } if ( speed <= 3 ) { if ( press ) thrust -= thrustUp; else thrust += noThrust; if ( thrust > maxDownThrust ) thrust = maxDownThrust; else if ( thrust < maxUpThrust ) thrust = maxUpThrust; } else { if ( press ) thrust -= 0.5; else thrust += 0.8; if ( thrust > 5.0 ) thrust = 5.0; else if ( thrust < -3.5 ) thrust = -3.5; } user.moveBy( 0, (int)thrust ); } void SFCave :: keyPressEvent( QKeyEvent *e ) { if ( state == STATE_MENU ) handleMenuKeys( e ); else { switch( e->key() ) { case Qt::Key_Up: case Qt::Key_F9: case Qt::Key_Space: - if ( !replay && !press ) + if ( state == STATE_RUNNING && !replay && !press ) { press = true; replayList.append( new int( nrFrames ) ); } + else if ( state == STATE_CRASHED && e->key() == Key_Up ) + state = STATE_NEWGAME; break; case Qt::Key_M: case Qt::Key_Return: case Qt::Key_Enter: if ( state == STATE_CRASHED ) { state = STATE_MENU; currentMenuNr = 0; currentMenuOption[currentMenuNr] = 0; } break; case Qt::Key_Z: showScoreZones = !showScoreZones; break; default: e->ignore(); break; } } } void SFCave :: keyReleaseEvent( QKeyEvent *e ) { if ( state == STATE_MENU ) { } else { switch( e->key() ) { case Qt::Key_F9: case Qt::Key_Space: case Qt::Key_Up: - if ( !replay && press ) - { - press = false; - + press = false; + if ( state == STATE_RUNNING && !replay && press ) replayList.append( new int( nrFrames ) ); - } + break; case Qt::Key_R: if ( state == STATE_CRASHED ) { state = STATE_REPLAY; } break; case Qt::Key_Down: if ( state == STATE_CRASHED ) state = STATE_NEWGAME; break; case Qt::Key_S: if ( state == STATE_CRASHED ) saveReplay(); break; case Qt::Key_L: if ( state == STATE_CRASHED ) loadReplay(); break; default: e->ignore(); break; } } } void SFCave :: saveScore() { #ifdef QWS Config cfg( "sfcave" ); cfg.setGroup( "settings" ); QString key = "highScore_"; cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); key += CURRENT_GAME_TYPE; cfg.writeEntry( key, highestScore[currentGameType] ); #endif } void SFCave :: saveReplay() { FILE *out; out = fopen( (const char *)replayFile, "w" ); if ( !out ) { printf( "Couldn't write to /home/root/sfcave.replay\n" ); return; } // Build up string of values // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... QString val; val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty ); QListIterator<int> it( replayList ); while( it.current() ) { QString tmp; tmp.sprintf( "%d ", (*it.current()) ); val += tmp; ++it; } val += "\n"; QString line; line.sprintf( "%d\n", val.length() ); fwrite( (const char *)line, 1, line.length(), out ); fwrite( (const char *)val, 1, val.length(), out ); fclose( out ); printf( "Replay saved to %s\n", (const char *)replayFile ); } void SFCave :: loadReplay() { FILE *in = fopen( (const char *)replayFile, "r" ); if ( in == 0 ) { printf( "Couldn't load replay file!\n" ); return; } // Read next line - contains the size of the options char line[10+1]; fgets( line, 10, in ); |