对引擎作以下修改:
1.添加键盘支持
2.添加鼠标支持
3.修改Bitmap类,使其支持位图透明
4.添加错误退出代码
修改代码如下:
GameEngine.h
函数声明 //键盘函数 void HandleKeys(); //鼠标函数 void MouseButtonDown(int x, int y, BOOL bLeft); void MouseButtonUp(int x, int y, BOOL bLeft); void MouseMove(int x, int y); //退出 void ErrorQuit(LPTSTR szErrorMsg);
GameEngine.cpp
if (iTickCount > iTickTrigger) { iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay(); HandleKeys(); //增加了这个函数 GameCycle(); } ... ... case WM_LBUTTONDOWN: // Handle left mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_LBUTTONUP: // Handle left mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_RBUTTONDOWN: // Handle right mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_RBUTTONUP: // Handle right mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_MOUSEMOVE: // Handle mouse movement MouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; ... ...
void GameEngine::ErrorQuit(LPTSTR szErrorMsg) { MessageBox(GetWindow(), szErrorMsg, TEXT("Critical Error"), MB_OK | MB_ICONERROR); PostQuitMessage(0); }
修改Bitmap.h
void Draw(HDC hDC, int x, int y, BOOL bTrans = FALSE, COLORREF crTransColor = RGB(255, 0, 255)); //初始默认值为不透明,透明色为紫色
修改Bitmap.cpp的Draw()方法
void Bitmap::Draw(HDC hDC, int x, int y, BOOL bTrans, COLORREF crTransColor) { ... if (bTrans) TransparentBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, GetWidth(), GetHeight(), crTransColor); else BitBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY); }
其中TransparentBlt()方法需要向项目添加msimg32.lib库
基于以上修改,一个UFO实例如下:
代码清单:
//----------------------------------------------------------------- // UFO Application // C++ Header - UFO.h //----------------------------------------------------------------- #pragma once //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include #include "Resource.h" #include "GameEngine.h" #include "Bitmap.h" //----------------------------------------------------------------- // Global Variables //----------------------------------------------------------------- HINSTANCE g_hInstance; GameEngine* g_pGame; const int g_iMAXSPEED = 8; //最大速度 Bitmap* g_pBackground; //背景位图 Bitmap* g_pSaucer; //UFO位图 int g_iSaucerX, g_iSaucerY; //UFO的X/Y坐标 int g_iSpeedX, g_iSpeedY; //UFO的X/Y方向的速度
//----------------------------------------------------------------- // UFO Application // C++ Source - UFO.cpp //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "UFO.h" //----------------------------------------------------------------- // Game Engine Functions //----------------------------------------------------------------- BOOL GameInitialize(HINSTANCE hInstance) { // Create the game engine g_pGame = new GameEngine(hInstance, TEXT("UFO"), TEXT("UFO"), IDI_UFO, IDI_UFO_SM, 500, 400); if (g_pGame == NULL) return FALSE; // Set the frame rate g_pGame->SetFrameRate(30); // Store the instance handle g_hInstance = hInstance; return TRUE; } void GameStart(HWND hWindow) { // Create and load the background and saucer bitmaps HDC hDC = GetDC(hWindow); g_pBackground = new Bitmap(hDC, IDB_BACKGROUND, g_hInstance); g_pSaucer = new Bitmap(hDC, IDB_SAUCER, g_hInstance); // Set the initial saucer position and speed g_iSaucerX = 250 - (g_pSaucer->GetWidth() / 2); g_iSaucerY = 200 - (g_pSaucer->GetHeight() / 2); g_iSpeedX = 0; g_iSpeedY = 0; } void GameEnd() { // Cleanup the background and saucer bitmaps delete g_pBackground; delete g_pSaucer; // Cleanup the game engine delete g_pGame; } void GameActivate(HWND hWindow) { } void GameDeactivate(HWND hWindow) { } void GamePaint(HDC hDC) { // Draw the background and saucer bitmaps g_pBackground->Draw(hDC, 0, 0); g_pSaucer->Draw(hDC, g_iSaucerX, g_iSaucerY, TRUE); } void GameCycle() { // 更新飞碟位置 g_iSaucerX = min(500 - g_pSaucer->GetWidth(), max(0, g_iSaucerX + g_iSpeedX)); g_iSaucerY = min(320, max(0, g_iSaucerY + g_iSpeedY)); // Force a repaint to redraw the saucer InvalidateRect(g_pGame->GetWindow(), NULL, FALSE); } void HandleKeys() { // Change the speed of the saucer in response to arrow key presses if (GetAsyncKeyState(VK_LEFT) < 0) g_iSpeedX = max(-g_iMAXSPEED, --g_iSpeedX); else if (GetAsyncKeyState(VK_RIGHT) < 0) g_iSpeedX = min(g_iMAXSPEED, ++g_iSpeedX); if (GetAsyncKeyState(VK_UP) < 0) g_iSpeedY = max(-g_iMAXSPEED, --g_iSpeedY); else if (GetAsyncKeyState(VK_DOWN) < 0) g_iSpeedY = min(g_iMAXSPEED, ++g_iSpeedY); } void MouseButtonDown(int x, int y, BOOL bLeft) { if (bLeft) { //鼠标左键事件 // Set the saucer position to the mouse position g_iSaucerX = x - (g_pSaucer->GetWidth() / 2); g_iSaucerY = y - (g_pSaucer->GetHeight() / 2); } else { // Stop the saucer g_iSpeedX = 0; g_iSpeedY = 0; } } void MouseButtonUp(int x, int y, BOOL bLeft) { } void MouseMove(int x, int y) { }
//----------------------------------------------------------------- // UFO Resource Identifiers // C++ Header - Resource.h //----------------------------------------------------------------- //----------------------------------------------------------------- // Icons Range : 1000 - 1999 //----------------------------------------------------------------- #define IDI_UFO 1000 #define IDI_UFO_SM 1001 //----------------------------------------------------------------- // Bitmaps Range : 2000 - 2999 //----------------------------------------------------------------- #define IDB_BACKGROUND 2000 #define IDB_SAUCER 2001
//----------------------------------------------------------------- // UFO Resources // RC Source - UFO.rc //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "Resource.h" //----------------------------------------------------------------- // Icons //----------------------------------------------------------------- IDI_UFO ICON "Res//UFO.ico" IDI_UFO_SM ICON "Res//UFO_sm.ico" //----------------------------------------------------------------- // Bitmaps //----------------------------------------------------------------- IDB_BACKGROUND BITMAP "Res//Background.bmp" IDB_SAUCER BITMAP "Res//Saucer.bmp"