酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 代码示例: ```cpp #include <windows.h> #include <iostream> #include <vector> #include <cstdlib> #include <ctime> // 全局变量 const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; const int SQUARE_SIZE_1 = 50; const int SQUARE_SIZE_2 = 100; const int CIRCLE_SIZE = 20; // 正方形结构体 struct Square { int x, y; int size; COLORREF color; int dx, dy; }; // 圆形结构体 struct Circle { int x, y; int size; int dx, dy; }; // 全局变量 Square square1, square2; std::vector<Circle> circles; int collisionCount = 0; int colorIndex = 0; 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)}; // 碰撞检测函数 bool isCollision(const Square& s1, const Square& s2) { return (s1.x < s2.x + s2.size && s1.x + s1.size > s2.x && s1.y < s2.y + s2.size && s1.y + s1.size > s2.y); } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制正方形 HBRUSH hBrush = CreateSolidBrush(square1.color); SelectObject(hdc, hBrush); Rectangle(hdc, square1.x, square1.y, square1.x + square1.size, square1.y + square1.size); DeleteObject(hBrush); hBrush = CreateSolidBrush(square2.color); SelectObject(hdc, hBrush); Rectangle(hdc, square2.x, square2.y, square2.x + square2.size, square2.y + square2.size); DeleteObject(hBrush); // 绘制圆形 hBrush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, hBrush); for (const auto& circle : circles) { Ellipse(hdc, circle.x - circle.size / 2, circle.y - circle.size / 2, circle.x + circle.size / 2, circle.y + circle.size / 2); } DeleteObject(hBrush); EndPaint(hwnd, &ps); break; } case WM_TIMER: { // 移动正方形 square1.x += square1.dx; square1.y += square1.dy; square2.x += square2.dx; square2.y += square2.dy; // 边界检测 if (square1.x < 0 || square1.x + square1.size > WINDOW_WIDTH) square1.dx = -square1.dx; if (square1.y < 0 || square1.y + square1.size > WINDOW_HEIGHT) square1.dy = -square1.dy; if (square2.x < 0 || square2.x + square2.size > WINDOW_WIDTH) square2.dx = -square2.dx; if (square2.y < 0 || square2.y + square2.size > WINDOW_HEIGHT) square2.dy = -square2.dy; // 碰撞检测 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount < 60) { Circle newCircle; newCircle.x = (square1.x + square2.x) / 2; newCircle.y = (square1.y + square2.y) / 2; newCircle.size = CIRCLE_SIZE; newCircle.dx = (rand() % 5) - 2; newCircle.dy = (rand() % 5) - 2; circles.push_back(newCircle); } if (collisionCount >= 50) { square1.color = colors[colorIndex % 7]; square2.color = colors[colorIndex % 7]; colorIndex++; } } // 移动圆形 for (auto& circle : circles) { circle.x += circle.dx; circle.y += circle.dy; if (circle.x - circle.size / 2 < 0 || circle.x + circle.size / 2 > WINDOW_WIDTH) circle.dx = -circle.dx; if (circle.y - circle.size / 2 < 0 || circle.y + circle.size / 2 > 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, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"CollisionWindow"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Collision Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); // 初始化正方形 square1.x = 100; square1.y = 100; square1.size = SQUARE_SIZE_1; square1.color = RGB(128, 128, 128); square1.dx = 2; square1.dy = 2; square2.x = 300; square2.y = 300; square2.size = SQUARE_SIZE_2; square2.color = RGB(128, 128, 128); square2.dx = -2; square2.dy = -2; // 初始化随机数种子 srand(static_cast<unsigned int>(time(nullptr))); // 显示窗口 ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 16, NULL); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return static_cast<int>(msg.wParam); } ``` 代码说明: 1. 结构体定义:定义了 `Square` 和 `Circle` 结构体,分别用于表示正方形和圆形。 2. 碰撞检测:`isCollision` 函数用于检测两个正方形是否发生碰撞。 3. 窗口过程函数:`WndProc` 处理窗口消息,包括绘制图形(`WM_PAINT`)和定时移动图形(`WM_TIMER`)。 4. 主函数:`WinMain` 是程序的入口点,负责注册窗口类、创建窗口、初始化正方形和圆形、设置定时器和消息循环。 运行步骤: 1. 打开 Visual Studio 或其他 C++ 开发环境。 2. 创建一个新的 Windows 控制台应用程序项目。 3. 将上述代码复制到源文件中。 4. 编译并运行程序。 程序运行后,会显示两个正方形,它们会自动移动并发生碰撞。每次碰撞会产生一个圆形,圆形会随机移动。当碰撞次数达到 50 次时,正方形会开始变色。当碰撞次数达到 60 次时,不再产生新的圆形,但正方形依然会变色。所有图形移动产生的轨迹为白色。 [2026-01-23 15:03:43 | AI问答 | 2042点数解答]

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