酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序。该程序会创建一个窗口,在窗口中显示两个正方形,它们会自动碰撞,每次碰撞会产生一个圆,圆会随机移动。当碰撞次数达到 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 = {100, 100, 50, 2, 2, RGB(128, 128, 128)}; Square square2 = {300, 300, 100, -2, -2, RGB(128, 128, 128)}; 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); } // 窗口过程函数 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); // 绘制圆 for (const auto& circle : circles) { hBrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, hBrush); Ellipse(hdc, circle.x - circle.radius, circle.y - circle.radius, circle.x + circle.radius, circle.y + circle.radius); 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; // 边界检测 RECT rect; GetClientRect(hwnd, &rect); if (square1.x < 0 || square1.x + square1.size > rect.right) square1.dx = -square1.dx; if (square1.y < 0 || square1.y + square1.size > rect.bottom) square1.dy = -square1.dy; if (square2.x < 0 || square2.x + square2.size > rect.right) square2.dx = -square2.dx; if (square2.y < 0 || square2.y + square2.size > rect.bottom) square2.dy = -square2.dy; // 检查碰撞 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount < 60) { Circle newCircle = {square1.x + square1.size / 2, square1.y + square1.size / 2, 10, rand() % 4 - 2, rand() % 4 - 2}; circles.push_back(newCircle); } if (collisionCount >= 50) { colorIndex = (collisionCount - 50) % 7; square1.color = colors[colorIndex]; square2.color = colors[colorIndex]; } square1.dx = -square1.dx; square1.dy = -square1.dy; square2.dx = -square2.dx; square2.dy = -square2.dy; } // 移动圆 for (auto& circle : circles) { circle.x += circle.dx; circle.y += circle.dy; if (circle.x - circle.radius < 0 || circle.x + circle.radius > rect.right) circle.dx = -circle.dx; if (circle.y - circle.radius < 0 || circle.y + circle.radius > rect.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) { // 注册窗口类 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, 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 (int)msg.wParam; } ``` 代码说明: 1. 结构体定义:定义了 `Square` 和 `Circle` 结构体,分别表示正方形和圆。 2. 碰撞检测:`isCollision` 函数用于检查两个正方形是否碰撞。 3. 窗口过程函数:`WndProc` 处理窗口消息,包括绘制图形、移动图形、检查碰撞等。 4. 主函数:`WinMain` 是程序的入口点,负责注册窗口类、创建窗口、显示窗口、设置定时器和消息循环。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用 Visual Studio 或其他 C++ 编译器进行编译和运行。运行程序后,你将看到两个正方形自动移动并碰撞,每次碰撞会产生一个圆,圆会随机移动。当碰撞次数达到 50 次时,两个正方形开始按顺序变色,达到 60 次后碰撞不再产生圆。 [2026-01-23 10:44:40 | AI问答 | 1887点数解答]

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