酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 代码示例: ```cpp #include <windows.h> #include <iostream> #include <cstdlib> #include <ctime> // 定义常量 const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; const int ALL_SQUARE_SIZE = 50; const int LARGE_SQUARE_SIZE = 100; const int CIRCLE_SIZE = 20; // 全局变量 int smallX = 100; int smallY = 100; int smallDx = 2; int smallDy = 2; int largeX = 300; int largeY = 300; int largeDx = -2; int largeDy = -2; int collisionCount = 0; // 圆的结构体 struct Circle { int x; int y; int dx; int dy; }; // 存储圆的向量 std::vector<Circle> circles; // 颜色数组 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)}; int colorIndex = 0; // 检查碰撞 bool checkCollision(int x1, int y1, int size1, int x2, int y2, int size2) { return (x1 < x2 + size2 && x1 + size1 > x2 && y1 < y2 + size2 && y1 + size1 > y2); } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制轨迹 HPEN whitePen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255)); SelectObject(hdc, whitePen); // 绘制圆 for (const auto& circle : circles) { Ellipse(hdc, circle.x, circle.y, circle.x + CIRCLE_SIZE, circle.y + CIRCLE_SIZE); } // 绘制正方形 COLORREF squareColor = (collisionCount >= 50) ? colors[colorIndex] : RGB(128, 128, 128); HBRUSH squareBrush = CreateSolidBrush(squareColor); SelectObject(hdc, squareBrush); Rectangle(hdc, smallX, smallY, smallX + ALL_SQUARE_SIZE, smallY + ALL_SQUARE_SIZE); Rectangle(hdc, largeX, largeY, largeX + LARGE_SQUARE_SIZE, largeY + LARGE_SQUARE_SIZE); DeleteObject(whitePen); DeleteObject(squareBrush); EndPaint(hwnd, &ps); break; } case WM_TIMER: { // 移动小正方形 smallX += smallDx; smallY += smallDy; // 边界检查 if (smallX < 0 || smallX + ALL_SQUARE_SIZE > WINDOW_WIDTH) smallDx = -smallDx; if (smallY < 0 || smallY + ALL_SQUARE_SIZE > WINDOW_HEIGHT) smallDy = -smallDy; // 移动大正方形 largeX += largeDx; largeY += largeDy; // 边界检查 if (largeX < 0 || largeX + LARGE_SQUARE_SIZE > WINDOW_WIDTH) largeDx = -largeDx; if (largeY < 0 || largeY + LARGE_SQUARE_SIZE > WINDOW_HEIGHT) largeDy = -largeDy; // 检查碰撞 if (checkCollision(smallX, smallY, ALL_SQUARE_SIZE, largeX, largeY, LARGE_SQUARE_SIZE)) { collisionCount++; if (collisionCount < 60) { // 产生圆 Circle newCircle; newCircle.x = (smallX + largeX) / 2; newCircle.y = (smallY + largeY) / 2; newCircle.dx = (rand() % 5) - 2; newCircle.dy = (rand() % 5) - 2; circles.push_back(newCircle); } if (collisionCount >= 50) { colorIndex = (colorIndex + 1) % 7; } } // 移动圆 for (auto& circle : circles) { circle.x += circle.dx; circle.y += circle.dy; // 圆的边界检查 if (circle.x < 0 || circle.x + CIRCLE_SIZE > WINDOW_WIDTH) circle.dx = -circle.dx; if (circle.y < 0 || circle.y + CIRCLE_SIZE > WINDOW_HEIGHT) 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(nullptr))); // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"CollisionWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Collision Simulation", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, 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. 碰撞检查:`checkCollision` 函数用于检查两个矩形是否发生碰撞。 3. 窗口过程函数:`WndProc` 处理窗口消息,包括绘制图形和处理定时器事件。在定时器事件中,移动正方形和圆,检查碰撞,并根据碰撞次数进行相应的处理。 4. 主函数:`WinMain` 初始化窗口,设置定时器,并进入消息循环。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用 Visual Studio 等 C++ 编译器进行编译和运行。运行程序后,你将看到两个正方形自动移动并发生碰撞,每次碰撞产生一个圆,当碰撞次数达到 50 次时,正方形开始变色,达到 60 次后不再产生圆。 [2026-01-23 10:47:09 | AI问答 | 1850点数解答]

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