酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序: ```cpp #include <windows.h> #include <iostream> #include <ctime> #include <vector> // 定义窗口大小 const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; // 定义正方形和圆的结构体 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; 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 isCollision(Square s1, 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); } // 边界检测 void boundaryCheck(Square& s) { if (s.x <= 0 || s.x + s.size >= WINDOW_WIDTH) { s.dx = -s.dx; } if (s.y <= 0 || s.y + s.size >= WINDOW_HEIGHT) { s.dy = -s.dy; } } // 移动圆 void moveCircles() { for (auto& c : circles) { c.x += c.dx; c.y += c.dy; // 圆的边界检测 if (c.x - c.radius <= 0 || c.x + c.radius >= WINDOW_WIDTH) { c.dx = -c.dx; } if (c.y - c.radius <= 0 || c.y + c.radius >= WINDOW_HEIGHT) { c.dy = -c.dy; } } } // 窗口过程函数 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(RGB(0, 0, 0)); FillRect(hdc, &ps.rcPaint, hBrush); DeleteObject(hBrush); // 绘制正方形 hBrush = CreateSolidBrush(square1.color); RECT rect1 = {square1.x, square1.y, square1.x + square1.size, square1.y + square1.size}; FillRect(hdc, &rect1, hBrush); DeleteObject(hBrush); hBrush = CreateSolidBrush(square2.color); RECT rect2 = {square2.x, square2.y, square2.x + square2.size, square2.y + square2.size}; FillRect(hdc, &rect2, hBrush); DeleteObject(hBrush); // 绘制圆 for (const auto& c : circles) { hBrush = CreateSolidBrush(RGB(255, 255, 255)); Ellipse(hdc, c.x - c.radius, c.y - c.radius, c.x + c.radius, c.y + c.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; // 边界检测 boundaryCheck(square1); boundaryCheck(square2); // 检查碰撞 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount >= 50) { square1.color = colors[colorIndex % 7]; square2.color = colors[colorIndex % 7]; 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() % 5) - 2; newCircle.dy = (rand() % 5) - 2; circles.push_back(newCircle); } } // 移动圆 moveCircles(); // 重绘窗口 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, 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 msg.wParam; } ``` 代码说明: 1. 结构体定义:定义了 `Square` 和 `Circle` 结构体,分别表示正方形和圆。 2. 全局变量:定义了两个正方形 `square1` 和 `square2`,以及存储圆的向量 `circles`,还有碰撞次数 `collisionCount` 和颜色数组 `colors`。 3. 碰撞检测:`isCollision` 函数用于检查两个正方形是否碰撞。 4. 边界检测:`boundaryCheck` 函数用于检查正方形是否到达边界,如果到达则反弹。 5. 窗口过程函数:`WndProc` 处理窗口消息,包括绘制图形和处理定时器消息。 6. 主函数:`WinMain` 注册窗口类,创建窗口,设置定时器,并进入消息循环。 编译和运行: 将上述代码保存为 `.cpp` 文件,使用 Visual Studio 或其他 C++ 编译器进行编译和运行。运行程序后,你将看到两个正方形在黑色上移动,每次碰撞会产生一个圆,当碰撞次数达到 50 次时,正方形开始变色,达到 60 次后不再产生圆。 [2026-01-23 15:20:49 | AI问答 | 1916点数解答]

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