酷代码 AI
菜单

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

要实现一个具有图形界面的 C++ 程序,同时满足绿球和黄球随机移动、碰撞变色以及计时功能,且不使用第三方库,可以使用 Windows API 来创建图形界面。以下是实现该功能的代码: ```cpp #include <windows.h> #include <iostream> #include <vector> #include <ctime> // 球的结构体 struct Ball { int x, y; COLORREF color; int dx, dy; }; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static std::vector<Ball> balls; static clock_t startTime = clock(); switch (msg) { case WM_CREATE: { // 初始化绿球 Ball greenBall; greenBall.x = rand() % 800; greenBall.y = rand() % 600; greenBall.color = RGB(0, 255, 0); greenBall.dx = (rand() % 3) - 1; greenBall.dy = (rand() % 3) - 1; balls.push_back(greenBall); // 初始化 72 个黄球 for (int i = 0; i < 72; ++i) { Ball yellowBall; yellowBall.x = rand() % 800; yellowBall.y = rand() % 600; yellowBall.color = RGB(255, 255, 0); yellowBall.dx = (rand() % 3) - 1; yellowBall.dy = (rand() % 3) - 1; balls.push_back(yellowBall); } // 设置定时器,每 10 毫秒触发一次 WM_TIMER 消息 SetTimer(hwnd, 1, 10, NULL); break; } case WM_TIMER: { // 重绘窗口 InvalidateRect(hwnd, NULL, TRUE); // 检查是否所有球都变为绿色 bool allGreen = true; for (const auto& ball : balls) { if (ball.color != RGB(0, 255, 0)) { allGreen = false; break; } } if (allGreen) { 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; KillTimer(hwnd, 1); } // 移动球 for (auto& ball : balls) { 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; } // 检查绿球与黄球的碰撞 for (auto& ball1 : balls) { if (ball1.color == RGB(0, 255, 0)) { for (auto& ball2 : balls) { if (ball2.color == RGB(255, 255, 0)) { int distance = (ball1.x - ball2.x) * (ball1.x - ball2.x) + (ball1.y - ball2.y) * (ball1.y - ball2.y); if (distance < 100) { ball2.color = RGB(0, 255, 0); } } } } } break; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制球 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) { // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"BallWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Ball Movement", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return static_cast<int>(msg.wParam); } ``` 代码解释: 1. Ball 结构体:定义了球的位置、颜色和移动速度。 2. WndProc 函数:窗口过程函数,处理各种窗口消息。 - `WM_CREATE`:初始化绿球和 72 个黄球,并设置定时器。 - `WM_TIMER`:移动球,检查碰撞,重绘窗口。 - `WM_PAINT`:绘制所有球。 - `WM_DESTROY`:结束程序。 3. WinMain 函数:程序入口,注册窗口类,创建窗口,进入消息循环。 注意事项: - 该程序使用 Windows API 实现图形界面,只能在 Windows 系统上运行。 - 球的移动速度和碰撞检测的精度可以根据需要进行调整。 [2026-01-23 08:53:17 | AI问答 | 1525点数解答]

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