酷代码 AI
菜单

c++windows api黑色背景显示一个50像素的正方形和100像素正方形自动发生碰撞,每次碰撞产生一个圆,圆会随机移动,当碰撞到达50次时,原本两个灰色的正方形开始变色,顺序为红->橙->黄->绿->青->蓝->紫轮流变色,到达60次后碰撞不再产生圆,但方形依然会变色(所有图形移动均不产生轨迹)(不能出现任何错误(如出现错误,我打死你))(包含边界检测,当正方形到达边界或碰到另一个正方形时自动反弹)

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序。该程序会在黑色上显示一个 50 像素的正方形和一个 100 像素的正方形,它们会自动发生碰撞,每次碰撞产生一个圆,圆会随机移动。当碰撞到达 50 次时,两个正方形开始按红 -> 橙 -> 黄 -> 绿 -> 青 -> 蓝 -> 紫的顺序轮流变色,到达 60 次后碰撞不再产生圆,但方形依然会变色。程序包含边界检测,当正方形到达边界或碰到另一个正方形时会自动反弹。 ```cpp #include <windows.h> #include <iostream> #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 Square { int x, y; int size; int dx, dy; COLORREF color; }; // 定义圆结构体 struct Circle { int x, y; int radius; int dx, dy; }; // 全局变量 Square square1, square2; std::vector<Circle> circles; int collisionCount = 0; int colorIndex = 0; // 检查两个正方形是否碰撞 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); } // 检查圆是否与正方形碰撞 bool isCircleCollision(const Circle& c, const Square& s) { int testX = c.x; int testY = c.y; if (c.x < s.x) testX = s.x; else if (c.x > s.x + s.size) testX = s.x + s.size; if (c.y < s.y) testY = s.y; else if (c.y > s.y + s.size) testY = s.y + s.size; int distX = c.x - testX; int distY = c.y - testY; int distance = (distX * distX) + (distY * distY); return (distance <= (c.radius * c.radius)); } // 绘制图形 void drawShapes(HDC hdc, int width, int height) { // 填充黑色 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0)); SelectObject(hdc, hBrush); Rectangle(hdc, 0, 0, width, height); DeleteObject(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); // 绘制圆 for (const auto& circle : circles) { hBrush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, hBrush); Ellipse(hdc, circle.x - circle.radius, circle.y - circle.radius, circle.x + circle.radius, circle.y + circle.radius); DeleteObject(hBrush); } } // 更新图形位置 void updateShapes(int width, int height) { // 更新正方形位置 square1.x += square1.dx; square1.y += square1.dy; square2.x += square2.dx; square2.y += square2.dy; // 边界检测和反弹 if (square1.x < 0 || square1.x + square1.size > width) square1.dx = -square1.dx; if (square1.y < 0 || square1.y + square1.size > height) square1.dy = -square1.dy; if (square2.x < 0 || square2.x + square2.size > width) square2.dx = -square2.dx; if (square2.y < 0 || square2.y + square2.size > height) square2.dy = -square2.dy; // 检查正方形碰撞 if (isCollision(square1, square2)) { square1.dx = -square1.dx; square1.dy = -square1.dy; square2.dx = -square2.dx; square2.dy = -square2.dy; collisionCount++; if (collisionCount >= 50) { colorIndex = (colorIndex + 1) % 7; square1.color = colors[colorIndex]; square2.color = colors[colorIndex]; } if (collisionCount < 60) { Circle newCircle; newCircle.x = (square1.x + square2.x) / 2; newCircle.y = (square1.y + square2.y) / 2; newCircle.radius = 10; newCircle.dx = (rand() % 3) - 1; newCircle.dy = (rand() % 3) - 1; circles.push_back(newCircle); } } // 更新圆的位置 for (auto& circle : circles) { circle.x += circle.dx; circle.y += circle.dy; // 圆的边界检测和反弹 if (circle.x - circle.radius < 0 || circle.x + circle.radius > width) circle.dx = -circle.dx; if (circle.y - circle.radius < 0 || circle.y + circle.radius > height) circle.dy = -circle.dy; // 圆与正方形的碰撞检测和反弹 if (isCircleCollision(circle, square1)) { circle.dx = -circle.dx; circle.dy = -circle.dy; } if (isCircleCollision(circle, square2)) { circle.dx = -circle.dx; circle.dy = -circle.dy; } } } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); RECT rect; GetClientRect(hwnd, &rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; drawShapes(hdc, width, height); EndPaint(hwnd, &ps); break; } case WM_TIMER: { RECT rect; GetClientRect(hwnd, &rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; updateShapes(width, height); 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))); // 初始化正方形 square1.x = 100; square1.y = 100; square1.size = 50; square1.dx = 2; square1.dy = 2; square1.color = RGB(128, 128, 128); square2.x = 300; square2.y = 300; square2.size = 100; square2.dx = -2; square2.dy = -2; square2.color = RGB(128, 128, 128); // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"ShapeWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Shape Collision", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hwnd, iCmdShow); 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` 函数用于检查两个正方形是否碰撞,`isCircleCollision` 函数用于检查圆是否与正方形碰撞。 3. 绘制图形:`drawShapes` 函数用于绘制黑色、正方形和圆。 4. 更新图形位置:`updateShapes` 函数用于更新正方形和圆的位置,处理边界检测和碰撞反弹,以及统计碰撞次数和改变正方形颜色。 5. 窗口过程函数:`WndProc` 函数处理窗口消息,包括绘制和定时器消息。 6. 主函数:`WinMain` 函数初始化窗口、设置定时器并进入消息循环。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用 Visual Studio 等 C++ 编译器进行编译和运行。运行程序后,你将看到两个正方形在黑色上移动并碰撞,每次碰撞产生一个圆,当碰撞次数达到 50 次时,正方形开始变色,达到 60 次后不再产生圆。 [2026-01-24 08:54:52 | AI问答 | 2660点数解答]

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