7: // The hi score file couldn’t be
28: _iHiScores[i] = atoi(cData); 29: } 30: 31: // Close the file 32: return CloseHandle(hFile); 33: } The ReadHiScores() function reads the high score list from the HiScores.dat file. The CreateFile() function is again used to obtain a file handle, but this time an existing file is opened, as opposed to creating a new file (lines 4 and 5). If there is an error reading the file, such as if the file doesn’t exist, the high score array is simply filled with scores of 0 (lines 9 and 10). This code is very important because the first time you play the game, there won’t be a high score file. If the file is opened okay, the function starts a loop to read each score from the file (line 15). Each score is then read by calling the ReadFile() function (line 20). Simply reading the scores from the file isn’t sufficient to place the score in the high score list because the scores are read as five-digit character strings. Each score must be converted to an integer number before you can add it to the high score array (line 28). After reading and converting all the scores, the ReadFile() function finishes by calling the CloseHandle() function to close the file (line 32). Testing the Finished Product Similar to a few of its predecessors, the Space Out 4 game is quite simple to test. In fact, it’s also a fun test to perform because you need to play the game a few times and build up some scores on the high score list. Figure 24.2 shows the high score list in the game upon playing the game for the first time, which means that the list is filled with scores of 0. Figure 24.2. Prior to playing the Space Out 4 game for the first time, the high score list is full of zero scores. Keep in mind that in this figure the game still tried to read the high scores from a file, but the file didn’t exist, so the scores were zeroed out. After you play a few games, the list will start looking better as it fills out with new high scores. Whenever you exit the game, the high score list gets stored to a file. Upon restarting the game, the high score list is restored by reading the scores from the same file. Figure 24.3 shows a high score list that has just been restored from a file. Figure 24.3. The high score list was read from a file, which causes it to persist between games. The high score list really is a neat enhancement to the Space Out game because it allows you to keep track of the best games you’ve played, potentially over a long period of time. Keep in mind that you can easily clear out the high score list by simply deleting the HiScores.dat file. Summary This hour explored a facet of games that you might have overlooked, the high score list. Granted, high score lists aren’t as popular these days because arcades aren’t as important as they were in the early days of video games, but it doesn’t necessarily mean that players don’t like to know what their highest gaming achievements are. High score lists are a great way to remember the best games you’ve played, and also serve as a way for competitive gamers to share a high score with friends. Although a high score list isn’t necessary in all games, you should consider adding it to games where it makes sense. The Space Out game is a good example of a game that benefits from keeping track of high scores. This hour is the last hour in the book, and therefore serves as your send-off into the expansive world of game programming. Hopefully you’ve had fun learning the ins and outs of game construction, and are excited to embark on some game development projects of your own. Have fun!
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services