Posted: 16th Jun 2010 18:52
Here's the code I have so far:
void displayBugsZapped(int bugs)
{
// Get the center coordinates.
int centerX = dbScreenWidth() / 2;
int centerY = dbScreenHeight() / 2;
// Enable auto refresh.
dbSyncOff();
// Delete all sprites.
dbDeleteSprite(BUG_SPRITE);
dbDeleteSprite(POINTER_SPRITE);
// Clear the screen.
dbCLS();
// Display the number of bugs zapped.
dbCenterText(centerX, centerY, "Number of bugs zapped:");
dbCenterText(centerX, centerY + 20, dbStr(bugs));
dbCenterText(centerX, centerY + 40, "Press any key...");
// Wait for the user to press a key.
dbWaitKey();
// Determine whether this is the new high score.
checkForHighScore(bugs);
// Disable auto refresh.
dbSyncOn();
}
//******************************************************
// The checkForHighScore function reads the high score *
// from the HighScore.dat file and determines whether *
// the user's score is the new high score. *
//******************************************************
void checkForHighScore(int bugs)
{
// Variable to hold the high score so far.
int highScore;
// If the HighScore.dat file exists, open it and read
// its value. Otherwise, set highScore to 0.
if ( dbFileExist("HighScore.dat") )
{
// Open the HighScore.dat file.
dbOpenToRead( HIGH_SCORE_FILE, "HighScore.dat" );
// Read the current high score.
highScore = dbReadFile(HIGH_SCORE_FILE);
// Close the file.
dbCloseFile(HIGH_SCORE_FILE);
}
else
{
highScore = 0;
}
// Determine whether the user's score is the
// new high score.
if ( bugs > highScore )
{
newHighScore(bugs);
}
}
//******************************************************
// The newHighScore file congratulates the user for *
// attaining the new high score and updates the *
// HighScore.dat file. *
//******************************************************
void newHighScore(int bugs)
{
// Get the center coordinates.
int centerX = dbScreenWidth() / 2;
int centerY = dbScreenHeight() / 2;
// Clear the screen.
dbCLS();
// Congratulate the user.
dbCenterText(centerX, centerY, "Congratulations!");
dbCenterText(centerX, centerY + 20, "That's the new high score!");
// Have the user input their name.
dbCenterText(centerX, centerY + 40, "Enter your name: ");
dbCenterText(centerX, centerY + 60, dbInput() );
// If the HighScore.dat file exists, delete it.
if ( dbFileExist("HighScore.dat") )
{
dbDeleteFile("HighScore.dat");
}
// Open the HighScore.dat file for writing.
dbOpenToWrite( HIGH_SCORE_FILE, "HighScore.dat" );
// Write the user's score to the file.
dbWriteFile(HIGH_SCORE_FILE, bugs);
// Close the file.
dbCloseFile(HIGH_SCORE_FILE);
// Wait for the user to press a key.
dbCenterText(centerX, centerY + 80, "Press any key...");
dbWaitKey();
}