UFO.cpp
上传用户:slhang369
上传日期:2022-04-19
资源大小:2452k
文件大小:5k
- //-----------------------------------------------------------------
- // UFO Application
- // C++ Source - UFO.cpp
- //-----------------------------------------------------------------
- //-----------------------------------------------------------------
- // Include Files
- //-----------------------------------------------------------------
- #include "UFO.h"
- //-----------------------------------------------------------------
- // Game Engine Functions
- //-----------------------------------------------------------------
- BOOL GameInitialize(HINSTANCE hInstance)
- {
- // Create the game engine
- g_pGame = new GameEngine(hInstance, TEXT("UFO"),
- TEXT("UFO"), IDI_UFO, IDI_UFO_SM, 500, 400);
- 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)
- {
- // Create and load the background and saucer bitmaps
- HDC hDC = GetDC(hWindow);
- g_pBackground = new Bitmap(hDC, IDB_BACKGROUND, g_hInstance);
- g_pSaucer = new Bitmap(hDC, IDB_SAUCER, g_hInstance);
- // Set the initial saucer position and speed
- g_iSaucerLastX=g_iSaucerX = 250 - (g_pSaucer->GetWidth() / 2);
- g_iSaucerLastY=g_iSaucerY = 200 - (g_pSaucer->GetHeight() / 2);
- g_iSpeedX = 0;
- g_iSpeedY = 0;
- // Play the background music
- g_pGame->PlayMIDISong(TEXT("Music.mid"));
- }
- void GameEnd()
- {
- // Close the MIDI player for the background music
- g_pGame->CloseMIDIPlayer();
- // Cleanup the background and saucer bitmaps
- delete g_pBackground;
- delete g_pSaucer;
- // Cleanup the game engine
- delete g_pGame;
- }
- void GameActivate(HWND hWindow)
- {
- // Resume the background music
- g_pGame->PlayMIDISong(TEXT(""), FALSE);
- }
- void GameDeactivate(HWND hWindow)
- {
- // Pause the background music
- g_pGame->PauseMIDISong();
- }
- void GamePaint(HDC hDC)
- {
- // Draw the background and saucer bitmaps
- g_pBackground->Draw(hDC, 0, 0);
- g_pSaucer->Draw(hDC, g_iSaucerX, g_iSaucerY, TRUE);
-
- }
- void GameCycle()
- {
-
- // Update the saucer position
- g_iSaucerX = min(500 - g_pSaucer->GetWidth(), max(0, g_iSaucerX + g_iSpeedX));
- g_iSaucerY = min(320, max(0, g_iSaucerY + g_iSpeedY));
- if(GetAsyncKeyState(VK_LEFT)==0&&GetAsyncKeyState(VK_RIGHT)==0&&GetAsyncKeyState(VK_UP) ==0&&GetAsyncKeyState(VK_DOWN) ==0)
- {
- g_iSpeedX=0;
- g_iSpeedY=0;
- }
- }
- void HandleKeys()
- {
- HDC hDC;
- HWND hWindow = g_pGame->GetWindow();
-
- // Change the speed of the saucer in response to arrow key presses
- if (GetAsyncKeyState(VK_LEFT) < 0)
- g_iSpeedX = max(-g_iMAXSPEED, --g_iSpeedX);
- else if (GetAsyncKeyState(VK_RIGHT) < 0)
- g_iSpeedX = min(g_iMAXSPEED, ++g_iSpeedX);
- if (GetAsyncKeyState(VK_UP) < 0)
- g_iSpeedY = max(-g_iMAXSPEED, --g_iSpeedY);
- else if (GetAsyncKeyState(VK_DOWN) < 0)
- g_iSpeedY = min(g_iMAXSPEED, ++g_iSpeedY);
- // Draw a line to the new position
- hDC = GetDC(hWindow);
- HPEN hPen = CreatePen(PS_SOLID, 2, RGB(19, 192, 0));
- SelectObject(hDC, hPen);
- MoveToEx(hDC,g_iSaucerLastX+(g_rcRectangle.right - g_rcRectangle.left) / 2,g_iSaucerLastY+(g_rcRectangle.bottom - g_rcRectangle.top) / 2, NULL);
- LineTo(hDC,g_iSaucerX+(g_rcRectangle.right - g_rcRectangle.left) / 2,g_iSaucerY+(g_rcRectangle.bottom - g_rcRectangle.top) / 2);
- g_iSaucerLastX=g_iSaucerX;
- g_iSaucerLastY=g_iSaucerY;
- }
- void MouseButtonDown(int x, int y, BOOL bLeft)
- {
- if (bLeft)
- {
- HDC hDC;
- HWND hWindow = g_pGame->GetWindow();
- RECT rect;
- // Set the saucer position to the mouse position
- g_iSaucerX = x - (g_pSaucer->GetWidth() / 2);
- g_iSaucerY = y - (g_pSaucer->GetHeight() / 2);
-
- // Draw a line to the new position
- hDC = GetDC(hWindow);
- HPEN hPen = CreatePen(PS_SOLID, 2, RGB(19, 192, 0)); // light green color
- SelectObject(hDC, hPen);
- MoveToEx(hDC,g_iSaucerLastX+(g_rcRectangle.right - g_rcRectangle.left) / 2,g_iSaucerLastY+(g_rcRectangle.bottom - g_rcRectangle.top) / 2, NULL);
-
- // Randomly alter the size and position of the new crop circle
- GetClientRect(g_pGame->GetWindow(), &rect);
- int iInflation = (rand() % 17) - 8; // increase or decrease size by up to 8
- InflateRect(&g_rcRectangle, iInflation, iInflation);
- OffsetRect(&g_rcRectangle, g_iSaucerX- g_rcRectangle.left,g_iSaucerY - g_rcRectangle.top);
-
- LineTo(hDC,g_iSaucerX+(g_rcRectangle.right - g_rcRectangle.left) / 2,g_iSaucerY+(g_rcRectangle.bottom - g_rcRectangle.top) / 2);
- g_iSaucerLastX=g_iSaucerX;
- g_iSaucerLastY=g_iSaucerY;
- // Draw the new crop circle
- HBRUSH hBrush = CreateSolidBrush(RGB(192, 92, 10)); // lighter yellow color
- SelectObject(hDC, hBrush);
- Ellipse(hDC, g_rcRectangle.left, g_rcRectangle.top,
- g_rcRectangle.right, g_rcRectangle.bottom);
- ReleaseDC(hWindow, hDC);
- DeleteObject(hBrush);
- DeleteObject(hPen);
- }
- else
- {
- // Stop the saucer
- g_iSpeedX = 0;
- g_iSpeedY = 0;
- }
- }
- void MouseButtonUp(int x, int y, BOOL bLeft)
- {
- }
- void MouseMove(int x, int y)
- {
- }