//-----------------------------------------------------------------
// Space Out 4 Application
// C++ Source - SpaceOut.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "SpaceOut.h"
//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
// Create the game engine
g_pGame = new GameEngine(hInstance, TEXT("Space Out 4"),
TEXT("Space Out 4"), IDI_SPACEOUT, IDI_SPACEOUT_SM, 600, 450);
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)
{
// Read the hi scores
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_pDesertBitmap = new Bitmap(hDC, IDB_DESERT, g_hInstance);
g_pCarBitmap = new Bitmap(hDC, IDB_CAR, g_hInstance);
g_pSmCarBitmap = new Bitmap(hDC, IDB_SMCAR, g_hInstance);
g_pMissileBitmap = new Bitmap(hDC, IDB_MISSILE, g_hInstance);
g_pBlobboBitmap = new Bitmap(hDC, IDB_BLOBBO, g_hInstance);
g_pBMissileBitmap = new Bitmap(hDC, IDB_BMISSILE, g_hInstance);
g_pJellyBitmap = new Bitmap(hDC, IDB_JELLY, g_hInstance);
g_pJMissileBitmap = new Bitmap(hDC, IDB_JMISSILE, g_hInstance);
g_pTimmyBitmap = new Bitmap(hDC, IDB_TIMMY, g_hInstance);
g_pTMissileBitmap = new Bitmap(hDC, IDB_TMISSILE, g_hInstance);
g_pSmExplosionBitmap = new Bitmap(hDC, IDB_SMEXPLOSION, g_hInstance);
g_pLgExplosionBitmap = new Bitmap(hDC, IDB_LGEXPLOSION, g_hInstance);
g_pGameOverBitmap = new Bitmap(hDC, IDB_GAMEOVER, g_hInstance);
// Create the starry background
g_pBackground = new StarryBackground(600, 450);
// Start the game for demo mode
g_bDemo = 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_pDesertBitmap;
delete g_pCarBitmap;
delete g_pSmCarBitmap;
delete g_pMissileBitmap;
delete g_pBlobboBitmap;
delete g_pBMissileBitmap;
delete g_pJellyBitmap;
delete g_pJMissileBitmap;
delete g_pTimmyBitmap;
delete g_pTMissileBitmap;
delete g_pSmExplosionBitmap;
delete g_pLgExplosionBitmap;
delete g_pGameOverBitmap;
// Cleanup the background
delete g_pBackground;
// Cleanup the sprites
g_pGame->CleanupSprites();
// Cleanup the game engine
delete g_pGame;
// Save the hi scores
WriteHiScores();
}
void GameActivate(HWND hWindow)
{
if (!g_bDemo)
// Resume the background music
g_pGame->PlayMIDISong(TEXT(""), FALSE);
}
void GameDeactivate(HWND hWindow)
{
if (!g_bDemo)
// Pause the background music
g_pGame->PauseMIDISong();
}
void GamePaint(HDC hDC)
{
// Draw the background
g_pBackground->Draw(hDC);
// Draw the desert bitmap
g_pDesertBitmap->Draw(hDC, 0, 371);
// Draw the sprites
g_pGame->DrawSprites(hDC);
if (g_bDemo)
{
// Draw the splash screen image
g_pSplashBitmap->Draw(hDC, 142, 20, TRUE);
// Draw the hi scores
TCHAR szText[64];
RECT rect = { 275, 250, 325, 270};
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 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 score
TCHAR szText[64];
RECT rect = { 460, 0, 510, 30 };
wsprintf(szText, "%d", g_iScore);
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 255));
DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
// Draw the number of remaining lives (cars)
for (int i = 0; i < g_iNumLives; i++)
g_pSmCarBitmap->Draw(hDC, 520 + (g_pSmCarBitmap->GetWidth() * i),
10, TRUE);
// Draw the game over message, if necessary
if (g_bGameOver)
g_pGameOverBitmap->Draw(hDC, 170, 100, TRUE);
}
}
void GameCycle()
{
if (!g_bGameOver)
{
if (!g_bDemo)
{
// Randomly add aliens
if ((rand() % g_iDifficulty) == 0)
AddAlien();
}
// Update the background
g_pBackground->Update();
// 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_bDemo = TRUE;
NewGame();
}
}
void HandleKeys()
{
if (!g_bGameOver && !g_bDemo)
{
// Move the car based upon left/right key presses
POINT ptVelocity = g_pCarSprite->GetVelocity();
if (GetAsyncKeyState(VK_LEFT) < 0)
{
// Move left
ptVelocity.x = max(ptVelocity.x - 1, -4);
g_pCarSprite->SetVelocity(ptVelocity);
}
else if (GetAsyncKeyState(VK_RIGHT) < 0)
{
// Move right
ptVelocity.x = min(ptVelocity.x + 2, 6);
g_pCarSprite->SetVelocity(ptVelocity);
}
// Fire missiles based upon spacebar presses
if ((++g_iFireInputDelay > 6) && GetAsyncKeyState(VK_SPACE) < 0)
{
// Create a new missile sprite
RECT rcBounds = { 0, 0, 600, 450 };
RECT rcPos = g_pCarSprite->GetPosition();
Sprite* pSprite = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE);
pSprite->SetPosition(rcPos.left + 15, 400);
pSprite->SetVelocity(0, -7);
g_pGame->AddSprite(pSprite);
// Play the missile (fire) sound
PlaySound((LPCSTR)IDW_MISSILE, g_hInstance, SND_ASYNC |
SND_RESOURCE | SND_NOSTOP);
// Reset the input delay
g_iFireInputDelay = 0;
}
}
// Start a new game based upon an Enter (Return) key press
if (GetAsyncKeyState(VK_RETURN) < 0)
if (g_bDemo)
{
// Switch out of demo mode to start a new game
g_bDemo = FALSE;
NewGame();
}
else if (g_bGameOver)
{
// Start a new game
NewGame();
}
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{
}
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 a player missile and an alien have collided
Bitmap* pHitter = pSpriteHitter->GetBitmap();
Bitmap* pHittee = pSpriteHittee->GetBitmap();
if ((pHitter == g_pMissileBitmap && (pHittee == g_pBlobboBitmap ||
pHittee == g_pJellyBitmap || pHittee == g_pTimmyBitmap)) ||
(pHittee == g_pMissileBitmap && (pHitter == g_pBlobboBitmap |
c++各种小游戏(我们老师的)
5星 · 超过95%的资源 需积分: 44 136 浏览量
2012-06-08
20:37:50
上传
评论 142
收藏 8.51MB RAR 举报
nateYang123
- 粉丝: 388
- 资源: 52
最新资源
- 微信自动抢红包APP.zip
- 微信抢红包神器-红包鸟(可以抢多种情况下的红包,支持手机息屏抢红包、屏蔽群抢红包).zip
- 微信抢红包,支持xposed与免root,xposed支持最新版,免root需自己实现.zip
- 一维卷积神经网络英语电影评论情感分类项目功能实现-Embedding层
- 微信 自动抢红包 插件外挂(Android),实现了监控通知栏,自动点击红包等功能,帮助大家快速抢到红包.zip
- 满满的干货:分享二十个Python学习资源材料.zip
- USD ISO14229-1-2013 中文版
- STM32单片机FPGA毕设电路原理论文报告模糊控制器在无线监控机群系统中的应用
- OpenWrt-Toolchain-ipq
- STM32单片机FPGA毕设电路原理论文报告模糊控制逆变弧焊电源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页