酷代码 AI
菜单

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

以下是一个使用Windows API实现你所描述功能的C++代码示例: ```cpp #include <windows.h> #include <iostream> #include <cstdlib> #include <ctime> // 全局变量 HWND hwnd; HDC hdc; RECT clientRect; // 正方形结构体 struct Square { int x, y; int width; int dx, dy; COLORREF color; }; // 圆形结构体 struct Circle { int x, y; int radius; int dx, dy; COLORREF color; }; // 全局变量 Square sq1 = {100, 100, 50, 2, 2, RGB(128, 128, 128)}; Square sq2 = {200, 200, 100, -2, -2, RGB(128, 128, 128)}; Circle circles[100]; int circleCount = 0; int collisionCount = 0; 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)}; int colorIndex = 0; // 检查两个正方形是否碰撞 bool isColliding(const Square& s1, const Square& s2) { return (s1.x < s2.x + s2.width && s1.x + s1.width > s2.x && s1.y < s2.y + s2.width && s1.y + s1.width > s2.y); } // 边界检测和反弹 void checkBoundaries(Square& s) { GetClientRect(hwnd, &clientRect); if (s.x <= 0 || s.x + s.width >= clientRect.right) { s.dx = -s.dx; } if (s.y <= 0 || s.y + s.width >= clientRect.bottom) { s.dy = -s.dy; } } // 移动圆形 void moveCircles() { for (int i = 0; i < circleCount; i++) { circles[i].x += circles[i].dx; circles[i].y += circles[i].dy; if (circles[i].x - circles[i].radius <= 0 || circles[i].x + circles[i].radius >= clientRect.right) { circles[i].dx = -circles[i].dx; } if (circles[i].y - circles[i].radius <= 0 || circles[i].y + circles[i].radius >= clientRect.bottom) { circles[i].dy = -circles[i].dy; } } } // 绘制图形 void draw() { hdc = GetDC(hwnd); // 清除 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0)); FillRect(hdc, &clientRect, hBrush); DeleteObject(hBrush); // 绘制正方形 hBrush = CreateSolidBrush(sq1.color); SelectObject(hdc, hBrush); Rectangle(hdc, sq1.x, sq1.y, sq1.x + sq1.width, sq1.y + sq1.width); DeleteObject(hBrush); hBrush = CreateSolidBrush(sq2.color); SelectObject(hdc, hBrush); Rectangle(hdc, sq2.x, sq2.y, sq2.x + sq2.width, sq2.y + sq2.width); DeleteObject(hBrush); // 绘制圆形 for (int i = 0; i < circleCount; i++) { hBrush = CreateSolidBrush(circles[i].color); SelectObject(hdc, hBrush); Ellipse(hdc, circles[i].x - circles[i].radius, circles[i].y - circles[i].radius, circles[i].x + circles[i].radius, circles[i].y + circles[i].radius); DeleteObject(hBrush); } ReleaseDC(hwnd, hdc); } // 主窗口过程 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: draw(); break; case WM_TIMER: // 移动正方形 sq1.x += sq1.dx; sq1.y += sq1.dy; sq2.x += sq2.dx; sq2.y += sq2.dy; // 边界检测和反弹 checkBoundaries(sq1); checkBoundaries(sq2); // 检查碰撞 if (isColliding(sq1, sq2)) { collisionCount++; sq1.dx = -sq1.dx; sq1.dy = -sq1.dy; sq2.dx = -sq2.dx; sq2.dy = -sq2.dy; if (collisionCount < 50) { circles[circleCount].x = (sq1.x + sq2.x) / 2; circles[circleCount].y = (sq1.y + sq2.y) / 2; circles[circleCount].radius = 10; circles[circleCount].dx = (rand() % 3) - 1; circles[circleCount].dy = (rand() % 3) - 1; circles[circleCount].color = RGB(255, 255, 255); circleCount++; } if (collisionCount >= 50) { sq1.color = colors[colorIndex % 7]; sq2.color = colors[colorIndex % 7]; colorIndex++; } } // 移动圆形 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, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = "MyWindowClass"; RegisterClass(&wc); // 创建窗口 hwnd = CreateWindow(wc.lpszClassName, "Collision Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 10, NULL); // 初始化随机数种子 srand(time(NULL)); // 消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } ``` 代码说明: 1. 结构体定义:定义了`Square`和`Circle`结构体,分别用于表示正方形和圆形。 2. 碰撞检测:`isColliding`函数用于检查两个正方形是否发生碰撞。 3. 边界检测:`checkBoundaries`函数用于检测正方形是否到达窗口边界,并使其反弹。 4. 图形移动:`moveCircles`函数用于移动圆形,并进行边界检测和反弹。 5. 绘制图形:`draw`函数用于绘制正方形和圆形。 6. 窗口过程:`WndProc`函数处理窗口消息,包括绘制、定时器事件和窗口销毁事件。 7. 主函数:`WinMain`函数是程序的入口,负责注册窗口类、创建窗口、显示窗口、设置定时器和消息循环。 注意事项: - 代码使用了Windows API,需要在Windows操作系统上编译和运行。 - 定时器的时间间隔为10毫秒,可以根据需要进行调整。 - 代码中使用了`rand`函数生成随机数,确保每次运行时圆形的移动方向不同。 [2026-01-23 15:25:04 | AI问答 | 2080点数解答]

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