Exercises 1. Experiment with adding more or less aliens to demo mode in the Space Out 3 game to get a different effect. You might also consider changing the AddAlien() function so that the aliens behave a little differently when in demo mode. 2. Add the car sprite to demo mode in the Space Out 3 game, and just have it move randomly back and forth on the screen to simulate a human player who isn’t too smart. Hour 24. Keeping Track of High Scores Unless you grew up during the heyday of arcade games in the 1980s, you might not have an appreciation for the sense of nerd accomplishment associated with a top spot on a game’s high score list. The high score list in arcade games serves as a public acknowledgement of who has the time, skills, and quarters to be the best of the best. If you think I’m dramatizing this a bit, keep in mind that a major plot device within a Seinfeld episode involved the character George Castanza attempting to move a Frogger game across a busy street while connecting it to a temporary battery supply to keep his high score from being lost. Even if you don’t have a large nerd ego, it can be rewarding to know that you placed within the upper ranks of those who have played a game before you. This hour shows you how to develop a high score list for the Space Out game that is saved to disk so that the scores are retained between games. In this hour, you’ll learn: Why it’s important to keep track of high scores How to represent high score data in a game How to store and retrieve high score data using a file How to add a high score list to the Space Out game The Significance of Keeping Score When you think of video games, the word “achievement” might not be the first thing that comes to mind. You might not approach video games with the idea that you could be the best player of a particular game, but people are out there who do. In fact, the concept of a professional “cyber athlete” is already alive and real companies are paying the best of the best video game players to test and promote their games. If you aspire to such a unique career, I have to warn you that thousands of hours of game play await you. I brought up the issue of “cyber athletes” because it has a lot to do with the topic of this hour, high scores. Nowadays, the best video game players are determined in national tournaments where people get together and compete head-to-head. However, in years past, the best players were known only by their three initials that appeared in the high score lists of arcade games. The high score list in a classic arcade game was quite important to many early gamers because it was their only way to show off their gaming achievements. It’s kind of sad really that high score lists aren’t as popular as they once were, but we can’t lament advances in technology. On the other hand, it doesn’t mean that high scores are entirely a thing of the past. For example, many popular games, such as Tony Hawk Pro Skater, still rely on a score to indicate how well a player performed. So, the idea of using a numeric score to measure your video game playing prowess is still valid. What has changed is that the shift away from arcade games has made it less of an issue to keep track of high scores. However, I still like the idea of a high score list even if it’s only shared between friends. This hour focuses on adding a high score list to the Space Out game that you’ve worked on in previous hours. A high score list presents a new challenge to you as a game programmer because you must store away the scores so that they can be retrieved even when a game program is closed. This requires storing the scores to disk and then reading them back later, which is a unique discipline in game programming. The first step is to figure out how to model the high score data, which means that you need to determine what you’re going
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
5: 6: // Initialize the game variables 7: _iFireInputDelay = 0; 8: _iScore = 0; 9: _iNumLives = 3; 10: _iDifficulty = 80; 11: _bGameOver = FALSE; 12: 13: if (_bDemo) 14: { 15: // Add a few aliens to the demo 16: for (int i = 0; i < 6; i++) 17: AddAlien(); 18: } 19: else 20: { 21: // Create the car sprite 22: RECT rcBounds = { 0, 0, 600, 450 }; 23: _pCarSprite = new Sprite(_pCarBitmap, rcBounds, BA_WRAP); 24: _pCarSprite->SetPosition(300, 405); 25: _pGame->AddSprite(_pCarSprite); 26: 27: // Play the background music 28: _pGame->PlayMIDISong(TEXT(”Music.mid”)); 29: } 30: } Although you might think of the NewGame() function as only being used to start a new game that you’re going to play, it is also used to start a new demo game. A demo game is a “game” that includes a few aliens and nothing else. In other words, there is no car, which eliminates the difficulty of trying to simulate a human player in code. Fortunately, the aliens in the Space Out game are interesting enough that they do a pretty good job of conveying the premise of the game without having to throw in the car. The NewGame() function actually adds six alien sprites to the game engine when the game is in demo mode (lines 13 18). One neat thing about this code is that the AddAlien() function is designed to add a random alien, which means that demo mode is different each time the game goes into it; the six aliens added are always different. Granted, this demo mode could have been made more interesting by adding the car sprite and having it fight back with the aliens, but for the sake of simplicity, you can’t help but like the approach of just adding a few aliens and letting them cruise around the game screen firing missiles on their own. Testing the Finished Product Testing demo mode in the Space Out 3 game is a little like testing the splash screen in Space Out 2 just launch the game and watch it go! Figure 23.1 shows the Space Out 3 demo mode with the aliens flying around having a good time. Figure 23.1. Demo mode in the Space Out 3 game involves several aliens flying around the game screen behind the splash screen image. As you can see, the aliens in demo mode help to demonstrate how the game is played even though they aren’t interacting directly with a simulated player through the car sprite. Like I said earlier, the car sprite would be a very nice improvement for demo mode, but the idea here is to keep things simple. Any time you can achieve a desired effect with less code, and therefore less complexity, it is a good thing. Figure 23.2 shows a game that has just ended in Space Out 3. In this figure, the game over screen is shown while the time delay is ticking away. Figure 23.2. When a game finishes in Space Out 3, the game over screen is displayed for a few seconds. The game over screen in Space Out 3 is displayed for a few seconds before the game goes back to demo mode, as shown in Figure 23.3. Figure 23.3. After the game over screen has been shown for a few seconds, the Space Out 3 game reverts back to demo mode, although each demo mode is a little different. I’m showing a shot of demo mode one more time just to demonstrate how it varies each time the game goes into it. If you compare this figure with Figure 23.1, you’ll notice that the aliens are distributed a little differently. This is a subtle detail of demo mode that helps to make the game a little more interesting.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
46: NewGame(); 47: } 48: else if (_bGameOver) 49: { 50: // Start a new game 51: NewGame(); 52: } 53: } The only changes in this function involve renaming the _bSplash variable to _bDemo (lines 3, 42, and 45). The new code in the SpriteCollision() function involves the timing delay for the game over screen. More specifically, the _iGameOverDelay variable is set to 150, which means that the game screen will be displayed for 150 cycles before the game returns to demo mode: if (–_iNumLives == 0) { // Play the game over sound PlaySound((LPCSTR)IDW_GAMEOVER, _hInstance, SND_ASYNC | SND_RESOURCE); _bGameOver = TRUE; _iGameOverDelay = 150; } Any idea how much time 150 cycles is? You know that the frame rate for the game is set at 30 frames per second, which is the same thing as saying that the game goes through 30 cycles per second. Knowing this, you can divide 150 by 30 to arrive at a delay of 5 seconds for the game over screen. Pretty neat, right? The SpriteDying() function involves an interesting change related to demo mode that you might not have thought about. Analyze Listing 23.4 and see if you can figure out why the change is necessary. Listing 23.4 The SpriteDying() Function Makes Sure Not to Play the Sound of Exploding Alien Missiles When the Game Is in Demo Mode 1: void SpriteDying(Sprite* pSprite) 2: { 3: // See if an alien missile sprite is dying 4: if (pSprite->GetBitmap() == _pBMissileBitmap || 5: pSprite->GetBitmap() == _pJMissileBitmap || 6: pSprite->GetBitmap() == _pTMissileBitmap) 7: { 8: // Play the small explosion sound 9: if (!_bDemo) 10: PlaySound((LPCSTR)IDW_SMEXPLODE, _hInstance, SND_ASYNC | 11: SND_RESOURCE | SND_NOSTOP); 12: 13: // Create a small explosion sprite at the missile’s position 14: RECT rcBounds = { 0, 0, 600, 450 }; 15: RECT rcPos = pSprite->GetPosition(); 16: Sprite* pSprite = new Sprite(_pSmExplosionBitmap, rcBounds); 17: pSprite->SetNumFrames(8, TRUE); 18: pSprite->SetPosition(rcPos.left, rcPos.top); 19: _pGame->AddSprite(pSprite); 20: } 21: } When you think about it, demo mode for a computer game shouldn’t be something that annoys you, which means that it’s probably best for it to not make a bunch of noise. Because the aliens in demo mode will be firing missiles that explode when they hit the ground, it’s necessary to quiet the missiles in the SpriteDying() function so that they don’t make noise in demo mode (lines 9 11). The NewGame() function is the last of the functions impacted by the switch to demo mode in the Space Out 3 game, and it’s also the most interesting. Listing 23.5 shows the code for the NewGame() function. Listing 23.5 The NewGame() Function Adds a Few Aliens to the Game When It Is in Demo Mode 1: void NewGame() 2: { 3: // Clear the sprites 4: _pGame->CleanupSprites();
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
18: { 19: // Draw the score 20: TCHAR szText[64]; 21: RECT rect = { 460, 0, 510, 30 }; 22: wsprintf(szText, “%d”, _iScore); 23: SetBkMode(hDC, TRANSPARENT); 24: SetTextColor(hDC, RGB(255, 255, 255)); 25: DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_RIGHT | 26: DT_VCENTER); 27: 28: // Draw the number of remaining lives (cars) 29: for (int i = 0; i < _iNumLives; i++) 30: _pSmCarBitmap->Draw(hDC, 520 + (_pSmCarBitmap->GetWidth() * i), 31: 10, TRUE); 32: 33: // Draw the game over message, if necessary 34: if (_bGameOver) 35: _pGameOverBitmap->Draw(hDC, 170, 100, TRUE); 36: } 37: } The GameCycle() function in Space Out 3 looks a lot like its predecessor, but there is a significant change you should pay close attention to. The change I’m talking about involves the fact that the sprites are drawn regardless of whether the game is in demo mode (line 10). In fact, all that is not drawn in demo mode is the score (lines 20 26), the number of remaining lives (lines 29 31), and the game over message (lines 34 and 35). Also notice that the same splash screen image is displayed when the game is in demo mode (lines 12 16), which means that the game is combining demo mode with the splash screen. This isn’t a problem, but it does mean that the game is being demonstrated behind the splash screen image. The GameCycle() function must be modified to accommodate demo mode as well. In fact, the GameCycle() function is where the timing delay is established that displays the game over screen for a period of time before reverting back to demo mode when a game ends. Listing 23.2 shows the code for the new version of the GameCycle() function. Listing 23.2 The GameCycle() Function Establishes a Timing Delay Before Moving to Demo Mode from the Game Over Screen 1: void GameCycle() 2: { 3: if (!_bGameOver) 4: { 5: if (!_bDemo) 6: { 7: // Randomly add aliens 8: if ((rand() % _iDifficulty) == 0) 9: AddAlien(); 10: } 11: 12: // Update the background 13: _pBackground->Update(); 14: 15: // Update the sprites 16: _pGame->UpdateSprites(); 17: 18: // Obtain a device context for repainting the game 19: HWND hWindow = _pGame->GetWindow(); 20: HDC hDC = GetDC(hWindow); 21: 22: // Paint the game to the offscreen device context 23: GamePaint(_hOffscreenDC); 24: 25: // Blit the offscreen bitmap to the game screen 26: BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame->GetHeight(), 27: _hOffscreenDC, 0, 0, SRCCOPY); 28: 29: // Cleanup 30: ReleaseDC(hWindow, hDC); 31: }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
32: else 33: if (–_iGameOverDelay == 0) 34: { 35: // Stop the music and switch to demo mode 36: _pGame->PauseMIDISong(); 37: _bDemo = TRUE; 38: NewGame(); 39: } 40: } In addition to changing the _bSplash variable to _bDemo (line 5), the GameCycle() function establishes a timing delay for the game over screen (lines 33 39). When this delay finishes counting down, it means that the game over message has been displayed long enough and it’s okay to go ahead and put the game in demo mode. This is accomplished by setting the _bDemo variable to TRUE (line 37), and then calling the NewGame() function to add a few alien sprites to demo mode (line 38). Notice that the MIDI music isn’t actually stopped until the game switches to demo mode (line 36), which makes sense when you consider that the game over screen is still a reflection on the last game played. Demo mode also impacts the HandleKeys() function in the Space Out 3 game, as you can see in Listing 23.3. Listing 23.3 The HandleKeys() Function Supports Demo Mode by Changing the Value of the _bDemo Variable if the Game Is Exiting Demo Mode to Start a New Game 1: void HandleKeys() 2: { 3: if (!_bGameOver && !_bDemo) 4: { 5: // Move the car based upon left/right key presses 6: POINT ptVelocity = _pCarSprite->GetVelocity(); 7: if (GetAsyncKeyState(VK_LEFT) < 0) 8: { 9: // Move left 10: ptVelocity.x = max(ptVelocity.x - 1, -4); 11: _pCarSprite->SetVelocity(ptVelocity); 12: } 13: else if (GetAsyncKeyState(VK_RIGHT) < 0) 14: { 15: // Move right 16: ptVelocity.x = min(ptVelocity.x + 2, 6); 17: _pCarSprite->SetVelocity(ptVelocity); 18: } 19: 20: // Fire missiles based upon spacebar presses 21: if ((++_iFireInputDelay > 6) && GetAsyncKeyState(VK_SPACE) < 0) 22: { 23: // Create a new missile sprite 24: RECT rcBounds = { 0, 0, 600, 450 }; 25: RECT rcPos = _pCarSprite->GetPosition(); 26: Sprite* pSprite = new Sprite(_pMissileBitmap, rcBounds, BA_DIE); 27: pSprite->SetPosition(rcPos.left + 15, 400); 28: pSprite->SetVelocity(0, -7); 29: _pGame->AddSprite(pSprite); 30: 31: // Play the missile (fire) sound 32: PlaySound((LPCSTR)IDW_MISSILE, _hInstance, SND_ASYNC | 33: SND_RESOURCE | SND_NOSTOP); 34: 35: // Reset the input delay 36: _iFireInputDelay = 0; 37: } 38: } 39: 40: // Start a new game based upon an Enter (Return) key press 41: if (GetAsyncKeyState(VK_RETURN) < 0) 42: if (_bDemo) 43: { 44: // Switch out of demo mode to start a new game 45: _bDemo = FALSE;
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
What Is Demo Mode? If you’ve ever been hesitant to buy a product, you might have been offered an opportunity to “try before you buy,” which allows you to try out a product before you spend any money on it. This sales technique is particularly useful in the automotive industry, where it is virtually impossible to justify purchasing a car without taking it for a test drive. In the early days of video games, it was important to convince game players that a game was worthy of spending a quarter for a play, so demo mode was invented. Demo mode is sort of like “try before you buy” applied to video games you get to see how a game plays before you invest any time or money playing it. In regard to computer games, “try before you buy” now typically involves downloading a limited version of the game that you really can play. However, demo mode is still a useful and worthwhile feature to consider adding to your games. The main idea behind demo mode is that it goes beyond a splash screen by showing more than just a title or stationary game graphics. Demo mode attempts to show the game actually being played, or at least show some of the main characters in the game going through the motions as you’ll see them in the game. Demo mode can be as simple or as complex as you choose, but the idea is to provide a hook to convince someone to play the game. In other words, you’re attempting to sell the game so that it looks fun and inviting. Demo mode can also be informative, similar to a splash screen. In fact, it’s not a bad idea to design demo mode so that it serves as a more interesting splash screen. Unlike a splash screen, demo mode is not something that appears at the beginning of the game, never to be seen again once the game starts. Instead, demo mode is displayed in between every game, and helps fill the space between games with interesting animations from the game. So, when a game ends, there should be a brief pause while the player is able to take in that the game is actually over, and then the game should return to demo mode. Of course, the game also starts in demo mode. In this way, demo mode replaces the splash screen for a game, and also goes a few steps beyond the role of a splash screen. The Nuts and Bolts of Demo Mode Similar to a splash screen, demo mode is a distinct mode that a game enters when a game is not underway. This mode is different from “game over” and “game not over” modes, and is entered when the game program first starts, as well as in between games. A single Boolean global variable is sufficient to keep track of when a game is in demo mode. This sounds very much like how the splash screen was managed in the previous hour. Demo mode differs from a splash screen in that it involves demonstrating the game, which means that sprites must be allowed to move around. If you recall, the splash screen for the Space Out 2 game deliberately disallowed sprites to be drawn. The key to making demo mode work in a game is to simulate a game being played without actually involving a human player. One way to do this is to start up a new game as normal, and simulate key strokes using code that is somehow timed. Of course, this also involves disabling the real keys used in the game so that the player can’t suddenly jump into a demo mode game. Although this approach can work very well, and is ultimately the ideal approach to creating a demo mode because it shows how the player interacts with other characters in a game, it is more difficult to create. The coding to replace a human player with a computer player can get tricky, and usually involves some degree of artificial intelligence programming. One work-around for this approach is to “record” the keystrokes made by a player during a real game, and store them in a file. You can then “play” the keystrokes back to recreate the demo game. This technique obviously requires some extra work, but will most likely be a lot simpler than trying to establish realistic AI for the computer player. A simpler approach to creating a demo mode for a game is to simply show how the characters in the game move around without attempting to simulate the human player in the game. In other words, you aren’t actually trying to make it look as if a human player is guiding his character through the game or otherwise interacting with the game. Instead, you’re just demonstrating how the computer-controlled characters within the game move around and interact with one another. This approach simplifies things considerably because you aren’t in a situation in which you have to try and control an otherwise human player using computer logic. Keep in mind that the whole premise of demo mode is to show off a game and make it look appealing. In many cases, it is sufficient to just show a few characters
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
within the game to achieve this goal. Creating a demo mode that shows a few characters and doesn’t actually simulate play is still somewhat of a challenge because you have to create and use sprites just as if you were starting a real game. However, in this case, the idea is to disable any interaction from a human player other than initiating a new game. If you think about it, creating a demo mode for a game such as Space Out involves several changes throughout the game to allow it to appear somewhat as if the game is being played even though there is no user interaction. The remainder of the hour focuses on how to add a demo mode to this game. Building the Space Out 3 Game You’ve already learned that in some ways demo mode is similar to a splash screen because it appears in a game when the game is not actually being played. Unlike a splash screen, however, demo mode is responsible for displaying sprites and enabling them to interact to some degree. Adding a demo mode to the Space Out game represents a programming challenge, but one that isn’t too terribly difficult to solve. The next few sections lead you through the modifications required in the game to add a demo mode. The new version of the game you’ll be creating is called Space Out 3. Writing the Game Code The best place to start with the code for the Space Out 3 game is the SpaceOut.h header file, which includes a couple of new global variables. Demo mode for the game requires two new global variables; one of which replaces the _bSplash global variable that you added in the last hour for the splash screen. Following are the new global variables necessary to support demo mode in the Space Out 3 game: BOOL _bDemo; int _iGameOverDelay; The first variable, _bDemo, is very similar to the _bSplash variable in the Space Out 2 game from the previous hour. In fact, it serves virtually the same purpose in this game, except that demo mode impacts more code than the splash screen. The _iGameOverDelay global variable is used to provide a delay between when a game ends and when the game enters demo mode. This allows the player to take a moment and realize that the game is over before it cuts back to demo mode. The first function of particular interest in the Space Out 3 game is the GameStart() function, which shouldn’t come as too much of a surprise. The only change to the GameStart() function is the initialization of the _bDemo variable. The GamePaint() function is where the code for the Space Out 3 game starts to diverge more significantly from the Space Out 2 game. However, the change is still somewhat subtle in that all the same code is here, it’s just organized a little differently. Take a look at Listing 23.1 to see what I mean. Listing 23.1 The GamePaint() Function Draws the Game Graphics While Taking into Consideration Demo Mode 1: void GamePaint(HDC hDC) 2: { 3: // Draw the background 4: _pBackground->Draw(hDC); 5: 6: // Draw the desert bitmap 7: _pDesertBitmap->Draw(hDC, 0, 371); 8: 9: // Draw the sprites 10: _pGame->DrawSprites(hDC); 11: 12: if (_bDemo) 13: { 14: // Draw the splash screen image 15: _pSplashBitmap->Draw(hDC, 142, 100, TRUE); 16: } 17: else
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
they are displayed in succession after pausing a few seconds on each one. This is carried out programmatically by adding additional splash screen modes to a game so that you know which splash screen image should be displayed. In practice, this could be accomplished by using the existing _bSplash variable to determine whether any splash screen image is displayed, and then use an integer variable to keep track of which splash screen bitmap is displayed from an array of bitmaps. You would have to add some timing code to the GameCycle() function to flip between splash screen images after a brief delay, or allow the user to press a key to move between each. Workshop The Workshop is designed to help you anticipate possible questions, review what you’ve learned, and begin learning how to put your knowledge into practice. The answers to the quiz can be found in Appendix A, “Quiz Answers.” Quiz 1: In addition to the title of the game, what kind of information is good to place on the splash screen for a game? 2: What is the purpose of the _bSplash global variable in the Space Out 2 game? 3: Why are the background and the desert bitmaps drawn in the Space Out 2 game regardless of whether the game is in splash screen mode? Exercises 1. Create your own splash screen bitmap image for the Space Out 2 game and incorporate it into the game. 2. Modify the splash screen for the Space Out 2 game so that it consists of two splash screen modes that display two images in succession for the splash screen. In other words, create one splash screen image for the game’s title and another that describes how to play the game and what keys to use. Hour 23. Showing Off Your Game with Demo Mode In the previous hour, you learned how important it is to display a splash screen that serves as a title for your games. This hour continues in the theme of making a game more complete by demonstrating the importance of demo mode. Demo mode is an animated sequence displayed when you’re not playing the game that demonstrates how the game is played. Demo mode can be as simple as showing some of the game creatures moving around, or as complex as showing an entire simulated game being played. Demo mode is important because it gives a player a glimpse at how a game actually plays, which is a considerable step beyond the role of a splash screen. In this hour, you’ll learn: Why demo mode is useful in showing people how a game works What is involved in adding a demo mode to a game How to add a demo mode to the Space Out game
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
51: NewGame(); 52: } 53: } Within the code that responds to the Enter (Return) key, you’ll find the code that checks to see if the game is leaving splash screen mode. This code first checks to see if the game is in splash screen mode (line 42), and then clears the _bSplash variable and starts a new game if so (lines 45 and 46). If the game is not in splash screen mode, the function checks to see if the game is over (line 48). If the game is over, a new game is started (line 51), which is similar to how the HandleKeys() function worked prior to adding the splash screen code. Testing the Finished Product The Space Out 2 game represents one of the easiest tests you’ll ever perform on a game. All you really have to do is run the game and make sure that the splash screen appears properly. Of course, you also need to make sure that the splash screen goes away when you press the Enter key to start a new game, but you get the idea. Figure 22.2 shows the splash screen displayed in the Space Out 2 game when the game first starts. Figure 22.2. The splash screen in the Space Out 2 game presents the game title, copyright information, and information about how to start the game. Keep in mind that you are free to get as fancy as you want with the splash screen image for your own games. I chose to keep the Space Out 2 splash screen relatively simple, but you might want to jazz it up to suit your own tastes. The important thing to note about this example is that the splash screen is properly displayed when the game starts. Summary This hour introduced you to an important feature of most games the splash screen. Not only is the splash screen important in terms of conveying useful information about a game, such as a copyright notice, but it also serves as the player’s first glimpse at what your game looks like. You can take advantage of a splash screen to dramatize the theme of your game and even exaggerate the game graphics. For example, it was common for arcade games to include a fully illustrated splash screen even though the graphics in the actual games were simpler. This is an engaging way to grab the player’s attention and get him interested in the game. The next hour builds on what you learned about splash screens and shows you how to include a demo mode in your games. A demo mode is similar to a splash screen, but its goal is to provide a glimpse at how a game actually plays. Q&A Q1: Is it necessary for a splash screen bitmap to use a transparent color and be drawn with transparency? A1: No. Although the splash screen bitmap in the Space Out 2 game does use a transparent color, this isn’t necessary in all splash screen bitmaps. In fact, if you’re creating a splash screen bitmap that fills the entire game screen, you probably wouldn’t want to draw it with transparency. In Space Out 2, transparency makes sense because it allows the starry background to shine through between the letters, which is a nice effect. Q2: Is it possible to create a splash screen that consists of more than one image? A2: Yes, in fact this was done quite a lot in classic arcade games that not only needed to display a title for a game, but also provide instructions in a relatively small amount of screen space. The idea is to present the images as a simplified slide show, where
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
37: } The change to the GamePaint() function involves drawing the splash screen image if the _bSplash variable is TRUE (lines 9 13). If the _bSplash variable is FALSE, the GamePaint() function draws the sprites, score, and remaining lives, which are only pertinent to a game being played (lines 15 36). It’s important to notice that the starry background and desert bitmap are both drawn regardless of the value of the _bSplash variable (lines 3 7). This results in the background and desert image being drawn even when the splash screen image is drawn, which is necessary because the splash screen doesn’t fill up the entire game screen. The last coding change in the Space Out 2 game involves the HandleKeys() function, which you know processes key presses for the game. If you recall, the Enter key is used to start a new game if the current game is over. Because the same key is used to leave the splash screen and start a game, it is necessary to change the value of the _bSplash variable when the user presses the Enter key. Listing 22.3 shows the new version of the HandleKeys() function with this change in place. Listing 22.3 The HandleKeys() Function Changes the Value of the _bSplash Variable if the Game Is Exiting the Splash Screen to Start a New Game 1: void HandleKeys() 2: { 3: if (!_bGameOver) 4: { 5: // Move the car based upon left/right key presses 6: POINT ptVelocity = _pCarSprite->GetVelocity(); 7: if (GetAsyncKeyState(VK_LEFT) < 0) 8: { 9: // Move left 10: ptVelocity.x = max(ptVelocity.x - 1, -4); 11: _pCarSprite->SetVelocity(ptVelocity); 12: } 13: else if (GetAsyncKeyState(VK_RIGHT) < 0) 14: { 15: // Move right 16: ptVelocity.x = min(ptVelocity.x + 2, 6); 17: _pCarSprite->SetVelocity(ptVelocity); 18: } 19: 20: // Fire missiles based upon spacebar presses 21: if ((++_iFireInputDelay > 6) && GetAsyncKeyState(VK_SPACE) < 0) 22: { 23: // Create a new missile sprite 24: RECT rcBounds = { 0, 0, 600, 450 }; 25: RECT rcPos = _pCarSprite->GetPosition(); 26: Sprite* pSprite = new Sprite(_pMissileBitmap, rcBounds, BA_DIE); 27: pSprite->SetPosition(rcPos.left + 15, 400); 28: pSprite->SetVelocity(0, -7); 29: _pGame->AddSprite(pSprite); 30: 31: // Play the missile (fire) sound 32: PlaySound((LPCSTR)IDW_MISSILE, _hInstance, SND_ASYNC | 33: SND_RESOURCE | SND_NOSTOP); 34: 35: // Reset the input delay 36: _iFireInputDelay = 0; 37: } 38: } 39: 40: // Start a new game based upon an Enter (Return) key press 41: if (GetAsyncKeyState(VK_RETURN) < 0) 42: if (_bSplash) 43: { 44: // Start a new game without the splash screen 45: _bSplash = FALSE; 46: NewGame(); 47: } 48: else if (_bGameOver) 49: { 50: // Start a new game
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services