|
|
// pacman.cpp : 定義應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include "pacman.h"
#include "GObject.h"
#define WLENTH 700
#define WHIGHT 740
#define MAX_LOADSTRING 100
#define STAGE_COUNT 3
// 全局變量:
HINSTANCE hInst; // 當(dāng)前實(shí)例
TCHAR szTitle[MAX_LOADSTRING]; // 標(biāo)題欄文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口類名s
//游戲物體
PacMan* p ;
GObject* e1;
GObject* e2 ;
GObject* e3 ;
GObject* e4 ;
//釋放動(dòng)態(tài)內(nèi)存函數(shù)模板
template<class T>
void Realese(T t)
{
if(t!=NULL)
delete t;
}
// 此代碼模塊中包含的函數(shù)的前向聲明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE,int,HWND&);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void ResetGObjects();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代碼。
int s_n = 0;//進(jìn)行到的關(guān)卡數(shù)
p = new PacMan(P_ROW,P_ARRAY);
e1 =new RedOne(E_ROW,E_ARRAY);
e2 =new RedOne(E_ROW,E_ARRAY);
e3 = new BlueOne(E_ROW,E_ARRAY);
e4 = new YellowOne(E_ROW,E_ARRAY);
GMap* MapArray[STAGE_COUNT] = {new Stage_1(),new Stage_2(),new Stage_3()};
GObject::pStage =MapArray[s_n];//初始化為第一關(guān)地圖
Enermy::player = p;
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PACMAN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 執(zhí)行應(yīng)用程序初始化:
HWND hWnd;
if (!InitInstance (hInstance, nCmdShow,hWnd))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PACMAN));
DWORD t =0;
// 主消息循環(huán):
while(p->GetTw()!=OVER&&s_n<3)
{
if(p->Win())
{
HDC hdc = GetDC(hWnd);
s_n++;
ResetGObjects();
if(s_n <3)
{
MessageBoxA(hWnd,"恭喜您過關(guān)","吃豆子提示",MB_OK);
GObject::pStage = MapArray[s_n];
RECT screenRect;
screenRect.top = 0;
screenRect.left = 0;
screenRect.right = WLENTH;
screenRect.bottom = WHIGHT;
::FillRect(hdc,&screenRect,CreateSolidBrush(RGB(255,255,255)));
GObject::pStage->DrawMap(hdc);
}
continue;
}
if(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(GetAsyncKeyState(VK_DOWN)&0x8000)
{
p->SetTwCommand(DOWN);
}
if(GetAsyncKeyState(VK_LEFT)&0x8000)
{
p->SetTwCommand(LEFT);
}
if(GetAsyncKeyState(VK_RIGHT)&0x8000)
{
p->SetTwCommand(RIGHT);
}
if(GetAsyncKeyState(VK_UP)&0x8000)
{
p->SetTwCommand(UP);
}
else
{
if(GetTickCount()-t>58)
{
HDC hdc = GetDC(hWnd);
e1->action();
e2->action();
e3->action();
e4->action();
p->action();
GObject::pStage->DrawPeas(hdc);
e1->DrawBlank(hdc);
e2->DrawBlank(hdc);
e3->DrawBlank(hdc);
e4->DrawBlank(hdc);
p->DrawBlank(hdc);
e1->Draw(hdc);
e2->Draw(hdc);
e3->Draw(hdc);
e4->Draw(hdc);
p->Draw(hdc);
DeleteDC(hdc);
t = GetTickCount();
}
}
}
Realese(e1);
Realese(e2);
Realese(e3);
Realese(e4);
for(int i = 0;i<STAGE_COUNT;i++)
{
Realese(MapArray[i]);
}
if(p->GetTw()==OVER)
{
MessageBoxA(hWnd,"出師未捷","吃豆子提示",MB_OK);
}
else
{
MessageBoxA(hWnd,"恭喜您贏得了勝利","吃豆子提示",MB_OK);
}
Realese(p);
return (int) msg.wParam;
}
//
// 函數(shù): MyRegisterClass()
//
// 目的: 注冊窗口類。
//
// 注釋:
//
// 僅當(dāng)希望
// 此代碼與添加到 Windows 95 中的“RegisterClassEx”
// 函數(shù)之前的 Win32 系統(tǒng)兼容時(shí),才需要此函數(shù)及其用法。調(diào)用此函數(shù)十分重要,
// 這樣應(yīng)用程序就可以獲得關(guān)聯(lián)的
// “格式正確的”小圖標(biāo)。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PACMAN));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PACMAN);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函數(shù): InitInstance(HINSTANCE, int)
//
// 目的: 保存實(shí)例句柄并創(chuàng)建主窗口
//
// 注釋:
//
// 在此函數(shù)中,我們在全局變量中保存實(shí)例句柄并
// 創(chuàng)建和顯示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow,HWND& hWnd)
{
hInst = hInstance; // 將實(shí)例句柄存儲(chǔ)在全局變量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, WLENTH, WHIGHT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函數(shù): WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 處理主窗口的消息。
//
// WM_COMMAND - 處理應(yīng)用程序菜單
// WM_PAINT - 繪制主窗口
// WM_DESTROY - 發(fā)送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜單選擇:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_START:
case IDM_EXIT:
DestroyWindow(hWnd);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意繪圖代碼...
GObject::pStage->DrawMap(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
::exit(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “關(guān)于”框的消息處理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void ResetGObjects()
{
p->SetPosition(P_ROW,P_ARRAY);
e1->SetPosition(E_ROW,E_ARRAY);
e2->SetPosition(E_ROW,E_ARRAY);
e3->SetPosition(E_ROW,E_ARRAY);
e4->SetPosition(E_ROW,E_ARRAY);
}
|
-
-
圖書管理系統(tǒng).doc
2019-5-9 18:04 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
578 KB, 下載次數(shù): 2, 下載積分: 黑幣 -5
評(píng)分
-
查看全部評(píng)分
|