//-----------------------------------------------------------------
// 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++各种小游戏(我们老师的)
共593个文件
bmp:162个
h:125个
cpp:101个
5星 · 超过95%的资源 需积分: 44 3.4k 下载量 185 浏览量
2012-06-08
20:37:50
上传
评论 142
收藏 8.51MB RAR 举报
温馨提示
这是我们老师做的,各种小游戏,我感觉还不错,所以传上来玩玩。是c++做的,有的可以用来交作业和课程设计都行的,而且对于c++游戏初学者的话很有帮助的。
资源推荐
资源详情
资源评论
收起资源包目录
c++各种小游戏(我们老师的) (593个子文件)
Jumper.ai 906KB
Background.ai 653KB
Splash.ai 367KB
GameOver.ai 228KB
Background_Landscape.bmp 2.64MB
Image1.bmp 900KB
Image5.bmp 900KB
Image1.bmp 900KB
Image5.bmp 900KB
Image2.bmp 900KB
Image2.bmp 900KB
Image3.bmp 900KB
Image3.bmp 900KB
Image4.bmp 900KB
Image4.bmp 900KB
Galaxy.bmp 703KB
Galaxy.bmp 703KB
Background.bmp 586KB
Background.bmp 586KB
Office.bmp 586KB
Background.bmp 586KB
Office.bmp 586KB
Background_Sky.bmp 550KB
Background_Road.bmp 550KB
Background_Trees.bmp 550KB
Background_Mountains.bmp 550KB
Background_Clouds.bmp 432KB
Asteroid.bmp 336KB
Asteroid.bmp 336KB
Splash.bmp 234KB
GameOver.bmp 234KB
Splash.bmp 208KB
Splash.bmp 208KB
Splash.bmp 208KB
Highway.bmp 184KB
Highway.bmp 184KB
Explosion.bmp 144KB
Desert.bmp 139KB
Desert.bmp 139KB
Desert.bmp 139KB
Desert.bmp 139KB
Guy4.bmp 119KB
GameOver.bmp 116KB
GameOver.bmp 116KB
GameOver.bmp 116KB
GameOver.bmp 116KB
GameOver.bmp 116KB
GameOver.bmp 116KB
GameOver.bmp 116KB
Guy5.bmp 113KB
Jumper.bmp 92KB
Ground.bmp 91KB
Meteor.bmp 85KB
Tile3.bmp 50KB
Tile1.bmp 50KB
Tile7.bmp 50KB
Tile8.bmp 50KB
Tile2.bmp 50KB
TileBlank.bmp 50KB
Tile5.bmp 50KB
Tile6.bmp 50KB
Tile4.bmp 50KB
Tile7.bmp 50KB
Tile3.bmp 50KB
Tile6.bmp 50KB
Tile1.bmp 50KB
Tile8.bmp 50KB
Tile5.bmp 50KB
TileBlank.bmp 50KB
Tile4.bmp 50KB
Tile2.bmp 50KB
Guy4.bmp 30KB
Guy5.bmp 28KB
Car1.bmp 28KB
Car1.bmp 28KB
Guy3.bmp 27KB
Guy3.bmp 27KB
LgExplosion.bmp 27KB
LgExplosion.bmp 27KB
LgExplosion.bmp 27KB
LgExplosion.bmp 27KB
Jelly.bmp 26KB
Jelly.bmp 26KB
Jelly.bmp 26KB
Jelly.bmp 26KB
Blobbo.bmp 26KB
Blobbo.bmp 26KB
Blobbo.bmp 26KB
Blobbo.bmp 26KB
Planet1.bmp 25KB
Planet1.bmp 25KB
Planet2.bmp 24KB
Planet2.bmp 24KB
Car3.bmp 23KB
Car3.bmp 23KB
Car4.bmp 21KB
Car2.bmp 21KB
Car4.bmp 21KB
Car2.bmp 21KB
City.bmp 19KB
共 593 条
- 1
- 2
- 3
- 4
- 5
- 6
nateYang123
- 粉丝: 388
- 资源: 52
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页