The _pSaucerBitmap is a bitmap for the flying
flying saucer sprite is updated properly. This change involves the addition of a call to the UpdateSaucer() function, which is responsible for updating the velocity of the flying saucer sprite to help it dodge the asteroids. Speaking of the UpdateSaucer() function, its code is shown in Listing 20.2. Listing 20.2 The UpdateSaucer() Function Updates the Flying Saucer’s Velocity to Help It Dodge Asteroids 1: void UpdateSaucer() 2: { 3: // Obtain the saucer’s position 4: RECT rcSaucer, rcRoid; 5: rcSaucer = _pSaucer->GetPosition(); 6: 7: // Find out which asteroid is closest to the saucer 8: int iXCollision = 500, iYCollision = 400, iXYCollision = 900; 9: for (int i = 0; i < 3; i++) 10: { 11: // Get the asteroid position 12: rcRoid = _pAsteroids[i]->GetPosition(); 13: 14: // Calculate the minimum XY collision distance 15: int iXCollisionDist = (rcSaucer.left + 16: (rcSaucer.right - rcSaucer.left) / 2) 17: (rcRoid.left + 18: (rcRoid.right - rcRoid.left) / 2); 19: int iYCollisionDist = (rcSaucer.top + 20: (rcSaucer.bottom - rcSaucer.top) / 2) 21: (rcRoid.top + 22: (rcRoid.bottom - rcRoid.top) / 2); 23: if ((abs(iXCollisionDist) < abs(iXCollision)) || 24: (abs(iYCollisionDist) < abs(iYCollision))) 25: if ((abs(iXCollisionDist) + abs(iYCollisionDist)) < iXYCollision) 26: { 27: iXYCollision = abs(iXCollision) + abs(iYCollision); 28: iXCollision = iXCollisionDist; 29: iYCollision = iYCollisionDist; 30: } 31: } 32: 33: // Move to dodge the asteroids, if necessary 34: POINT ptVelocity; 35: ptVelocity = _pSaucer->GetVelocity(); 36: if (abs(iXCollision) < 60) 37: { 38: // Adjust the X velocity 39: if (iXCollision < 0) 40: ptVelocity.x = max(ptVelocity.x - 1, -8); 41: else 42: ptVelocity.x = min(ptVelocity.x + 1, 8); 43: } 44: if (abs(iYCollision) < 60) 45: { 46: // Adjust the Y velocity 47: if (iYCollision < 0) 48: ptVelocity.y = max(ptVelocity.y - 1, -8); 49: else 50: ptVelocity.y = min(ptVelocity.y + 1, 8); 51: } 52: 53: // Update the saucer to the new position 54: _pSaucer->SetVelocity(ptVelocity); 55: } I realize that this function contains a lot of code, but if you take it a section at a time, it’s really not too complex. First of all, let’s understand how the UpdateSaucer() function is helping the flying saucer to dodge the asteroids. The idea is to check for the closest asteroid in relation to the saucer, and then alter the saucer’s velocity so that it has a tendency to move in the opposite direction of the asteroid. I say “tendency” because
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services