酷代码 AI
菜单

c++windows api黑色背景显示一个随机可通行迷宫,显示一个圆在起点,可通过上下左右键移动,直到终点,显示从起点到现在的时间。

以下是一个使用 Windows API 实现的 C++ 程序,它可以显示一个随机可通行的迷宫,在起点处显示一个圆,允许用户通过上下左右键移动这个圆,直到到达终点,并显示从起点开始到当前的时间。 ```cpp #include <windows.h> #include <iostream> #include <ctime> #include <cstdlib> #include <vector> // 迷宫的大小 const int MAZE_WIDTH = 21; const int MAZE_HEIGHT = 21; // 迷宫数组 char maze[MAZE_HEIGHT][MAZE_WIDTH]; // 玩家位置 int playerX, playerY; // 起点和终点位置 int startX, startY, endX, endY; // 时间记录 clock_t startTime; // 生成迷宫 void generateMaze() { // 初始化迷宫为墙 for (int y = 0; y < MAZE_HEIGHT; ++y) { for (int x = 0; x < MAZE_WIDTH; ++x) { if (x % 2 == 0 || y % 2 == 0) { maze[y][x] = '#'; } else { maze[y][x] = ' '; } } } // 随机选择起点和终点 startX = 1; startY = 1; endX = MAZE_WIDTH - 2; endY = MAZE_HEIGHT - 2; // 深度优先搜索生成迷宫 std::vector<int> stackX, stackY; stackX.push_back(startX); stackY.push_back(startY); maze[startY][startX] = ' '; while (!stackX.empty()) { int currentX = stackX.back(); int currentY = stackY.back(); std::vector<int> directions; if (currentX - 2 > 0 && maze[currentY][currentX - 2] == '#') directions.push_back(0); if (currentX + 2 < MAZE_WIDTH - 1 && maze[currentY][currentX + 2] == '#') directions.push_back(1); if (currentY - 2 > 0 && maze[currentY - 2][currentX] == '#') directions.push_back(2); if (currentY + 2 < MAZE_HEIGHT - 1 && maze[currentY + 2][currentX] == '#') directions.push_back(3); if (!directions.empty()) { int randomDirection = directions[rand() % directions.size()]; switch (randomDirection) { case 0: maze[currentY][currentX - 1] = ' '; maze[currentY][currentX - 2] = ' '; stackX.push_back(currentX - 2); stackY.push_back(currentY); break; case 1: maze[currentY][currentX + 1] = ' '; maze[currentY][currentX + 2] = ' '; stackX.push_back(currentX + 2); stackY.push_back(currentY); break; case 2: maze[currentY - 1][currentX] = ' '; maze[currentY - 2][currentX] = ' '; stackX.push_back(currentX); stackY.push_back(currentY - 2); break; case 3: maze[currentY + 1][currentX] = ' '; maze[currentY + 2][currentX] = ' '; stackX.push_back(currentX); stackY.push_back(currentY + 2); break; } } else { stackX.pop_back(); stackY.pop_back(); } } // 设置起点和终点 maze[startY][startX] = 'S'; maze[endY][endX] = 'E'; playerX = startX; playerY = startY; } // 绘制迷宫 void drawMaze(HDC hdc) { for (int y = 0; y < MAZE_HEIGHT; ++y) { for (int x = 0; x < MAZE_WIDTH; ++x) { if (maze[y][x] == '#') { Rectangle(hdc, x * 20, y * 20, (x + 1) * 20, (y + 1) * 20); } else if (maze[y][x] == 'S') { Ellipse(hdc, startX * 20 + 5, startY * 20 + 5, startX * 20 + 15, startY * 20 + 15); } else if (maze[y][x] == 'E') { Ellipse(hdc, endX * 20 + 5, endY * 20 + 5, endX * 20 + 15, endY * 20 + 15); } } } // 绘制玩家 Ellipse(hdc, playerX * 20 + 5, playerY * 20 + 5, playerX * 20 + 15, playerY * 20 + 15); } // 处理按键事件 void handleKeyPress(int key) { int newX = playerX; int newY = playerY; switch (key) { case VK_UP: newY--; break; case VK_DOWN: newY++; break; case VK_LEFT: newX--; break; case VK_RIGHT: newX++; break; } if (maze[newY][newX] != '#') { playerX = newX; playerY = newY; if (playerX == endX && playerY == endY) { // 到达终点 std::cout << "You reached the end! Time: " << (clock() - startTime) / CLOCKS_PER_SEC << " seconds." << std::endl; exit(0); } } } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: srand(time(NULL)); generateMaze(); startTime = clock(); break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 设置黑色 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0)); FillRect(hdc, &ps.rcPaint, hBrush); DeleteObject(hBrush); // 绘制迷宫 drawMaze(hdc); // 显示时间 clock_t currentTime = clock(); char timeStr[20]; sprintf_s(timeStr, "Time: %d s", (currentTime - startTime) / CLOCKS_PER_SEC); SetTextColor(hdc, RGB(255, 255, 255)); TextOut(hdc, 10, MAZE_HEIGHT * 20 + 10, timeStr, strlen(timeStr)); EndPaint(hwnd, &ps); break; } case WM_KEYDOWN: handleKeyPress(wParam); InvalidateRect(hwnd, NULL, TRUE); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } // 主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = "MazeWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, "Maze Game", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, MAZE_WIDTH * 20 + 20, MAZE_HEIGHT * 20 + 60, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ``` 代码说明: 1. 迷宫生成:使用深度优先搜索算法生成一个随机可通行的迷宫。 2. 玩家移动:通过上下左右键控制玩家在迷宫中移动。 3. 时间记录:记录从起点开始到当前的时间,并在窗口中显示。 4. 图形绘制:使用 Windows API 的绘图函数绘制迷宫、玩家和时间信息。 编译和运行: 将上述代码保存为一个 `.cpp` 文件,使用支持 Windows API 的 C++ 编译器(如 Visual Studio)进行编译和运行。运行程序后,你将看到一个黑色的迷宫窗口,通过上下左右键控制圆在迷宫中移动,直到到达终点,程序将显示你所用的时间。 [2026-01-24 09:37:13 | AI问答 | 2263点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多