酷代码 AI
菜单

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

以下是一个使用 Windows API 实现你所描述功能的 C++ 程序。该程序在黑色上显示一个 50 像素的正方形和一个 100 像素的正方形,它们会自动碰撞,每次碰撞产生一个圆,圆会随机移动。当碰撞达到 50 次时,两个正方形开始按特定顺序变色,到达 60 次后碰撞不再产生圆,但方形依然会变色。同时包含边界检测功能。 ```cpp #include <windows.h> #include <vector> #include <ctime> #include <cstdlib> // 窗口 const char g_szClassName[] = "myWindowClass"; // 定义颜色数组 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)}; // 定义全局变量 RECT square1 = {100, 100, 150, 150}; // 50 像素的正方形 RECT square2 = {300, 300, 400, 400}; // 100 像素的正方形 std::vector<RECT> circles; int collisionCount = 0; int colorIndex = 0; // 检测两个矩形是否碰撞 bool isCollision(const RECT& rect1, const RECT& rect2) { return!((rect1.right < rect2.left) || (rect1.left > rect2.right) || (rect1.bottom < rect2.top) || (rect1.top > rect2.bottom)); } // 绘制图形 void drawShapes(HDC hdc) { // 填充黑色 HBRUSH hbrBlack = CreateSolidBrush(RGB(0, 0, 0)); FillRect(hdc, &(RECT){0, 0, 800, 600}, hbrBlack); DeleteObject(hbrBlack); // 绘制正方形 HBRUSH hbrSquare; if (collisionCount >= 50) { hbrSquare = CreateSolidBrush(colors[colorIndex]); } else { hbrSquare = CreateSolidBrush(RGB(128, 128, 128)); } FillRect(hdc, &square1, hbrSquare); FillRect(hdc, &square2, hbrSquare); DeleteObject(hbrSquare); // 绘制圆形 HBRUSH hbrCircle = CreateSolidBrush(RGB(255, 255, 255)); for (const auto& circle : circles) { Ellipse(hdc, circle.left, circle.top, circle.right, circle.bottom); } DeleteObject(hbrCircle); } // 移动图形 void moveShapes() { // 移动正方形 square1.left += 1; square1.right += 1; square2.left -= 1; square2.right -= 1; // 边界检测 if (square1.right > 800) { square1.left = 0; square1.right = 50; } if (square2.left < 0) { square2.left = 700; square2.right = 800; } // 检测碰撞 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount < 60) { int x = rand() % 750; int y = rand() % 550; RECT circle = {x, y, x + 20, y + 20}; circles.push_back(circle); } if (collisionCount >= 50) { colorIndex = (colorIndex + 1) % 7; } } // 移动圆形 for (auto& circle : circles) { int dx = (rand() % 3) - 1; int dy = (rand() % 3) - 1; circle.left += dx; circle.top += dy; circle.right += dx; circle.bottom += dy; // 圆形边界检测 if (circle.left < 0) circle.left = 0; if (circle.top < 0) circle.top = 0; if (circle.right > 800) circle.right = 800; if (circle.bottom > 600) circle.bottom = 600; } } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); drawShapes(hdc); EndPaint(hwnd, &ps); break; } case WM_TIMER: { moveShapes(); 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) { WNDCLASSEX wc; HWND hwnd; MSG Msg; // 注册窗口类 wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wc)) { MessageBox(NULL, "窗口注册失败!", "错误", MB_ICONEXCLAMATION | MB_OK); return 0; } // 创建窗口 hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "图形碰撞与变色", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, "窗口创建失败!", "错误", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 10, NULL); // 消息循环 while (GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } ``` 代码说明: 1. 全局变量:定义了两个正方形的位置和大小、圆形数组、碰撞次数和颜色索引。 2. isCollision 函数:用于检测两个矩形是否碰撞。 3. drawShapes 函数:绘制黑色、正方形和圆形。 4. moveShapes 函数:移动正方形和圆形,进行边界检测,检测碰撞,并在碰撞时增加碰撞次数、生成圆形和改变正方形颜色。 5. WndProc 函数:处理窗口消息,包括绘制和定时器消息。 6. WinMain 函数:注册窗口类、创建窗口、设置定时器并进入消息循环。 注意事项: - 该程序使用了 Windows API,需要在 Windows 操作系统上编译和运行。 - 程序中的定时器间隔为 10 毫秒,可以根据需要调整。 [2026-01-23 15:18:13 | AI问答 | 2005点数解答]

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