📄 lab11.cpp
字号:
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Lab11.h"
//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
// Task 1
// Create the game engine and a window of size 465 * 400
g_pGame = new GameEngine(g_hInstance, TEXT("Henway"), TEXT("Henway"),
IDI_HENWAY, IDI_HENWAY_SM, 465, 400);
// Then set the Frame Rate to 30 frames per second
g_pGame ->SetFrameRate(30);
// Store the instance handle
g_hInstance = hInstance;
return TRUE;
}
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());
// Task 2 -- Double Buffering
// (2a) Create the g_hOffscreenDC device context, g_hOffscreenBitmap and Select the
// g_hOffscreenBitmap to the g_hOffscreenDC device context
g_hOffscreenDC = CreateCompatibleDC(GetDC(hWindow));
g_hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow), g_pGame->GetWidth(), g_pGame->GetHeight());
SelectObject(g_hOffscreenDC, g_hOffscreenBitmap);
// (2b) Then create and load the 7 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[1] = new Bitmap(hDC, IDB_CAR1, g_hInstance);
g_pCarBitmaps[2] = new Bitmap(hDC, IDB_CAR2, g_hInstance);
g_pCarBitmaps[3] = new Bitmap(hDC, IDB_CAR3, g_hInstance);
g_pCarBitmaps[4] = new Bitmap(hDC, IDB_CAR4, g_hInstance);
g_pChickenHeadBitmap = new Bitmap(hDC, IDB_CHICKENHEAD, g_hInstance);
Sprite* pSprite;
RECT rcBounds = { 0, 0, 465, 400 };
// (2c) Create the chicken sprite
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_iInputDelay = 0;
g_iNumLives = 3;
g_iScore = 0;
g_bGameOver = FALSE;
// Play the background music
g_pGame->PlayMIDISong(TEXT("Res/Music.mid"));
}
void GameEnd()
{
// Close the MIDI player for the background music
g_pGame->CloseMIDIPlayer();
// Task 3
// 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)
{
// Resume the background music
g_pGame->PlayMIDISong(TEXT("Res/Music.mid"), FALSE);
}
void GameDeactivate(HWND hWindow)
{
// Pause the background music
g_pGame->PauseMIDISong();
}
void GamePaint(HDC hDC)
{
// Task 4
// (4a) Draw the background highway at position (0,0)
g_pHighwayBitmap->Draw(hDC, 0,0);
// (4b) Draw the sprites
g_pGame->DrawSprites(hDC);
// (4c) Draw the number of remaining chicken lives from position (406, 382)
// and set the transpancy to TRUE
for(int i = 0;i < g_iNumLives; i++)
g_pChickenHeadBitmap->Draw(hDC, 406+(g_pChickenHeadBitmap-> GetWidth()* i), 382, TRUE);
}
void GameCycle()
{
if (!g_bGameOver)
{
// Play a random car sound randomly
if (rand() % 100 == 0)
if (rand() % 2 == 0)
PlaySound((LPCSTR)IDR_CARHORN1, g_hInstance, SND_ASYNC | SND_RESOURCE);
else
PlaySound((LPCSTR)IDR_CARHORN2, g_hInstance, SND_ASYNC | SND_RESOURCE);
// Update the sprites
g_pGame->UpdateSprites();
// Obtain a device context for repainting the game
HWND hWindow = g_pGame->GetWindow();
HDC hDC = GetDC(hWindow);
// Task 5 --Double Buffering Part II
// Call GamePaint() function and draw the images and sprites to the g_hOffscreenDC
GamePaint(g_hOffscreenDC);
// Task 6 -- Double Buffering Part III
// Blit the offscreen bitmap to the game screen
BitBlt(hDC, 0, 0, g_pGame->GetWidth(), g_pGame->GetHeight(), g_hOffscreenDC, 0, 0, SRCCOPY);
// Cleanup
ReleaseDC(hWindow, hDC);
}
}
void HandleKeys()
{
if (!g_bGameOver && (++g_iInputDelay > 2))
{
// Task 7
// Based on The left key code, complete "RIGHT", "UP", "DOWN"
// Move the chicken based upon key presses
if (GetAsyncKeyState(VK_LEFT) < 0)
MoveChicken(-20, 0);
// Reset the input delay
g_iInputDelay = 0;
}
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{
// Start a new game, if necessary
if (g_bGameOver)
{
// Restart the background music
g_pGame->PlayMIDISong();
// Initialize the game variables
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)
{
}
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)
// Play a sound for the chicken getting hit
PlaySound((LPCSTR)IDR_SQUISH, g_hInstance, SND_ASYNC | SND_RESOURCE);
else
{
// Play a sound for the game ending
PlaySound((LPCSTR)IDR_GAMEOVER, g_hInstance, SND_ASYNC | SND_RESOURCE);
// 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;
// Pause the background music
g_pGame->PauseMIDISong();
}
return FALSE;
}
return TRUE;
}
//-----------------------------------------------------------------
// Functions
//-----------------------------------------------------------------
void MoveChicken(int iXDistance, int iYDistance)
{
// Task 8 -- Complete the MoveChicken function to set the chicken's new position
// Play the "bok" chicken sound
PlaySound((LPCSTR)IDR_BOK, g_hInstance, SND_ASYNC | SND_NOSTOP | SND_RESOURCE);
// Move the chicken to its new position by using the sprite's OffsetPosition
g_pChickenSprite->OffsetPosition(iXDistance, iYDistance);
// See if the chicken made it across, if the Chicken's sprite left position is bigger
// than 400
if(g_pChickenSprite->GetPosition().left > 400){
// Play a sound for the chicken making it safely across
PlaySound((LPCSTR)IDR_CELEBRATE, g_hInstance, SND_ASYNC | SND_NOSTOP | SND_RESOURCE);
// Move the chicken back to the start (4, 175) and add 150 to the score
g_pChickenSprite->SetPosition(4, 175);
g_iScore +=150;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -