Meteor Defense —— 超级简单的射击游戏

来源:互联网 发布:宝果网络 编辑:程序博客网 时间:2024/05/22 14:02

 基于前一篇文章中的游戏引擎,一个简单的射击类游戏Meteor Defense.

游戏规则:鼠标左键点击发射导弹,击中流星则加分(呵呵,可不能无休止点击,每发射一枚导弹则减1分)。鼠标右键重新开始。

程序注释为英文,但没什么难度。:)

程序清单:

  1. //-----------------------------------------------------------------
  2. // Meteor Defense Application
  3. // C++ Header - MeteorDefense.h
  4. //-----------------------------------------------------------------
  5. #pragma once
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include <windows.h>
  10. #include "Resource.h"
  11. #include "GameEngine.h"
  12. #include "Bitmap.h"
  13. #include "Sprite.h"
  14. #include "Background.h"
  15. //-----------------------------------------------------------------
  16. // Global Variables
  17. //-----------------------------------------------------------------
  18. HINSTANCE         g_hInstance;
  19. GameEngine*       g_pGame;
  20. HDC               g_hOffscreenDC;
  21. HBITMAP           g_hOffscreenBitmap;
  22. Bitmap*           g_pGroundBitmap;
  23. Bitmap*           g_pTargetBitmap;
  24. Bitmap*           g_pCityBitmap;
  25. Bitmap*           g_pMeteorBitmap;
  26. Bitmap*           g_pMissileBitmap;
  27. Bitmap*           g_pExplosionBitmap;
  28. Bitmap*           g_pGameOverBitmap;
  29. StarryBackground* g_pBackground;
  30. Sprite*           g_pTargetSprite;
  31. int               g_iNumCities, g_iScore, g_iDifficulty;
  32. BOOL              g_bGameOver;
  33. //-----------------------------------------------------------------
  34. // Function Declarations
  35. //-----------------------------------------------------------------
  36. void NewGame();   //new game
  37. void AddMeteor(); //add a Meteor
  1. //-----------------------------------------------------------------
  2. // Meteor Defense Application
  3. // C++ Source - MeteorDefense.cpp
  4. //-----------------------------------------------------------------
  5. //-----------------------------------------------------------------
  6. // Include Files
  7. //-----------------------------------------------------------------
  8. #include "MeteorDefense.h"
  9. //-----------------------------------------------------------------
  10. // Game Engine Functions
  11. //-----------------------------------------------------------------
  12. BOOL GameInitialize(HINSTANCE hInstance)
  13. {
  14.   // Create the game engine
  15.   g_pGame = new GameEngine(hInstance, TEXT("Meteor Defense"),
  16.     TEXT("Meteor Defense"), IDI_METEORDEFENSE, IDI_METEORDEFENSE_SM, 600, 450);
  17.   if (g_pGame == NULL)
  18.     return FALSE;
  19.   // Set the frame rate
  20.   g_pGame->SetFrameRate(30);
  21.   // Store the instance handle
  22.   g_hInstance = hInstance;
  23.   return TRUE;
  24. }
  25. void GameStart(HWND hWindow)
  26. {
  27.   // Seed the random number generator
  28.   srand(GetTickCount());
  29.   // Create the offscreen device context and bitmap
  30.   g_hOffscreenDC = CreateCompatibleDC(GetDC(hWindow));
  31.   g_hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow),
  32.     g_pGame->GetWidth(), g_pGame->GetHeight());
  33.   SelectObject(g_hOffscreenDC, g_hOffscreenBitmap);
  34.   // Create and load the bitmaps
  35.   HDC hDC = GetDC(hWindow);
  36.   g_pGroundBitmap = new Bitmap(hDC, IDB_GROUND, g_hInstance);
  37.   g_pTargetBitmap = new Bitmap(hDC, IDB_TARGET, g_hInstance);
  38.   g_pCityBitmap = new Bitmap(hDC, IDB_CITY, g_hInstance);
  39.   g_pMeteorBitmap = new Bitmap(hDC, IDB_METEOR, g_hInstance);
  40.   g_pMissileBitmap = new Bitmap(hDC, IDB_MISSILE, g_hInstance);
  41.   g_pExplosionBitmap = new Bitmap(hDC, IDB_EXPLOSION, g_hInstance);
  42.   g_pGameOverBitmap = new Bitmap(hDC, IDB_GAMEOVER, g_hInstance);
  43.   // Create the starry background
  44.   g_pBackground = new StarryBackground(600, 450);
  45.   // Play the background music
  46.   g_pGame->PlayMIDISong(TEXT("Music.mid"));
  47.   // Start the game
  48.   NewGame();
  49. }
  50. void GameEnd()
  51. {
  52.   // Close the MIDI player for the background music
  53.   g_pGame->CloseMIDIPlayer();
  54.   // Cleanup the offscreen device context and bitmap
  55.   DeleteObject(g_hOffscreenBitmap);
  56.   DeleteDC(g_hOffscreenDC);  
  57.   // Cleanup the bitmaps
  58.   delete g_pGroundBitmap;
  59.   delete g_pTargetBitmap;
  60.   delete g_pCityBitmap;
  61.   delete g_pMeteorBitmap;
  62.   delete g_pMissileBitmap;
  63.   delete g_pExplosionBitmap;
  64.   delete g_pGameOverBitmap;
  65.   // Cleanup the background
  66.   delete g_pBackground;
  67.   // Cleanup the sprites
  68.   g_pGame->CleanupSprites();
  69.   // Cleanup the game engine
  70.   delete g_pGame;
  71. }
  72. void GameActivate(HWND hWindow)
  73. {
  74.   // Resume the background music
  75.   g_pGame->PlayMIDISong(TEXT(""), FALSE);
  76. }
  77. void GameDeactivate(HWND hWindow)
  78. {
  79.   // Pause the background music
  80.   g_pGame->PauseMIDISong();
  81. }
  82. void GamePaint(HDC hDC)
  83. {
  84.   // Draw the background
  85.   g_pBackground->Draw(hDC);
  86.   // Draw the ground bitmap
  87.   g_pGroundBitmap->Draw(hDC, 0, 398, TRUE);
  88.   // Draw the sprites
  89.   g_pGame->DrawSprites(hDC);
  90.   // Draw the score
  91.   TCHAR szText[64];
  92.   RECT  rect = { 275, 0, 325, 50 };
  93.   wsprintf(szText, "%d", g_iScore);
  94.   SetBkMode(hDC, TRANSPARENT);
  95.   SetTextColor(hDC, RGB(255, 255, 255));
  96.   DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  97.   // Draw the game over message, if necessary
  98.   if (g_bGameOver)
  99.     g_pGameOverBitmap->Draw(hDC, 170, 150, TRUE);
  100. }
  101. void GameCycle()
  102. {
  103.   if (!g_bGameOver)
  104.   {
  105.     // Randomly add meteors
  106.     if ((rand() % g_iDifficulty) == 0)
  107.       AddMeteor();
  108.     // Update the background
  109.     g_pBackground->Update();
  110.     // Update the sprites
  111.     g_pGame->UpdateSprites();
  112.     // Obtain a device context for repainting the game
  113.     HWND  hWindow = g_pGame->GetWindow();
  114.     HDC   hDC = GetDC(hWindow);
  115.     // Paint the game to the offscreen device context
  116.     GamePaint(g_hOffscreenDC);
  117.     // Blit the offscreen bitmap to the game screen
  118.     BitBlt(hDC, 0, 0, g_pGame->GetWidth(), g_pGame->GetHeight(),
  119.       g_hOffscreenDC, 0, 0, SRCCOPY);
  120.     // Cleanup
  121.     ReleaseDC(hWindow, hDC);
  122.   }
  123. }
  124. void HandleKeys()
  125. {
  126. }
  127. void MouseButtonDown(int x, int y, BOOL bLeft)
  128. {
  129.   if (!g_bGameOver && bLeft)
  130.   {
  131.     // Create a new missile sprite and set its position
  132.     RECT    rcBounds = { 0, 0, 600, 450 };
  133.     int     iXPos = (x < 300) ? 144 : 449;
  134.     Sprite* pSprite = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE);
  135.     pSprite->SetPosition(iXPos, 365);
  136.     // Calculate the velocity so that it is aimed at the target
  137.     int iXVel, iYVel = -6;
  138.     y = min(y, 300);
  139.     iXVel = (iYVel * ((iXPos + 8) - x)) / (365 - y);
  140.     pSprite->SetVelocity(iXVel, iYVel);
  141.     // Add the missile sprite
  142.     g_pGame->AddSprite(pSprite);
  143.     // Play the fire sound
  144.     PlaySound((LPCSTR)IDW_FIRE, g_hInstance, SND_ASYNC |
  145.       SND_RESOURCE | SND_NOSTOP);
  146.     // Update the score
  147.     g_iScore = max(--g_iScore, 0);
  148.   }
  149.   else if (g_bGameOver && !bLeft)
  150.     // Start a new game
  151.     NewGame();
  152. }
  153. void MouseButtonUp(int x, int y, BOOL bLeft)
  154. {
  155. }
  156. void MouseMove(int x, int y)
  157. {
  158.   // Track the mouse with the target sprite
  159.   g_pTargetSprite->SetPosition(x - (g_pTargetSprite->GetWidth() / 2),
  160.     y - (g_pTargetSprite->GetHeight() / 2));
  161. }
  162. void HandleJoystick(JOYSTATE jsJoystickState)
  163. {
  164. }
  165. BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
  166. {
  167.   // See if a missile and a meteor have collided
  168.   if ((pSpriteHitter->GetBitmap() == g_pMissileBitmap &
  169.     pSpriteHittee->GetBitmap() == g_pMeteorBitmap) ||
  170.     (pSpriteHitter->GetBitmap() == g_pMeteorBitmap &
  171.     pSpriteHittee->GetBitmap() == g_pMissileBitmap))
  172.   {
  173.     // Kill both sprites
  174.     pSpriteHitter->Kill();
  175.     pSpriteHittee->Kill();
  176.     // Update the score
  177.     g_iScore += 6;
  178.     g_iDifficulty = max(50 - (g_iScore / 10), 5);
  179.   }
  180.   // See if a meteor has collided with a city
  181.   if (pSpriteHitter->GetBitmap() == g_pMeteorBitmap &
  182.     pSpriteHittee->GetBitmap() == g_pCityBitmap)
  183.   {
  184.     // Play the big explosion sound
  185.     PlaySound((LPCSTR)IDW_BIGEXPLODE, g_hInstance, SND_ASYNC |
  186.       SND_RESOURCE);
  187.     // Kill both sprites
  188.     pSpriteHitter->Kill();
  189.     pSpriteHittee->Kill();
  190.     // See if the game is over
  191.     if (--g_iNumCities == 0)
  192.       g_bGameOver = TRUE;
  193.   }
  194.   return FALSE;
  195. }
  196. void SpriteDying(Sprite* pSpriteDying)
  197. {
  198.   // See if a meteor sprite is dying
  199.   if (pSpriteDying->GetBitmap() == g_pMeteorBitmap)
  200.   {
  201.     // Play the explosion sound
  202.     PlaySound((LPCSTR)IDW_EXPLODE, g_hInstance, SND_ASYNC |
  203.       SND_RESOURCE | SND_NOSTOP);
  204.     // Create an explosion sprite at the meteor's position
  205.     RECT rcBounds = { 0, 0, 600, 450 };
  206.     RECT rcPos = pSpriteDying->GetPosition();
  207.     Sprite* pSprite = new Sprite(g_pExplosionBitmap, rcBounds);
  208.     pSprite->SetNumFrames(12, TRUE);
  209.     pSprite->SetPosition(rcPos.left, rcPos.top);
  210.     g_pGame->AddSprite(pSprite);
  211.   }
  212. }
  213. //-----------------------------------------------------------------
  214. // Functions
  215. //-----------------------------------------------------------------
  216. void NewGame()
  217. {
  218.   // Clear the sprites
  219.   g_pGame->CleanupSprites();
  220.   // Create the target sprite
  221.   RECT rcBounds = { 0, 0, 600, 450 };
  222.   g_pTargetSprite = new Sprite(g_pTargetBitmap, rcBounds, BA_STOP);
  223.   g_pTargetSprite->SetZOrder(10);
  224.   g_pGame->AddSprite(g_pTargetSprite);
  225.   // Create the city sprites
  226.   Sprite* pSprite = new Sprite(g_pCityBitmap, rcBounds);
  227.   pSprite->SetPosition(2, 370);
  228.   g_pGame->AddSprite(pSprite);
  229.   pSprite = new Sprite(g_pCityBitmap, rcBounds);
  230.   pSprite->SetPosition(186, 370);
  231.   g_pGame->AddSprite(pSprite);
  232.   pSprite = new Sprite(g_pCityBitmap, rcBounds);
  233.   pSprite->SetPosition(302, 370);
  234.   g_pGame->AddSprite(pSprite);
  235.   pSprite = new Sprite(g_pCityBitmap, rcBounds);
  236.   pSprite->SetPosition(490, 370);
  237.   g_pGame->AddSprite(pSprite);
  238.   // Initialize the game variables
  239.   g_iScore = 0;
  240.   g_iNumCities = 4;
  241.   g_iDifficulty = 50;
  242.   g_bGameOver = FALSE;
  243.   // Play the background music
  244.   g_pGame->PlayMIDISong();
  245. }
  246. void AddMeteor()
  247. {
  248.   // Create a new meteor sprite and set its position
  249.   RECT    rcBounds = { 0, 0, 600, 390 };
  250.   int     iXPos = rand() % 600;
  251.   Sprite* pSprite = new Sprite(g_pMeteorBitmap, rcBounds, BA_DIE);
  252.   pSprite->SetNumFrames(14);
  253.   pSprite->SetPosition(iXPos, 0);
  254.   // Calculate the velocity so that it is aimed at one of the cities
  255.   int iXVel, iYVel = (rand() % 4) + 3;
  256.   switch(rand() % 4)
  257.   {
  258.   case 0:
  259.     iXVel = (iYVel * (56 - (iXPos + 50))) / 400;
  260.     break;
  261.   case 1:
  262.     iXVel = (iYVel * (240 - (iXPos + 50))) / 400;
  263.     break;
  264.   case 2:
  265.     iXVel = (iYVel * (360 - (iXPos + 50))) / 400;
  266.     break;
  267.   case 3:
  268.     iXVel = (iYVel * (546 - (iXPos + 50))) / 400;
  269.     break;
  270.   }
  271.   pSprite->SetVelocity(iXVel, iYVel);
  272.   // Add the meteor sprite
  273.   g_pGame->AddSprite(pSprite);
  274. }
  1. //-----------------------------------------------------------------
  2. // Meteor Defense Resource Identifiers
  3. // C++ Header - Resource.h
  4. //-----------------------------------------------------------------
  5. //-----------------------------------------------------------------
  6. // Icons                      Range : 1000 - 1999
  7. //-----------------------------------------------------------------
  8. #define IDI_METEORDEFENSE     1000
  9. #define IDI_METEORDEFENSE_SM  1001
  10. //-----------------------------------------------------------------
  11. // Bitmaps                    Range : 2000 - 2999
  12. //-----------------------------------------------------------------
  13. #define IDB_GROUND            2000
  14. #define IDB_TARGET            2001
  15. #define IDB_CITY              2002
  16. #define IDB_METEOR            2003
  17. #define IDB_MISSILE           2004
  18. #define IDB_EXPLOSION         2005
  19. #define IDB_GAMEOVER          2006
  20. //-----------------------------------------------------------------
  21. // Wave Sounds                Range : 3000 - 3999
  22. //-----------------------------------------------------------------
  23. #define IDW_FIRE              3000
  24. #define IDW_EXPLODE           3001
  25. #define IDW_BIGEXPLODE        3002
  1. //-----------------------------------------------------------------
  2. // Meteor Defense Resources
  3. // RC Source - MeteorDefense.rc
  4. //-----------------------------------------------------------------
  5. //-----------------------------------------------------------------
  6. // Include Files
  7. //-----------------------------------------------------------------
  8. #include "Resource.h"
  9. //-----------------------------------------------------------------
  10. // Icons
  11. //-----------------------------------------------------------------
  12. IDI_METEORDEFENSE     ICON         "Res//MeteorDefense.ico"
  13. IDI_METEORDEFENSE_SM  ICON         "Res//MeteorDefense_sm.ico"
  14. //-----------------------------------------------------------------
  15. // Bitmaps
  16. //-----------------------------------------------------------------
  17. IDB_GROUND            BITMAP       "Res//Ground.bmp"
  18. IDB_TARGET            BITMAP       "Res//Target.bmp"
  19. IDB_CITY              BITMAP       "Res//City.bmp"
  20. IDB_METEOR            BITMAP       "Res//Meteor.bmp"
  21. IDB_MISSILE           BITMAP       "Res//Missile.bmp"
  22. IDB_EXPLOSION         BITMAP       "Res//Explosion.bmp"
  23. IDB_GAMEOVER          BITMAP       "Res//GameOver.bmp"
  24. //-----------------------------------------------------------------
  25. // Wave Sounds
  26. //-----------------------------------------------------------------
  27. IDW_FIRE              WAVE         "Res//Fire.wav"
  28. IDW_EXPLODE           WAVE         "Res//Explode.wav"
  29. IDW_BIGEXPLODE        WAVE         "Res//BigExplode.wav"

游戏截图:

 

原创粉丝点击