new saucer position (line 8 ). The flying
new saucer position (line 8 ). The flying saucer is now being drawn and updated properly, but you still don’t have a way to change its speed so that it can fly. This is accomplished first by handling keyboard input in the HandleKeys() function, which is shown in Listing 6.5. Listing 6.5 The HandleKeys() Function Checks the Status of the Arrow Keys, Which Are Used to Control the Flying Saucer 1: void HandleKeys() 2: { 3: // Change the speed of the saucer in response to arrow key presses 4: if (GetAsyncKeyState(VK_LEFT) < 0) 5: _iSpeedX = max(-_iMAXSPEED, --_iSpeedX); 6: else if (GetAsyncKeyState(VK_RIGHT) < 0) 7: _iSpeedX = min(_iMAXSPEED, ++_iSpeedX); 8: if (GetAsyncKeyState(VK_UP) < 0) 9: _iSpeedY = max(-_iMAXSPEED, --_iSpeedY); 10: else if (GetAsyncKeyState(VK_DOWN) < 0) 11: _iSpeedY = min(_iMAXSPEED, ++_iSpeedY); 12: } The HandleKeys() function uses the Win32 GetAsyncKeyState() function to check the status of the arrow keys (VK_LEFT, VK_RIGHT, VK_UP, and VK_DOWN) and see if any of them are being pressed. If so, the speed of the flying saucer is adjusted appropriately. Notice that the newly calculated speed is always checked against the _iMAXSPEED global constant to make sure that a speed limit is enforced. Even flying saucers are required to stay within the speed limit! The GetAsyncKeyState() function is part of the Win32 API, and provides a means of obtaining the state of any key on the keyboard at any time. You specify which key you're looking for by using its virtual key code; Windows defines virtual key codes for all the keys on a standard keyboard. Common key codes for games include VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_CONTROL, VK_SHIFT, and VK_RETURN. If you thought handling keyboard input in the UFO program was easy, wait until you see how the mouse is handled. To make things a little more interesting, both mouse buttons are used in this program. The left mouse button sets the flying saucer position to the current mouse cursor position, whereas the right mouse button sets the speed of the flying saucer to 0. So, you can use the mouse to quickly get control of the flying saucer; just right-click to stop it and then left-click to position it wherever you want. Listing 6.6 shows the code for the MouseButtonDown() function, which makes this mouse magic possible. Listing 6.6 The MouseButtonDown() Function Uses the Left and Right Mouse Buttons to Move the Flying Saucer to the Current Mouse Position and Stop the Flying Saucer, Respectively 1: void MouseButtonDown(int x, int y, BOOL bLeft) 2: { 3: if (bLeft) 4: { 5: // Set the saucer position to the mouse position 6: _iSaucerX = x - (_pSaucer->GetWidth() / 2); 7: _iSaucerY = y - (_pSaucer->GetHeight() / 2); 8: } 9: else 10: { 11: // Stop the saucer 12: _iSpeedX = 0; 13: _iSpeedY = 0; 14: } 15: } The first step in this code is to check and see which one of the mouse buttons was pressed left or right (line 3). I know, most PC mice these days have three buttons, but I wanted to keep the game engine relatively simple, so I just focused on the two most important buttons. If the left mouse button was pressed, the function calculates the position of the flying saucer so that it is centered on the current mouse cursor position (lines 6 and 7). If the right button was pressed, the speed of the flying saucer is set to 0 (lines 12 and 13). If you determine that supporting two mouse buttons is simply too much of a weakness for the game engine, feel free to modify it on your own. It primarily involves changing the third
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services