Building the UFO Program Example In order to
saucer, as you soon find out. Next on the game function agenda is the GameStart() function, which creates and loads the flying saucer bitmaps, as well as sets the initial flying saucer position and speed (Listing 6.2). Listing 6.2 The GameStart() Function Performs Startup Tasks for the UFO Program Example 1: void GameStart(HWND hWindow) 2: { 3: // Create and load the background and saucer bitmaps 4: HDC hDC = GetDC(hWindow); 5: _pBackground = new Bitmap(hDC, IDB_BACKGROUND, _hInstance); 6: _pSaucer = new Bitmap(hDC, IDB_SAUCER, _hInstance); 7: 8: // Set the initial saucer position and speed 9: _iSaucerX = 250 - (_pSaucer->GetWidth() / 2); 10: _iSaucerY = 200 - (_pSaucer->GetHeight() / 2); 11: _iSpeedX = 0; 12: _iSpeedY = 0; 13: } The GameStart() function is used to initialize data pertaining to the program such as the bitmaps and other global variables. The flying saucer position is initially set to the middle of the game screen (lines 9 and 10), and then the speed of the saucer is set to 0 so that it isn’t moving (lines 11 and 12). You might think that a program with an animated flying saucer cruising over a background image would require a complex GamePaint() function. However, Listing 6.3 shows how this simply isn’t the case. Listing 6.3 The GamePaint() Function Draws the Background and Flying Saucer Bitmaps 1: void GamePaint(HDC hDC) 2: { 3: // Draw the background and saucer bitmaps 4: _pBackground->Draw(hDC, 0, 0); 5: _pSaucer->Draw(hDC, _iSaucerX, _iSaucerY, TRUE); 6: } As the code reveals, the GamePaint() function for the UFO program is painfully simple. Aside from the standard code that you’re now getting accustomed to seeing in the GamePaint() function, all the function does is draw the background and flying saucer bitmaps. The background bitmap is drawn at the origin (0, 0) of the game screen (line 4), whereas the flying saucer is drawn at its current position (line 5). Notice that TRUE is passed as the last argument to the Draw() method when drawing the flying saucer, which indicates that the saucer is to be drawn with transparency using the default transparent color. The GameCycle() function is a little more interesting than the others you’ve seen because it is actually responsible for updating the position of the flying saucer based on its speed. Listing 6.4 shows how this is accomplished in the code for the GameCycle() function. Listing 6.4 The GameCycle() Function Updates the Saucer Position and Then Repaints the Game Screen 1: void GameCycle() 2: { 3: // Update the saucer position 4: _iSaucerX = min(500 - _pSaucer->GetWidth(), max(0, _iSaucerX + _iSpeedX)); 5: _iSaucerY = min(320, max(0, _iSaucerY + _iSpeedY)); 6: 7: // Force a repaint to redraw the saucer 8: InvalidateRect(_pGame->GetWindow(), NULL, FALSE); 9: } The GameCycle() function updates the position of the flying saucer by adding its speed to its position. If the speed is negative, the saucer will move to the left and/or up, whereas positive speed values move the saucer right and/or down. The seemingly tricky code for setting the position must also take into account the boundaries of the game screen so that the flying saucer can’t be flown off into oblivion. Granted, the concept of flying off the screen might sound interesting, but it turns out being quite confusing! Another option would be to wrap the saucer around to the other side of the screen if it goes over the boundary, which is how games like Asteroids solved this problem, but I opted for the simpler solution of just stopping it at the edges. After updating the position of the flying saucer, the GameCycle() function forces a repaint of the game screen to reflect the
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services