to keep up with, and how. Modeling High
to keep up with, and how. Modeling High Score Data I would love to tell you that I’m going to show you how to create a classic arcade game high score feature in which you get to enter your name or initials, and then see them displayed for all to see. Unfortunately, the seemingly simple task of allowing a player to enter his name or initials is fairly complex from a game programming perspective. Or more accurately, it requires a significant enough sidestep from the topic at hand that I don’t want to burden you with the details. So, you’re instead going to focus on a high score list that simply keeps up with the top five scores for a game, without any personalization associated with the scores. Although this approach to keeping track of a high score list doesn’t give credit for each score, it’s still a useful means of keeping up with the top five scores for a game. Because you aren’t going to worry with storing the name of the person for each score, you only have to contend with storing away five numbers. At this point, you have to consider how many digits you need to represent the maximum possible score for a game. In the case of the Space Out game, it’s virtually impossible to score beyond four digits, which means that you can safely make a five-digit score the maximum. This also means that each score in the high score list is capable of having five digits. Of course, the number of digits in an integer in the game isn’t really a critical factor; the number of digits enters the picture when it comes time to store the scores to disk, and then read them back. The maximum number of digits in a high score is important because you’re going to store that many digits for a score even if it doesn’t need that many. This makes the scores much easier to read after you’ve written them to disk. So, as an example, if the highest score is 1250, you obviously only need four digits to represent the score. However, the score needs to be stored on disk as 01250 to make it fit within a five-digit storage space. Each of the numbers in the high score list is stored this way. Getting back to the Space Out game itself, after the high scores are read from disk, they are treated as normal integers. In fact, a simple array of integers is sufficient to represent the high score list. So, the process of reading and writing the high score list involves initializing and then storing away an array of integers. The next section gets into more detail about how exactly you use the Win32 API to read and write data using files. Storing and Retrieving High Score Data Before you learn about the specific Win32 API functions used to read and write files, let’s go over exactly what happens to the array of high score integers when they are read from and written to a file. You’ve already learned that each number in the array of integers gets converted to five digits. However, I didn’t mention that these digits aren’t actually numbers, but instead are text characters. In other words, the number 1250 gets converted to “01250″ before being written to a file. This process is known as streaming the high score data to a file because you are converting the numeric data into a stream of characters. Figure 24.1 shows what the streaming process looks like. Figure 24.1. Streaming the high scores involves padding the numbers to five digits and converting them to a stream of characters. As the figure reveals, the five high scores are converted to a stream of five-digit characters during the streaming process. Along with making it very straightforward to store the numbers to a file, streaming the numbers also makes it much easier to read them back in. More specifically, you just read five digits of characters at a time, and then convert the character string into an integer number. I’ve talked about how to read and write files in general terms, but I haven’t given you any
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services