The _pSaucerBitmap is a bitmap for the flying
The _pSaucerBitmap is a bitmap for the flying saucer image. The _pAsteroids and _pSaucer variables both store sprite pointers. These pointers are necessary so that you can compare the positions of the saucer and asteroids and alter the saucer’s velocity; this is how you add “intelligence” to the flying saucer. The Roids 2 program also includes a new helper function named UpdateSaucer(), which is responsible for updating the saucer sprite. Of course, the saucer sprite is already being updated in terms of its position and velocity in the game engine. However, in this case an additional update is taking place that alters the saucer’s velocity based on its proximity to nearby asteroids. You learn exactly how this facet of the program works a little later in the hour. The GameStart() function is similar to the previous version, except that it now contains code to initialize the flying saucer. Listing 20.1 shows the code for this function. Listing 20.1 The GameStart() Function Initializes the Flying Saucer Bitmap and Sprite 1: void GameStart(HWND hWindow) 2: { 3: // Seed the random number generator 4: srand(GetTickCount()); 5: 6: // Create the offscreen device context and bitmap 7: _hOffscreenDC = CreateCompatibleDC(GetDC(hWindow)); 8: _hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow), 9: _pGame->GetWidth(), _pGame->GetHeight()); 10: SelectObject(_hOffscreenDC, _hOffscreenBitmap); 11: 12: // Create and load the asteroid and saucer bitmaps 13: HDC hDC = GetDC(hWindow); 14: _pAsteroidBitmap = new Bitmap(hDC, IDB_ASTEROID, _hInstance); 15: _pSaucerBitmap = new Bitmap(hDC, IDB_SAUCER, _hInstance); 16: 17: // Create the starry background 18: _pBackground = new StarryBackground(500, 400); 19: 20: // Create the asteroid sprites 21: RECT rcBounds = { 0, 0, 500, 400 }; 22: _pAsteroids[0] = new Sprite(_pAsteroidBitmap, rcBounds, BA_WRAP); 23: _pAsteroids[0]->SetNumFrames(14); 24: _pAsteroids[0]->SetFrameDelay(1); 25: _pAsteroids[0]->SetPosition(250, 200); 26: _pAsteroids[0]->SetVelocity(-3, 1); 27: _pGame->AddSprite(_pAsteroids[0]); 28: _pAsteroids[1] = new Sprite(_pAsteroidBitmap, rcBounds, BA_WRAP); 29: _pAsteroids[1]->SetNumFrames(14); 30: _pAsteroids[1]->SetFrameDelay(2); 31: _pAsteroids[1]->SetPosition(250, 200); 32: _pAsteroids[1]->SetVelocity(3, -2); 33: _pGame->AddSprite(_pAsteroids[1]); 34: _pAsteroids[2] = new Sprite(_pAsteroidBitmap, rcBounds, BA_WRAP); 35: _pAsteroids[2]->SetNumFrames(14); 36: _pAsteroids[2]->SetFrameDelay(3); 37: _pAsteroids[2]->SetPosition(250, 200); 38: _pAsteroids[2]->SetVelocity(-2, -4); 39: _pGame->AddSprite(_pAsteroids[2]); 40: 41: // Create the saucer sprite 42: _pSaucer = new Sprite(_pSaucerBitmap, rcBounds, BA_WRAP); 43: _pSaucer->SetPosition(0, 0); 44: _pSaucer->SetVelocity(3, 1); 45: _pGame->AddSprite(_pSaucer); 46: } The changes to the GameStart() function primarily involve the addition of the flying saucer sprite. The saucer bitmap is first loaded (line 15), and then the saucer sprite is created and added to the game engine (lines 42 45). It’s also worth pointing out that the asteroid sprite pointers are now being stored in the _pAsteroids array (lines 22, 28, and 34) because you need to reference them later when helping the saucer avoid hitting the asteroids. The GameCycle() function in Roids 2 requires a slight modification to ensure that 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