酷代码 AI
菜单

c++windows api显示一个50像素的正方形和500像素正方形发生碰撞,每次碰撞产生一个圆,圆会随机移动,当碰撞到达一百次时,原本两个灰色的正方形开始变色,顺序为红->橙->黄->绿->青->蓝->紫轮流变色,到达120次后碰撞不产生圆(所有图形的轨迹和界面颜色相同)

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序。该程序会显示两个正方形,当它们发生碰撞时会产生一个随机移动的圆,碰撞达到 100 次时正方形开始变色,达到 120 次后不再产生圆。 ```cpp #include <windows.h> #include <vector> #include <cstdlib> #include <ctime> // 定义颜色数组 const COLORREF colors[] = {RGB(255, 0, 0), RGB(255, 165, 0), RGB(255, 255, 0), RGB(0, 128, 0), RGB(0, 255, 255), RGB(0, 0, 255), RGB(128, 0, 128)}; // 定义圆的结构体 struct Circle { int x, y; int dx, dy; int radius; }; // 全局变量 RECT square1 = {100, 100, 150, 150}; RECT square2 = {600, 100, 1100, 600}; std::vector<Circle> circles; int collisionCount = 0; int colorIndex = 0; // 检查两个矩形是否碰撞 bool isCollision(const RECT& rect1, const RECT& rect2) { return !(rect1.right < rect2.left || rect1.left > rect2.right || rect1.bottom < rect2.top || rect1.top > rect2.bottom); } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制正方形 HBRUSH brush; if (collisionCount >= 100) { brush = CreateSolidBrush(colors[colorIndex]); } else { brush = CreateSolidBrush(RGB(128, 128, 128)); } FillRect(hdc, &square1, brush); DeleteObject(brush); if (collisionCount >= 100) { brush = CreateSolidBrush(colors[colorIndex]); } else { brush = CreateSolidBrush(RGB(128, 128, 128)); } FillRect(hdc, &square2, brush); DeleteObject(brush); // 绘制圆 for (const auto& circle : circles) { brush = CreateSolidBrush(RGB(255, 255, 255)); Ellipse(hdc, circle.x - circle.radius, circle.y - circle.radius, circle.x + circle.radius, circle.y + circle.radius); DeleteObject(brush); } EndPaint(hwnd, &ps); break; } case WM_TIMER: { // 检查碰撞 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount < 120) { // 产生一个新的圆 Circle newCircle; newCircle.x = (square1.left + square1.right) / 2; newCircle.y = (square1.top + square1.bottom) / 2; newCircle.dx = rand() % 5 - 2; newCircle.dy = rand() % 5 - 2; newCircle.radius = 10; circles.push_back(newCircle); } if (collisionCount >= 100) { colorIndex = (colorIndex + 1) % 7; } } // 移动圆 for (auto& circle : circles) { circle.x += circle.dx; circle.y += circle.dy; // 边界检测 RECT clientRect; GetClientRect(hwnd, &clientRect); if (circle.x - circle.radius < clientRect.left || circle.x + circle.radius > clientRect.right) { circle.dx = -circle.dx; } if (circle.y - circle.radius < clientRect.top || circle.y + circle.radius > clientRect.bottom) { circle.dy = -circle.dy; } } 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) { srand(static_cast<unsigned int>(time(NULL))); // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"CollisionWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Collision Demo", 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. 全局变量:定义了两个正方形的矩形区域、圆的向量、碰撞次数和颜色索引。 2. `isCollision` 函数:用于检查两个矩形是否碰撞。 3. `WndProc` 函数:处理窗口消息,包括绘制图形和处理定时器事件。 4. `WinMain` 函数:注册窗口类、创建窗口、显示窗口、设置定时器和进入消息循环。 运行步骤: 1. 将上述代码保存为 `.cpp` 文件。 2. 使用 Visual Studio 或其他 C++ 编译器编译并运行该程序。 注意事项: - 代码中的定时器间隔为 10 毫秒,可以根据需要调整。 - 圆的移动速度和半径可以在代码中进行修改。 [2026-01-23 10:39:20 | AI问答 | 1595点数解答]

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