酷代码 AI
菜单

用c++写一个图形界面程序,有一个绿球和72个黄球,绿球和黄球随机移动,当绿球碰到黄球时,黄球变为绿球,同时加入绿球阵营。当所有球都变绿时,输出从程序开始到现在的时间,不含第三方内容

以下是一个使用 C++ 和 Windows API 实现的图形界面程序,满足你的需求。程序中有一个绿球和 72 个黄球,绿球和黄球随机移动,当绿球碰到黄球时,黄球变为绿球并加入绿球阵营,当所有球都变绿时,输出从程序开始到现在的时间。 ```cpp #include <windows.h> #include <iostream> #include <vector> #include <ctime> // 球的结构体 struct Ball { int x, y; // 球的位置 int dx, dy; // 球的移动速度 COLORREF color; // 球的颜色 }; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static std::vector<Ball> balls; static Ball greenBall; static clock_t startTime = clock(); static bool allGreen = false; switch (msg) { case WM_CREATE: { // 初始化绿球 greenBall.x = rand() % 800; greenBall.y = rand() % 600; greenBall.dx = (rand() % 3) - 1; greenBall.dy = (rand() % 3) - 1; greenBall.color = RGB(0, 255, 0); // 初始化 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); balls.push_back(yellowBall); } break; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制绿球 HPEN hPen = CreatePen(PS_SOLID, 2, greenBall.color); HBRUSH hBrush = CreateSolidBrush(greenBall.color); SelectObject(hdc, hPen); SelectObject(hdc, hBrush); Ellipse(hdc, greenBall.x - 10, greenBall.y - 10, greenBall.x + 10, greenBall.y + 10); DeleteObject(hPen); DeleteObject(hBrush); // 绘制黄球 for (const auto& ball : balls) { hPen = CreatePen(PS_SOLID, 2, ball.color); hBrush = CreateSolidBrush(ball.color); SelectObject(hdc, hPen); SelectObject(hdc, hBrush); Ellipse(hdc, ball.x - 10, ball.y - 10, ball.x + 10, ball.y + 10); DeleteObject(hPen); DeleteObject(hBrush); } EndPaint(hwnd, &ps); break; } case WM_TIMER: { // 移动绿球 greenBall.x += greenBall.dx; greenBall.y += greenBall.dy; // 边界检测 if (greenBall.x < 10 || greenBall.x > 790) greenBall.dx = -greenBall.dx; if (greenBall.y < 10 || greenBall.y > 590) greenBall.dy = -greenBall.dy; // 移动黄球并检测碰撞 int greenCount = 0; for (auto& ball : balls) { ball.x += ball.dx; ball.y += ball.dy; // 边界检测 if (ball.x < 10 || ball.x > 790) ball.dx = -ball.dx; if (ball.y < 10 || ball.y > 590) ball.dy = -ball.dy; // 检测绿球和黄球的碰撞 int dx = greenBall.x - ball.x; int dy = greenBall.y - ball.y; if (dx * dx + dy * dy <= 400 && ball.color == RGB(255, 255, 0)) { ball.color = RGB(0, 255, 0); } if (ball.color == RGB(0, 255, 0)) ++greenCount; } // 检查是否所有球都变绿 if (greenCount == 72 && !allGreen) { allGreen = true; clock_t endTime = clock(); double elapsedTime = static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC; std::cout << "All balls turned green in " << elapsedTime << " seconds." << std::endl; } // 重绘窗口 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 = L"BallWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Ball Game", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 10, NULL); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return static_cast<int>(msg.wParam); } ``` 代码说明: 1. 结构体 `Ball`:用于表示球的位置、移动速度和颜色。 2. 窗口过程函数 `WndProc`:处理窗口消息,包括窗口创建、绘制、定时器和销毁消息。 3. `WM_CREATE` 消息:初始化绿球和 72 个黄球的位置、速度和颜色。 4. `WM_PAINT` 消息:在窗口上绘制绿球和黄球。 5. `WM_TIMER` 消息:移动绿球和黄球,检测边界和碰撞,当绿球碰到黄球时,将黄球变为绿球。如果所有球都变绿,输出从程序开始到现在的时间。 6. 主函数 `WinMain`:注册窗口类,创建窗口,显示窗口,设置定时器,并进入消息循环。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用 Visual Studio 或其他 C++ 编译器进行编译和运行。运行程序后,会弹出一个窗口,绿球和黄球会随机移动,当所有球都变绿时,会在控制台输出所用的时间。 [2026-01-23 08:43:07 | AI问答 | 1799点数解答]

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