which is important because it detects when a
64: // Move the car back to the start 65: _pCarSprite->SetPosition(300, 405); 66: 67: // See if the game is over 68: if (–_iNumLives == 0) 69: { 70: // Play the game over sound 71: PlaySound((LPCSTR)IDW_GAMEOVER, _hInstance, SND_ASYNC | 72: SND_RESOURCE); 73: _bGameOver = TRUE; 74: _bGameOverDelay = 150; 75: 76: // Update the hi scores 77: UpdateHiScores(); 78: } 79: } 80: 81: return FALSE; 82: } When a game ends, the SpriteCollision() function plays a game over sound and sets the game over delay, which you already know about (lines 71 74). However, what you don’t already know about is the line of code that calls the UpdateHiScores() function to give the game a chance to insert the new score in the high score list (line 77). You don’t have to worry about whether the score is high enough to make the high score list because this is determined by the UpdateHiScores() function, which is shown in Listing 24.3. Listing 24.3 The UpdateHiScores() Function Checks to See if a High Score Should Be Added to the High Score List, and Adds It if Necessary 1: void UpdateHiScores() 2: { 3: // See if the current score made the hi score list 4: int i; 5: for (i = 0; i < 5; i++) 6: { 7: if (_iScore > _iHiScores[i]) 8: break; 9: } 10: 11: // Insert the current score into the hi score list 12: if (i < 5) 13: { 14: for (int j = 4; j > i; j–) 15: { 16: _iHiScores[j] = _iHiScores[j - 1]; 17: } 18: _iHiScores[i] = _iScore; 19: } 20: } Although the code for the UpdateHiScores() function looks a little tricky at first, it really isn’t too bad. The first loop checks to see if the score is higher than any of the existing high scores (lines 5 9). If so, the second loop is entered, which handles the task of inserting the score in the correct position in the list, as well as sliding down the lower scores in the list (lines 12 19). You might notice that the list is looped through in reverse, which allows it to easily move scores down the list to make room for the new score (lines 14 17). After a space has been made, the new score is placed in the high score list (line 18). The high scores are written to a data file in the WriteHiScores() function, which is shown in Listing 24.4. Listing 24.4 The WriteHiScores() Function Writes the High Score List to the File HiScores.dat 1: BOOL WriteHiScores() 2: { 3: // Create the hi score file (HiScores.dat) for writing 4: HANDLE hFile = CreateFile(TEXT(”HiScores.dat”), GENERIC_WRITE, 0, NULL, 5: CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 6: if (hFile == INVALID_HANDLE_VALUE)
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services