酷代码 AI
菜单

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

要实现一个满足你需求的 C++ 图形界面程序,由于不能使用第三方库,我们可以使用 Windows API 来创建图形界面。以下是实现该功能的代码: ```cpp #include <windows.h> #include <iostream> #include <vector> #include <ctime> // 球的结构体 struct Ball { int x, y; // 球的位置 int dx, dy; // 球的移动速度 COLORREF color; // 球的颜色 }; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); const int BALL_SIZE = 30; // 球的大小 const int NUM_YELLOW_BALLS = 72; // 黄球的数量 const int WINDOW_WIDTH = 800; // 窗口宽度 const int WINDOW_HEIGHT = 600; // 窗口高度 std::vector<Ball> balls; // 存储所有球的向量 Ball greenBall; // 绿球 clock_t startTime; // 程序开始时间 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("BallGame"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Ball Game"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 初始化随机数种子 srand((unsigned)time(NULL)); // 初始化绿球 greenBall.x = rand() % (WINDOW_WIDTH - BALL_SIZE); greenBall.y = rand() % (WINDOW_HEIGHT - BALL_SIZE); greenBall.dx = (rand() % 3) - 1; greenBall.dy = (rand() % 3) - 1; greenBall.color = RGB(0, 255, 0); // 初始化黄球 for (int i = 0; i < NUM_YELLOW_BALLS; i++) { Ball yellowBall; yellowBall.x = rand() % (WINDOW_WIDTH - BALL_SIZE); yellowBall.y = rand() % (WINDOW_HEIGHT - BALL_SIZE); yellowBall.dx = (rand() % 3) - 1; yellowBall.dy = (rand() % 3) - 1; yellowBall.color = RGB(255, 255, 0); balls.push_back(yellowBall); } startTime = clock(); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } // 检查两个球是否碰撞 bool isCollision(const Ball& ball1, const Ball& ball2) { int dx = ball1.x - ball2.x; int dy = ball1.y - ball2.y; int distanceSquared = dx * dx + dy * dy; return distanceSquared <= (BALL_SIZE * BALL_SIZE); } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); // 绘制绿球 SetDCBrushColor(hdc, greenBall.color); Ellipse(hdc, greenBall.x, greenBall.y, greenBall.x + BALL_SIZE, greenBall.y + BALL_SIZE); // 绘制黄球 for (const auto& ball : balls) { SetDCBrushColor(hdc, ball.color); Ellipse(hdc, ball.x, ball.y, ball.x + BALL_SIZE, ball.y + BALL_SIZE); } EndPaint(hwnd, &ps); break; case WM_TIMER: // 清除原位置 InvalidateRect(hwnd, NULL, TRUE); // 移动绿球 greenBall.x += greenBall.dx; greenBall.y += greenBall.dy; // 边界检测 if (greenBall.x < 0 || greenBall.x > WINDOW_WIDTH - BALL_SIZE) { greenBall.dx = -greenBall.dx; } if (greenBall.y < 0 || greenBall.y > WINDOW_HEIGHT - BALL_SIZE) { greenBall.dy = -greenBall.dy; } // 移动黄球并检查碰撞 for (auto& ball : balls) { ball.x += ball.dx; ball.y += ball.dy; // 边界检测 if (ball.x < 0 || ball.x > WINDOW_WIDTH - BALL_SIZE) { ball.dx = -ball.dx; } if (ball.y < 0 || ball.y > WINDOW_HEIGHT - BALL_SIZE) { ball.dy = -ball.dy; } // 检查碰撞 if (ball.color == RGB(255, 255, 0) && isCollision(greenBall, ball)) { ball.color = RGB(0, 255, 0); } } // 检查是否所有球都变绿 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 = (double)(endTime - startTime) / CLOCKS_PER_SEC; std::cout << "All balls are green! Elapsed time: " << elapsedTime << " seconds." << std::endl; KillTimer(hwnd, 1); } // 重绘窗口 InvalidateRect(hwnd, NULL, FALSE); break; case WM_CREATE: // 设置定时器 SetTimer(hwnd, 1, 10, NULL); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } ``` 代码说明: 1. 结构体 `Ball`:用于存储球的位置、移动速度和颜色。 2. `WinMain` 函数:创建窗口,初始化随机数种子,初始化绿球和黄球,并启动程序。 3. `WndProc` 函数:处理窗口消息,包括绘制球、移动球、检查碰撞和定时器消息。 4. `isCollision` 函数:检查两个球是否碰撞。 5. 定时器:每隔 10 毫秒触发一次定时器消息,用于移动球和检查碰撞。 6. 碰撞检测:当绿球碰到黄球时,将黄球的颜色变为绿色。 7. 结束条件:当所有球都变为绿色时,输出从程序开始到现在的时间。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用支持 Windows API 的编译器(如 Visual Studio)进行编译和运行。 [2026-01-23 09:04:23 | AI问答 | 1988点数解答]

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