酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序示例。该程序创建一个窗口,在窗口中显示两个正方形,使它们自动发生碰撞,每次碰撞产生一个圆,圆会随机移动。当碰撞达到一百次时,两个正方形开始按指定顺序变色,到达 120 次后碰撞不再产生圆。 ```cpp #include <windows.h> #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #define WM_COLLISION (WM_USER + 1) // 颜色数组 const COLORREF colors[] = { RGB(255, 0, 0), RGB(255, 165, 0), RGB(255, 255, 0), RGB(0, 255, 0), RGB(0, 255, 255), RGB(0, 0, 255), RGB(128, 0, 128) }; // 圆的结构体 struct Circle { int x, y; int dx, dy; int radius = 10; }; // 全局变量 const int SQUARE_SIZE1 = 50; const int SQUARE_SIZE2 = 500; int square1X = 100, square1Y = 100; int square2X = 700, square2Y = 100; int dx1 = 2, dy1 = 2; int dx2 = -2, dy2 = -2; int collisionCount = 0; std::vector<Circle> circles; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("CollisionApp"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Collision Simulation"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 初始化随机数种子 std::srand(std::time(nullptr)); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } // 检测碰撞 bool isColliding(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 message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; static int currentColorIndex = 0; switch (message) { case WM_CREATE: SetTimer(hwnd, 1, 10, NULL); return 0; case WM_TIMER: // 更新正方形位置 square1X += dx1; square1Y += dy1; square2X += dx2; square2Y += dy2; // 边界检测 GetClientRect(hwnd, &rect); if (square1X < 0 || square1X + SQUARE_SIZE1 > rect.right) dx1 = -dx1; if (square1Y < 0 || square1Y + SQUARE_SIZE1 > rect.bottom) dy1 = -dy1; if (square2X < 0 || square2X + SQUARE_SIZE2 > rect.right) dx2 = -dx2; if (square2Y < 0 || square2Y + SQUARE_SIZE2 > rect.bottom) dy2 = -dy2; // 检测碰撞 if (isColliding(square1X, square1Y, SQUARE_SIZE1, square2X, square2Y, SQUARE_SIZE2)) { collisionCount++; PostMessage(hwnd, WM_COLLISION, 0, 0); dx1 = -dx1; dy1 = -dy1; dx2 = -dx2; dy2 = -dy2; } // 更新圆的位置 for (Circle& 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); return 0; case WM_COLLISION: if (collisionCount <= 120) { // 产生新的圆 Circle newCircle; newCircle.x = (square1X + square2X) / 2; newCircle.y = (square1Y + square2Y) / 2; newCircle.dx = std::rand() % 5 - 2; newCircle.dy = std::rand() % 5 - 2; circles.push_back(newCircle); } if (collisionCount >= 100) { currentColorIndex = (currentColorIndex + 1) % 7; } return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); // 绘制正方形 COLORREF squareColor; if (collisionCount >= 100) { squareColor = colors[currentColorIndex]; } else { squareColor = RGB(128, 128, 128); } HBRUSH hBrush = CreateSolidBrush(squareColor); SelectObject(hdc, hBrush); Rectangle(hdc, square1X, square1Y, square1X + SQUARE_SIZE1, square1Y + SQUARE_SIZE1); Rectangle(hdc, square2X, square2Y, square2X + SQUARE_SIZE2, square2Y + SQUARE_SIZE2); DeleteObject(hBrush); // 绘制圆 hBrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, hBrush); for (const Circle& circle : circles) { Ellipse(hdc, circle.x - circle.radius, circle.y - circle.radius, circle.x + circle.radius, circle.y + circle.radius); } DeleteObject(hBrush); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: KillTimer(hwnd, 1); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 代码说明: 1. 全局变量:定义了两个正方形的大小、位置和速度,以及碰撞计数和圆的向量。 2. 碰撞检测:`isColliding` 函数用于检测两个正方形是否发生碰撞。 3. 定时器:使用 `SetTimer` 函数创建一个定时器,每 10 毫秒更新一次图形的位置。 4. 碰撞处理:当检测到碰撞时,增加碰撞计数,改变正方形的运动方向,并在碰撞次数小于等于 120 时产生一个新的圆。 5. 颜色变化:当碰撞次数达到 100 次时,正方形开始按指定顺序变色。 6. 绘制图形:在 `WM_PAINT` 消息处理中,绘制正方形和圆。 编译和运行: 将上述代码保存为一个 `.cpp` 文件,使用 Visual Studio 或其他 C++ 编译器进行编译和运行。运行程序后,你将看到两个正方形自动碰撞,每次碰撞产生一个圆,圆会随机移动。当碰撞达到 100 次时,正方形开始变色,到达 120 次后碰撞不再产生圆。 [2026-01-23 10:41:34 | AI问答 | 2193点数解答]

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