酷代码 AI
菜单

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

要实现一个满足你需求的 C++ 图形界面程序,并且不使用第三方库,可以使用 Windows API 来创建窗口和绘制图形。以下是一个示例代码: ```cpp #include <windows.h> #include <iostream> #include <vector> #include <ctime> #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define BALL_RADIUS 10 #define YELLOW_BALL_COUNT 72 // 球的结构体 struct Ball { int x, y; int dx, dy; bool isGreen; Ball(int _x, int _y, bool _isGreen) : x(_x), y(_y), isGreen(_isGreen) { // 随机速度 dx = (rand() % 3) - 1; if (dx == 0) dx = 1; dy = (rand() % 3) - 1; if (dy == 0) dy = 1; } }; // 全局变量 std::vector<Ball> balls; clock_t startTime; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: // 初始化随机数种子 srand(static_cast<unsigned int>(time(nullptr))); startTime = clock(); // 创建绿球 balls.emplace_back(rand() % (WINDOW_WIDTH - 2 * BALL_RADIUS) + BALL_RADIUS, rand() % (WINDOW_HEIGHT - 2 * BALL_RADIUS) + BALL_RADIUS, true); // 创建黄球 for (int i = 0; i < YELLOW_BALL_COUNT; ++i) { balls.emplace_back(rand() % (WINDOW_WIDTH - 2 * BALL_RADIUS) + BALL_RADIUS, rand() % (WINDOW_HEIGHT - 2 * BALL_RADIUS) + BALL_RADIUS, false); } // 设置定时器,每隔一段时间更新球的位置 SetTimer(hwnd, 1, 10, nullptr); break; case WM_TIMER: { // 清除所有球 InvalidateRect(hwnd, nullptr, TRUE); // 更新球的位置 for (auto& ball : balls) { ball.x += ball.dx; ball.y += ball.dy; // 边界检测 if (ball.x - BALL_RADIUS < 0 || ball.x + BALL_RADIUS > WINDOW_WIDTH) { ball.dx = -ball.dx; } if (ball.y - BALL_RADIUS < 0 || ball.y + BALL_RADIUS > WINDOW_HEIGHT) { ball.dy = -ball.dy; } } // 检测绿球和黄球的碰撞 for (auto& ball1 : balls) { if (ball1.isGreen) { for (auto& ball2 : balls) { if (!ball2.isGreen) { int dx = ball1.x - ball2.x; int dy = ball1.y - ball2.y; int distance = static_cast<int>(sqrt(dx * dx + dy * dy)); if (distance <= 2 * BALL_RADIUS) { ball2.isGreen = true; } } } } } // 检查是否所有球都变绿 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); } // 重绘窗口 InvalidateRect(hwnd, nullptr, FALSE); break; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制所有球 for (const auto& ball : balls) { HBRUSH brush = ball.isGreen ? CreateSolidBrush(RGB(0, 255, 0)) : CreateSolidBrush(RGB(255, 255, 0)); SelectObject(hdc, brush); Ellipse(hdc, ball.x - BALL_RADIUS, ball.y - BALL_RADIUS, ball.x + BALL_RADIUS, ball.y + BALL_RADIUS); DeleteObject(brush); } 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) { // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"BallWindow"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Ball Movement", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, nullptr, nullptr, hInstance, nullptr); // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 消息循环 MSG msg = {}; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return static_cast<int>(msg.wParam); } ``` 代码说明: 1. 结构体 `Ball`:定义了球的位置、速度和颜色(是否为绿色)。 2. 窗口过程函数 `WndProc`:处理窗口消息,包括创建窗口、定时器消息、绘制消息和销毁窗口消息。 3. 定时器:每隔 10 毫秒更新球的位置和检测碰撞。 4. 碰撞检测:当绿球碰到黄球时,将黄球变为绿球。 5. 时间计算:当所有球都变绿时,输出从程序开始到现在的时间。 6. 主函数 `WinMain`:注册窗口类、创建窗口、显示窗口并进入消息循环。 编译和运行该程序,你将看到一个包含一个绿球和 72 个黄球的窗口,球会随机移动,并且满足碰撞和颜色变化的条件。当所有球都变绿时,程序会输出所用的时间。 [2026-01-23 08:48:26 | AI问答 | 1674点数解答]

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