ufo.cpp
来自「《24小时教会你游戏编程》上的源码」· C++ 代码 · 共 154 行
CPP
154 行
//-----------------------------------------------------------------
// UFO 2 Application
// C++ Source - UFO.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "UFO.h"
//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
// Create the game engine
_pGame = new GameEngine(hInstance, TEXT("UFO 2"),
TEXT("UFO 2"), IDI_UFO, IDI_UFO_SM, 500, 400);
if (_pGame == NULL)
return FALSE;
// Set the frame rate
_pGame->SetFrameRate(30);
// Initialize the joystick
_pGame->InitJoystick();
// Store the instance handle
_hInstance = hInstance;
return TRUE;
}
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());
// Create and load the background and saucer bitmaps
HDC hDC = GetDC(hWindow);
_pBackground = new Bitmap(hDC, IDB_BACKGROUND, _hInstance);
_pSaucer[0] = new Bitmap(hDC, IDB_SAUCER, _hInstance);
_pSaucer[1] = new Bitmap(hDC, IDB_SAUCERFLAME, _hInstance);
// Set the initial saucer position and speed
_iSaucerX = 250 - (_pSaucer[0]->GetWidth() / 2);
_iSaucerY = 200 - (_pSaucer[0]->GetHeight() / 2);
_iSpeedX = 0;
_iSpeedY = 0;
}
void GameEnd()
{
// Cleanup the background and saucer bitmaps
delete _pBackground;
delete _pSaucer[0];
delete _pSaucer[1];
// Cleanup the game engine
delete _pGame;
}
void GameActivate(HWND hWindow)
{
// Capture the joystick
_pGame->CaptureJoystick();
}
void GameDeactivate(HWND hWindow)
{
// Release the joystick
_pGame->ReleaseJoystick();
}
void GamePaint(HDC hDC)
{
// Draw the background and saucer bitmaps
_pBackground->Draw(hDC, 0, 0);
_pSaucer[_bSaucerFlame ? 1:0]->Draw(hDC, _iSaucerX, _iSaucerY, TRUE);
}
void GameCycle()
{
// Update the saucer position
_iSaucerX = min(500 - _pSaucer[0]->GetWidth(), max(0, _iSaucerX + _iSpeedX));
_iSaucerY = min(320, max(0, _iSaucerY + _iSpeedY));
// Force a repaint to redraw the saucer
InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
}
void HandleKeys()
{
// Change the speed of the saucer in response to arrow key presses
if (GetAsyncKeyState(VK_LEFT) < 0)
_iSpeedX = max(-_iMAXSPEED, --_iSpeedX);
else if (GetAsyncKeyState(VK_RIGHT) < 0)
_iSpeedX = min(_iMAXSPEED, ++_iSpeedX);
if (GetAsyncKeyState(VK_UP) < 0)
_iSpeedY = max(-_iMAXSPEED, --_iSpeedY);
else if (GetAsyncKeyState(VK_DOWN) < 0)
_iSpeedY = min(_iMAXSPEED, ++_iSpeedY);
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{
if (bLeft)
{
// Set the saucer position to the mouse position
_iSaucerX = x - (_pSaucer[0]->GetWidth() / 2);
_iSaucerY = y - (_pSaucer[0]->GetHeight() / 2);
}
else
{
// Stop the saucer
_iSpeedX = 0;
_iSpeedY = 0;
}
}
void MouseButtonUp(int x, int y, BOOL bLeft)
{
}
void MouseMove(int x, int y)
{
}
void HandleJoystick(JOYSTATE jsJoystickState)
{
// Check horizontal movement
if (jsJoystickState & JOY_LEFT)
_iSpeedX = max(-_iMAXSPEED, _iSpeedX - 2);
else if (jsJoystickState & JOY_RIGHT)
_iSpeedX = min(_iMAXSPEED, _iSpeedX + 2);
// Check vertical movement
if (jsJoystickState & JOY_UP)
_iSpeedY = max(-_iMAXSPEED, _iSpeedY - 2);
else if (jsJoystickState & JOY_DOWN)
_iSpeedY = min(_iMAXSPEED, _iSpeedY + 2);
// Check primary joystick button
_bSaucerFlame = (jsJoystickState & JOY_FIRE1);
// Check secondary joystick button
if (jsJoystickState & JOY_FIRE2)
{
// Force the flying saucer into hyperspace
_iSaucerX = rand() % (500 - _pSaucer[0]->GetWidth());
_iSaucerY = rand() % 320;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?