游戏规则为玩家控制小鸡避开四辆上下行驶的汽车,自左向右穿过高速公路。游戏支持键盘和游戏杆输入,鼠标左键/游戏杆FIRE1 重新开始。 游戏基于目前的游戏引擎。
程序清单:
//----------------------------------------------------------------- // myHenway Application // C++ Header - myHenway.h //----------------------------------------------------------------- #pragma once //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include #include "Resource.h" #include "GameEngine.h" #include "Bitmap.h" #include "Sprite.h" //----------------------------------------------------------------- // Global Variables //----------------------------------------------------------------- HINSTANCE g_hInstance; GameEngine* g_pGame; HDC g_hOffscreenDC; HBITMAP g_hOffscreenBitmap; Bitmap* g_pHighwayBitmap; //背景 Bitmap* g_pChickenBitmap; //小鸡 Bitmap* g_pCarBitmaps[4]; //车 Bitmap* g_pChickenHeadBitmap; //鸡头 Sprite* g_pChickenSprite; //小鸡子画面 int g_iInputDelay1; //键盘输入延迟 int g_iInputDelay2; //游戏杆输入延迟 int g_iNumLives; //生命条数 int g_iScore; //分数 BOOL g_bGameOver; //----------------------------------------------------------------- // Function Declarations //----------------------------------------------------------------- void MoveChicken(int iXDistance, int iYDistance);
//----------------------------------------------------------------- // myHenway Application // C++ Source - myHenway.cpp //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "Henway.h" //----------------------------------------------------------------- // Game Engine Functions //----------------------------------------------------------------- BOOL GameInitialize(HINSTANCE hInstance) { // Create the game engine g_pGame = new GameEngine(hInstance, TEXT("Henway"), TEXT("Henway"), IDI_HENWAY, IDI_HENWAY_SM, 465, 400); if (g_pGame == NULL) return FALSE; // Set the frame rate g_pGame->SetFrameRate(30); // Initialize the joystick g_pGame->InitJoystick(); // Store the instance handle g_hInstance = hInstance; return TRUE; } void GameStart(HWND hWindow) { // Seed the random number generator srand(GetTickCount()); // Create the offscreen device context and bitmap g_hOffscreenDC = CreateCompatibleDC(GetDC(hWindow)); g_hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow), g_pGame->GetWidth(), g_pGame->GetHeight()); SelectObject(g_hOffscreenDC, g_hOffscreenBitmap); // Create and load the bitmaps HDC hDC = GetDC(hWindow); g_pHighwayBitmap = new Bitmap(hDC, IDB_HIGHWAY, g_hInstance); g_pChickenBitmap = new Bitmap(hDC, IDB_CHICKEN, g_hInstance); g_pCarBitmaps[0] = new Bitmap(hDC, IDB_CAR1, g_hInstance); g_pCarBitmaps[1] = new Bitmap(hDC, IDB_CAR2, g_hInstance); g_pCarBitmaps[2] = new Bitmap(hDC, IDB_CAR3, g_hInstance); g_pCarBitmaps[3] = new Bitmap(hDC, IDB_CAR4, g_hInstance); g_pChickenHeadBitmap = new Bitmap(hDC, IDB_CHICKENHEAD, g_hInstance); // Create the chicken and car sprites Sprite* pSprite; RECT rcBounds = { 0, 0, 465, 400 }; g_pChickenSprite = new Sprite(g_pChickenBitmap, rcBounds, BA_STOP); g_pChickenSprite->SetPosition(4, 175); g_pChickenSprite->SetVelocity(0, 0); g_pChickenSprite->SetZOrder(1); g_pGame->AddSprite(g_pChickenSprite); pSprite = new Sprite(g_pCarBitmaps[0], rcBounds, BA_WRAP); pSprite->SetPosition(70, 0); pSprite->SetVelocity(0, 7); pSprite->SetZOrder(2); g_pGame->AddSprite(pSprite); pSprite = new Sprite(g_pCarBitmaps[1], rcBounds, BA_WRAP); pSprite->SetPosition(160, 0); pSprite->SetVelocity(0, 3); pSprite->SetZOrder(2); g_pGame->AddSprite(pSprite); pSprite = new Sprite(g_pCarBitmaps[2], rcBounds, BA_WRAP); pSprite->SetPosition(239, 400); pSprite->SetVelocity(0, -5); pSprite->SetZOrder(2); g_pGame->AddSprite(pSprite); pSprite = new Sprite(g_pCarBitmaps[3], rcBounds, BA_WRAP); pSprite->SetPosition(329, 400); pSprite->SetVelocity(0, -10); pSprite->SetZOrder(2); g_pGame->AddSprite(pSprite); // Initialize the remaining global variables g_iInputDelay1 = 0; g_iInputDelay2 = 0; g_iNumLives = 3; g_iScore = 0; g_bGameOver = FALSE; } void GameEnd() { // Cleanup the offscreen device context and bitmap DeleteObject(g_hOffscreenBitmap); DeleteDC(g_hOffscreenDC); // Cleanup the bitmaps delete g_pHighwayBitmap; delete g_pChickenBitmap; for (int i = 0; i < 4; i++) delete g_pCarBitmaps[i]; delete g_pChickenHeadBitmap; // Cleanup the sprites g_pGame->CleanupSprites(); // Cleanup the game engine delete g_pGame; } void GameActivate(HWND hWindow) { // Capture the joystick g_pGame->CaptureJoystick(); } void GameDeactivate(HWND hWindow) { // Release the joystick g_pGame->ReleaseJoystick(); } void GamePaint(HDC hDC) { //绘制公路背景 g_pHighwayBitmap->Draw(hDC,0,0); //绘制子画面 g_pGame->DrawSprites(hDC); //绘制剩余小鸡生命数量 for(int i=0;i Draw(hDC,406+(g_pChickenHeadBitmap->GetWidth()*i),382,TRUE); } void GameCycle() { if(!g_bGameOver) { //更新子画面 g_pGame->UpdateSprites(); //获得用来绘制游戏的设备环境 HWND hWindow=g_pGame->GetWindow(); HDC hDC = GetDC(hWindow); //在屏幕外设备环境上绘制游戏 GamePaint(g_hOffscreenDC); //将屏幕外位图传送到游戏屏幕 BitBlt(hDC,0,0,g_pGame->GetWidth(),g_pGame->GetHeight(), g_hOffscreenDC,0,0,SRCCOPY); //清理 ReleaseDC(hWindow,hDC); } } void HandleKeys() { if (!g_bGameOver && (++g_iInputDelay1 > 1)) { // Move the chicken based upon key presses if (GetAsyncKeyState(VK_LEFT) < 0) MoveChicken(-20, 0); else if (GetAsyncKeyState(VK_RIGHT) < 0) MoveChicken(20, 0); if (GetAsyncKeyState(VK_UP) < 0) MoveChicken(0, -20); else if (GetAsyncKeyState(VK_DOWN) < 0) MoveChicken(0, 20); // Reset the input delay g_iInputDelay1 = 0; } } void MouseButtonDown(int x, int y, BOOL bLeft) { // Start a new game, if necessary if (g_bGameOver) { g_iNumLives = 3; g_iScore = 0; g_bGameOver = FALSE; } } void MouseButtonUp(int x, int y, BOOL bLeft) { } void MouseMove(int x, int y) { } void HandleJoystick(JOYSTATE jsJoystickState) { if (!g_bGameOver && (++g_iInputDelay2 > 1)) { // Check horizontal movement if (jsJoystickState & JOY_LEFT) MoveChicken(-20, 0); else if (jsJoystickState & JOY_RIGHT) MoveChicken(20, 0); // Check vertical movement if (jsJoystickState & JOY_UP) MoveChicken(0, -20); else if (jsJoystickState & JOY_DOWN) MoveChicken(0, 20); // Reset the input delay g_iInputDelay2 = 0; } // Check the joystick button and start a new game, if necessary if (g_bGameOver && (jsJoystickState & JOY_FIRE1)) { g_iNumLives = 3; g_iScore = 0; g_bGameOver = FALSE; } } BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee) { // See if the chicken was hit if (pSpriteHittee == g_pChickenSprite) { // Move the chicken back to the start g_pChickenSprite->SetPosition(4, 175); // See if the game is over if (--g_iNumLives > 0) MessageBox(g_pGame->GetWindow(), TEXT("Ouch!"), TEXT("Henway"), MB_OK); else { // Display game over message TCHAR szText[64]; wsprintf(szText, "Game Over! You scored %d points.", g_iScore); MessageBox(g_pGame->GetWindow(), szText, TEXT("Henway"), MB_OK); g_bGameOver = TRUE; } return FALSE; } return TRUE; } //----------------------------------------------------------------- // Functions //----------------------------------------------------------------- void MoveChicken(int iXDistance, int iYDistance) { // Move the chicken to its new position g_pChickenSprite->OffsetPosition(iXDistance, iYDistance); // See if the chicken made it across if (g_pChickenSprite->GetPosition().left > 400) { // Move the chicken back to the start and add to the score g_pChickenSprite->SetPosition(4, 175); g_iScore += 150; MessageBox(g_pGame->GetWindow(), TEXT("You made it!"), TEXT("Henway"), MB_OK); } }
//----------------------------------------------------------------- // Henway Resource Identifiers // C++ Header - Resource.h //----------------------------------------------------------------- //----------------------------------------------------------------- // Icons Range : 1000 - 1999 //----------------------------------------------------------------- #define IDI_HENWAY 1000 #define IDI_HENWAY_SM 1001 //----------------------------------------------------------------- // Bitmaps Range : 2000 - 2999 //----------------------------------------------------------------- #define IDB_HIGHWAY 2000 #define IDB_CHICKEN 2001 #define IDB_CAR1 2002 #define IDB_CAR2 2003 #define IDB_CAR3 2004 #define IDB_CAR4 2005 #define IDB_CHICKENHEAD 2006
//----------------------------------------------------------------- // Henway Resources // RC Source - Henway.rc //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "Resource.h" //----------------------------------------------------------------- // Icons //----------------------------------------------------------------- IDI_HENWAY ICON "Res//Henway.ico" IDI_HENWAY_SM ICON "Res//Henway_sm.ico" //----------------------------------------------------------------- // Bitmaps //----------------------------------------------------------------- IDB_HIGHWAY BITMAP "Res//Highway.bmp" IDB_CHICKEN BITMAP "Res//Chicken.bmp" IDB_CAR1 BITMAP "Res//Car1.bmp" IDB_CAR2 BITMAP "Res//Car2.bmp" IDB_CAR3 BITMAP "Res//Car3.bmp" IDB_CAR4 BITMAP "Res//Car4.bmp" IDB_CHICKENHEAD BITMAP "Res//ChickenHead.bmp"
游戏截图: