问题是这样的:
//my_pacman.cpp: 定义应用程序的入口点。 // #include "stdafx.h" #pragma comment(lib,"winmm.lib") #include <Windows.h> #include "my_pacman.h" #include "GMap.h" #include "GObject.h" #include <mmsystem.h> #include <vector> #include <iostream> using namespace std; #define WLENTH 1400 #define WHIGHT 1050 #define STAGE_COUNT 2 static HWND g_hwnd; #define MAX_LOADSTRING 100 // 全局变量: HINSTANCE hInst; // 当前实例这个类型其实是VS自己定义的变量类型,真实类型是void*,是一个指针类型 WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, // main函数, 程序入口点; _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MYPACMAN, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); if (!InitInstance(hInstance, nCmdShow)) { return FALSE; } //HACCEL加速键 HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MYPACMAN)); int s_n = 0; // 一开始设置第一关; GMap *MapArray[STAGE_COUNT] = { new Stage_1(),new Stage_2()}; // 三个指向地图的指针 MSG msg; DWORD dwLastTime = 0; DWORD time_sign = 0; GObject* All_E[100]; auto g_me = std::make_shared<PacMan>(P_ROW, P_ARRAY); auto p = std::make_shared<PacMan>(P_ROW + 1, P_ARRAY + 1); auto e1 = std::make_shared <RedOne>(E_ROW, E_ARRAY); auto e2 = std::make_shared <RedOne>(E_ROW, E_ARRAY); auto e3 = std::make_shared <BlueOne>(E_ROW, E_ARRAY); auto e4 = std::make_shared <YellowOne>(E_ROW, E_ARRAY); GObject* e; int total = 0; for (int i = 0; i < total; i += 2) { e = new YellowOne(E_ROW, E_ARRAY); All_E[i] = e; } for (int i = 1; i < total; i += 2) { e = new BlueOne(E_ROW, E_ARRAY); All_E[i] = e; } int my_total = 0; MessageBoxA(g_hwnd, "您即将开始一段全新的旅程,准备好了吗!", "吃豆子提示", MB_YESNO|MB_ICONWARNING); // 用法 for (s_n = 0; s_n < STAGE_COUNT; s_n++) { ShowWindow(g_hwnd, nCmdShow); UpdateWindow(g_hwnd); if (s_n != 0) MessageBoxA(g_hwnd, "您挺厉害的嘛!,准备好迎接新的挑战了吗!", "吃豆子提示", MB_YESNO | MB_ICONWARNING); dwLastTime = 0; time_sign = 0; my_total = 0; e1->SetPosition(E_ROW, E_ARRAY); e2->SetPosition(E_ROW, E_ARRAY); e3->SetPosition(E_ROW, E_ARRAY); e4->SetPosition(E_ROW, E_ARRAY); p->SetPosition(P_ROW, P_ARRAY); g_me->SetPosition(P_ROW, P_ARRAY); GObject::pStage = MapArray[s_n]; Enermy::player = g_me; RECT screenRect; screenRect.top = 0; screenRect.left = 0; screenRect.right = WLENTH; screenRect.bottom = WHIGHT; HDC hdc = GetDC(g_hwnd); std::shared_ptr<HDC__> dc(hdc, [](HDC hdc) { ::ReleaseDC(g_hwnd, hdc); }); ::FillRect(dc.get(), &screenRect, CreateSolidBrush(RGB(255, 255, 255))); GObject::pStage->DrawMap(hdc); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // 其他消息处理 { TranslateMessage(&msg); DispatchMessage(&msg); } PlaySound(L"1.wav", NULL, SND_FILENAME | SND_ASYNC); while (1) { if (g_me->IsOver() || g_me->IsWin()) break; if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // 其他消息处理 { TranslateMessage(&msg); DispatchMessage(&msg); } if (GetTickCount() - time_sign >= 6000 && my_total < total) { my_total++; time_sign = GetTickCount(); } if (GetTickCount() - dwLastTime >= 40) { dwLastTime = GetTickCount(); HDC hdc = ::GetDC(g_hwnd); std::shared_ptr<HDC__> dc(hdc, [](HDC hdc) { ::ReleaseDC(g_hwnd, hdc); }); MapArray[s_n]->DrawPeas(hdc); for (int j = 0; j < my_total; j++) { All_E[j]->action(); All_E[j]->DrawBlank(hdc); All_E[j]->Draw(hdc); } e1->action(); e1->DrawBlank(hdc); e1->Draw(hdc); e2->action(); e2->DrawBlank(hdc); e2->Draw(hdc); e3->action(); e3->DrawBlank(hdc); e3->Draw(hdc); e4->action(); e4->DrawBlank(hdc); e4->Draw(hdc); g_me->DrawBlank(hdc); // g_me->Draw(hdc); g_me->action(); p->DrawBlank(hdc); // p->Draw(hdc); p->action(); if (GetAsyncKeyState(VK_DOWN) & 0x8000) { g_me->SetTwCommand(DOWN); } if (GetAsyncKeyState(VK_LEFT) & 0x8000) { g_me->SetTwCommand(LEFT); } if (GetAsyncKeyState(VK_RIGHT) & 0x8000) { g_me->SetTwCommand(RIGHT); } if (GetAsyncKeyState(VK_UP) & 0x8000) { g_me->SetTwCommand(UP); } if (GetAsyncKeyState('S') & 0x8000) { // PacMan* p = dynamic_cast<PacMan*>(All_E[total]); // if (p != 0) p->SetTwCommand(DOWN); } if (GetAsyncKeyState('A') & 0x8000) { //PacMan* p = dynamic_cast<PacMan*>(All_E[total]); //if (p != 0) p->SetTwCommand(LEFT); } if (GetAsyncKeyState('D') & 0x8000) { //PacMan* p = dynamic_cast<PacMan*>(All_E[total]); //if (p != 0) p->SetTwCommand(RIGHT); } if (GetAsyncKeyState('W') & 0x8000) { //PacMan* p = dynamic_cast<PacMan*>(All_E[total]); //if (p != 0) p->SetTwCommand(UP); } } else continue; } if (g_me->IsOver()) { MessageBox(g_hwnd, _T("输了"), _T("吃豆子提示"), MB_OK); } //break; else { PlaySound(L"2.wav", NULL, SND_FILENAME | SND_ASYNC); MessageBox(g_hwnd, _T("恭喜过关"), _T("吃豆子提示"), MB_OK); } } for (int i = 0; i < total; i++) { if (All_E[i] != 0) delete All_E[i]; } if (s_n < 2) // 这是跳出了While循环之后 要么是死了, 要么是赢了; { PlaySound(L"3.wav", NULL, SND_FILENAME | SND_ASYNC); MessageBoxA(g_hwnd, "出师未捷身先死", "吃豆子提示", MB_OK); } else { MessageBoxA(g_hwnd, "赢了! \r\n确定后退出", "吃豆子提示", MB_OK); } return (int)msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance)//typedef unsigned short WORD; typedef WORD ATOM; { WNDCLASSEXW wcex;// WNDCLASSEXW是一个窗口类结构体; 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_MYPACMAN)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MYPACMAN); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); // 注册是否成功 } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 0, 0, WLENTH, WHIGHT, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } g_hwnd = hWnd; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此处添加使用 hdc 的任何绘图代码... EndPaint(hWnd, &ps); UpdateWindow(hWnd); } break; case WM_DESTROY: PostQuitMessage(0); ::exit(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “关于”框的消息处理程序。 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; }
当运行时间较长时,就在游戏结束的时候不能正常显示提示信息 就是MessageBox函数不能正常显示,一闪而过,并且主窗口一片空白,没有画面。其他时候都还正常
以下为主函数
```
求大神解答!!!
```
好久没有搞MFC的程序了。有报什么错误吗?要分析一下程序堆载信息。
我水平太菜了,没调试出来,您方便运行一下源代码吗,我发给您