📄 eatapple.cpp
字号:
//-----------------------------------------------------------------
// Stunt Jumper Application
// C++ Source - StuntJumper.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "EatApple.h"
#include "TIME.h"
#include "math.h"
//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
// Create the game engine
g_pGame = new GameEngine(hInstance, TEXT("Eat Fruit"),
TEXT("Eat Fruit"), IDI_EATFRUIT, IDI_EATFRUIT_SM, 995, 724);
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)
{
ReadHiScores();
// 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_pSplashBitmap = new Bitmap(hDC, IDB_SPLASH, g_hInstance);
g_pTargetBitmap = new Bitmap(hDC, IDB_TARGET, g_hInstance);
g_pGrapeBitmap = new Bitmap(hDC, IDB_GRAPE, g_hInstance);
g_pBananaBitmap = new Bitmap(hDC, IDB_BANANA, g_hInstance);
g_pGameOverBitmap = new Bitmap(hDC, IDB_GAMEOVER, g_hInstance);
g_pStrwberryBitmap= new Bitmap(hDC, IDB_STRWBERRY, g_hInstance);
g_pPearBitmap = new Bitmap(hDC, IDB_PEAR, g_hInstance);
g_pPlaneBitmap = new Bitmap(hDC, IDB_PLANE, g_hInstance);
g_pFrontbasketBitmap = new Bitmap(hDC, IDB_FRONTBASKET, g_hInstance);
g_pBackbasketBitmap = new Bitmap(hDC, IDB_BACKBASKET, g_hInstance);
g_pBigmouseBitmap = new Bitmap(hDC, IDB_BIGMOUSE, g_hInstance);
g_pSmallmouseBitmap = new Bitmap(hDC, IDB_SMALLMOUSE, g_hInstance);
g_pBottomBitmap=new Bitmap(hDC,IDB_BOTTOM,g_hInstance);
g_pEatingBitmap = new Bitmap(hDC, IDB_EATING, g_hInstance);
g_pPowBitmap=new Bitmap(hDC, IDB_BPOW, g_hInstance);
g_pNextBitmap=new Bitmap(hDC, IDB_BNEXT, g_hInstance);
g_pBGLayer=new BackgroundLayer(hDC,IDB_BACKGROUNDLAYER,g_hInstance,2,SD_LEFT);
g_ScrollingBackground=new ScrollingBackground(995,725);
g_ScrollingBackground->AddLayer(g_pBGLayer);
g_pBackgroundBitmap = new BackgroundLayer(hDC,IDB_BACKGROUND,g_hInstance,0,SD_LEFT);
g_ScrollingBackground->AddLayer(g_pBackgroundBitmap);
// Set the splash screen variable
g_bSplash = TRUE;
NewGame();
}
void GameEnd()
{
// Close the MIDI player for the background music
g_pGame->CloseMIDIPlayer();
// Cleanup the offscreen device context and bitmap
DeleteObject(g_hOffscreenBitmap);
DeleteDC(g_hOffscreenDC);
// Cleanup the bitmaps
delete g_pSplashBitmap;
delete g_pTargetBitmap;
delete g_pBackgroundBitmap;
delete g_pGrapeBitmap ;
delete g_pBananaBitmap;
delete g_pGameOverBitmap;
delete g_pStrwberryBitmap;
delete g_pPearBitmap ;
delete g_pPlaneBitmap ;
delete g_pFrontbasketBitmap ;
delete g_pBackbasketBitmap ;
delete g_pBigmouseBitmap;
delete g_pSmallmouseBitmap;
delete g_pBottomBitmap;
delete g_pEatingBitmap;
delete g_ScrollingBackground;
delete g_pBGLayer;
// Cleanup the sprites
g_pGame->CleanupSprites();
// Cleanup the game engine
delete g_pGame;
WriteHiScores();
}
void GameActivate(HWND hWindow)
{
if (!g_bSplash)
{
// Resume the background music
g_pGame->PlayMIDISong(TEXT(""), FALSE);
}
}
void GameDeactivate(HWND hWindow)
{
if (!g_bSplash)
{
// Pause the background music
g_pGame->PauseMIDISong();
}
}
void GamePaint(HDC hDC)
{
if (g_bSplash)
{
// Draw the splash screen image
g_pSplashBitmap->Draw(hDC, 0, 0,TRUE);
// Draw the hi scores
TCHAR szText[64];
RECT rect = { 700, 380, 720, 400};
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(142, 111, 255));
for (int i = 0; i < 5; i++)
{
wsprintf(szText, "%d", g_iHiScores[i]);
DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
rect.top += 20;
rect.bottom += 20;
}
}
else
{
// Draw the sprites
g_pBackgroundBitmap->Draw(hDC,0,0);
g_ScrollingBackground->Draw(hDC,TRUE);
g_pGame->DrawSprites(hDC);
//Draw the time
TCHAR Text[64];
RECT rects = { 880, 620, 910, 640};
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0, 255, 255));
wsprintf(Text, "%d", g_iTime);
DrawText(hDC, Text, -1, &rects, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
rects.top += 20;
rects.bottom += 20;
// Draw the score
TCHAR szText[64];
RECT rect = { 570, 640, 610, 660 };
wsprintf(szText, "%d", g_iScore);
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255,0, 0));
DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
// Draw the game over message, if necessary
if (g_bGameOver)
g_pGameOverBitmap->Draw(hDC, 140, 160, TRUE);
}
}
void GameCycle()
{
if (!g_bGameOver)
{
g_ScrollingBackground->Update();
// Randomly show and hide the targets
if(!g_bSplash)
{
if(rand()%10==0)
{
g_pPlane->SetHidden(FALSE);
if(!IsClickPlane) g_pPlane->SetVelocity(-5,0);
else
{
g_pPlane->SetVelocity(-20,0);
IsClickPlane=FALSE;
}
}
for (int i = 0; i < 4; i++)
if (g_pTargetSprite[i]->IsHidden())
{
if (rand() % g_iTargetDelay == 0)
{
// Show the target
g_pTargetSprite[i]->SetHidden(FALSE);
g_pTargetSprite[i]->setAutoUpdate(FALSE);
g_pTargetSprite[i]->SetCurrentFrame(0);
// Start the countdown delay
if (i == 0)
{
// Start the left target
g_pTargetSprite[i]->SetPosition(rand()%40, rand()%30+500);
g_pTargetSprite[i]->SetVelocity(4,-4);
}
else if (i == 1)
{
// Start the left two target
g_pTargetSprite[i]->SetPosition(rand()%100+300, rand()%30+500);
g_pTargetSprite[i]->SetVelocity(2,-6);
}
else if (i == 2)
{
// Start the left two target
g_pTargetSprite[i]->SetPosition(rand()%100+480, rand()%30+500);
//g_iGametime+=3.1415/6;(sin(g_iGametime),-4)
g_pTargetSprite[i]->SetVelocity(-1,-6);
}
else if(i==3)
{
g_pTargetSprite[i]->SetPosition(rand()%100+710, rand()%30+500);
g_pTargetSprite[i]->SetVelocity(-3,-5);
}
}
}
if(--g_iTemp==0)
{
g_iTime--;
g_iTemp=30;
}
if(g_iTime==0)
{
g_bGameOver=true;
PlaySound((LPCSTR)IDW_GAMEOVER, 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);
// Paint the game to the offscreen device context
GamePaint(g_hOffscreenDC);
// 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);
}
else
if (--g_iGameOverDelay == 0)
{
// Stop the music and switch to demo mode
g_pGame->PauseMIDISong();
g_bSplash = TRUE;
NewGame();
}
}
void HandleKeys()
{
// Start a new game based upon an Enter (Return) key press
if (GetAsyncKeyState(VK_RETURN) < 0)
if (g_bSplash)
{
// Start a new game without the splash screen
g_bSplash = FALSE;
NewGame();
}
else if (g_bGameOver)
{
// Start a new game
NewGame();
}
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{
// Only check the left mouse button
if (!g_bGameOver && bLeft)
{
// Temporarily hide the target and pow sprites
g_pMouseSprite[current]->SetHidden(TRUE);
g_pPowSprite->SetHidden(TRUE);
// See if a guy sprite was clicked
Sprite* pSprite;
if ((pSprite = g_pGame->IsPointInSprite(x, y)) != NULL)
{
// Position and show the pow sprite
if(((pSprite==g_pTargetSprite[0])||(pSprite==g_pTargetSprite[1])||
(pSprite==g_pTargetSprite[2])||(pSprite==g_pTargetSprite[3]))&&
pSprite->GetPosition().bottom>=140)
{
double curtime=clock();
for(int i=0;i<4;i++)
{
if(pSprite==g_pTargetSprite[i])
{
pSprite->SetFrameDelay(5);
pSprite->setAutoUpdate(TRUE);
g_iIsFirst[i]++;
if (g_iIsFirst[i]==ShoutNumber)
{
PlaySound((LPCSTR)IDW_SUCCESS, g_hInstance, SND_ASYNC | SND_RESOURCE);
g_pPowSprite->SetPosition(x - (g_pPowSprite->GetWidth() / 2),
y - (g_pPowSprite->GetHeight() / 2));
g_pPowSprite->SetHidden(FALSE);
g_iIsFirst[i]=0;
// Hide the guy that was clicked
pSprite->SetHidden(TRUE);
// Update the score
g_iScore += 20;
UpdateHiScores();
if(g_iHits==2&&g_iTemp2!=0)
{
current=0;
ShoutNumber=g_iTemp2;
g_iTemp2=0;
}
if(++g_iHits>=10)
{
PlaySound((LPCSTR)IDW_PASS, g_hInstance, SND_ASYNC | SND_RESOURCE);
HWND hWindow=g_pGame->GetWindow();
HDC hDC = GetDC(hWindow);
g_pNextBitmap->Draw(hDC,100,200,TRUE);
if (--g_iTargetDelay == 0)
g_iTargetDelay = 1;
if(++ShoutNumber>5)
ShoutNumber=5;
g_iHits=0;
current=0;
g_iTime=20;
Sleep(3000);
break;
}
}
if((clock()-curtime)/1000>=0.5)
{
g_iIsFirst[i]=0;
}
}
}
}
if(pSprite==g_pPlane)
{
current=1;
g_iTemp2=ShoutNumber;
ShoutNumber=1;
g_pPlane->SetVelocity(-20,0);
IsClickPlane=TRUE;
}
}
}
// Show the target sprite again
g_pMouseSprite[current]->SetHidden(FALSE);
if (g_bGameOver && !bLeft)
{
// Start a new game
g_bGameOver = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -