酷代码 AI
菜单

用c++写一个图形界面程序,黑**面有一个绿球和72个黄球,绿球和黄球随机移动,移动后原位置的球出现黑色轨迹。当绿球碰到黄球时,黄球变为绿球,同时加入绿球阵营。当所有球都变绿时,输出从程序开始到现在的时间,不含第三方内容,不能在任何版本的c++上出现错误

要在 C++ 中实现一个图形界面程序,且不使用第三方库,我们可以借助 Windows API 来完成。以下是一个示例代码,它能创建一个黑面,其中有一个绿球和 72 个黄球,这些球会随机移动,移动后原位置会留下黑色轨迹。当绿球碰到黄球时,黄球会变为绿球并加入绿球阵营,当所有球都变为绿球时,会输出从程序开始到现在的时间。 ```cpp #include <windows.h> #include <iostream> #include <vector> #include <cstdlib> #include <ctime> // 球的结构体 struct Ball { int x, y; int dx, dy; COLORREF color; bool isGreen; }; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static std::vector<Ball> balls; static clock_t startTime; switch (msg) { case WM_CREATE: { // 初始化随机数种子 std::srand(static_cast<unsigned int>(std::time(nullptr))); startTime = clock(); // 创建一个绿球 Ball greenBall; greenBall.x = rand() % 800; greenBall.y = rand() % 600; greenBall.dx = (rand() % 3) - 1; greenBall.dy = (rand() % 3) - 1; greenBall.color = RGB(0, 255, 0); greenBall.isGreen = true; balls.push_back(greenBall); // 创建 72 个黄球 for (int i = 0; i < 72; ++i) { Ball yellowBall; yellowBall.x = rand() % 800; yellowBall.y = rand() % 600; yellowBall.dx = (rand() % 3) - 1; yellowBall.dy = (rand() % 3) - 1; yellowBall.color = RGB(255, 255, 0); yellowBall.isGreen = false; balls.push_back(yellowBall); } // 设置定时器,每 10 毫秒触发一次 WM_TIMER 消息 SetTimer(hwnd, 1, 10, NULL); break; } case WM_TIMER: { InvalidateRect(hwnd, NULL, TRUE); for (auto& ball : balls) { // 保存原位置 int oldX = ball.x; int oldY = ball.y; // 移动球 ball.x += ball.dx; ball.y += ball.dy; // 边界检测 if (ball.x < 0 || ball.x > 800) { ball.dx = -ball.dx; } if (ball.y < 0 || ball.y > 600) { ball.dy = -ball.dy; } // 检查绿球是否碰到黄球 if (balls[0].isGreen) { for (size_t i = 1; i < balls.size(); ++i) { if (!balls[i].isGreen) { int dx = balls[0].x - balls[i].x; int dy = balls[0].y - balls[i].y; if (dx * dx + dy * dy <= 100) { // 假设球的半径为 5 balls[i].isGreen = true; balls[i].color = RGB(0, 255, 0); } } } } } // 检查是否所有球都变绿 bool allGreen = true; for (const auto& ball : balls) { if (!ball.isGreen) { allGreen = false; break; } } if (allGreen) { clock_t endTime = clock(); double elapsedTime = static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC; std::cout << "All balls are green! Elapsed time: " << elapsedTime << " seconds." << std::endl; KillTimer(hwnd, 1); } break; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 填充为黑色 HBRUSH hbr = CreateSolidBrush(RGB(0, 0, 0)); FillRect(hdc, &ps.rcPaint, hbr); DeleteObject(hbr); // 绘制球 for (const auto& ball : balls) { HBRUSH hBrush = CreateSolidBrush(ball.color); SelectObject(hdc, hBrush); Ellipse(hdc, ball.x - 5, ball.y - 5, ball.x + 5, ball.y + 5); DeleteObject(hBrush); } EndPaint(hwnd, &ps); 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) { // 注册窗口类 const char CLASS_NAME[] = "BallWindowClass"; WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow( CLASS_NAME, "Ball Simulation", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) { return 0; } // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } ``` 代码说明 1. 结构体 `Ball`:用于存储球的位置、速度、颜色和是否为绿球的信息。 2. 窗口过程函数 `WndProc`:处理窗口消息,包括窗口创建、定时器触发、绘制和销毁等。 3. `WM_CREATE` 消息:初始化随机数种子,创建一个绿球和 72 个黄球,并设置定时器。 4. `WM_TIMER` 消息:移动球,进行边界检测,检查绿球是否碰到黄球,以及是否所有球都变绿。 5. `WM_PAINT` 消息:填充为黑色,绘制所有球。 6. 主函数 `WinMain`:注册窗口类,创建窗口,显示窗口,进入消息循环。 编译和运行 将上述代码保存为 `.cpp` 文件,使用支持 Windows API 的 C++ 编译器(如 Visual Studio)进行编译和运行。 [2026-01-23 08:57:09 | AI问答 | 1767点数解答]

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