酷代码 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, square2; 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); } // 绘制图形 void drawShapes(HDC hdc) { // 绘制黑色 HBRUSH hBlackBrush = CreateSolidBrush(RGB(0, 0, 0)); SelectObject(hdc, hBlackBrush); Rectangle(hdc, 0, 0, GetSystemMetrics(_CXSCREEN), GetSystemMetrics(_CYSCREEN)); DeleteObject(hBlackBrush); // 绘制正方形 HBRUSH hSquareBrush = CreateSolidBrush(square1.color); SelectObject(hdc, hSquareBrush); Rectangle(hdc, square1.x, square1.y, square1.x + square1.size, square1.y + square1.size); Rectangle(hdc, square2.x, square2.y, square2.x + square2.size, square2.y + square2.size); DeleteObject(hSquareBrush); // 绘制圆 for (const auto& circle : circles) { HBRUSH hCircleBrush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, hCircleBrush); Ellipse(hdc, circle.x - circle.radius, circle.y - circle.radius, circle.x + circle.radius, circle.y + circle.radius); DeleteObject(hCircleBrush); } } // 更新图形位置 void updateShapes() { // 更新正方形位置 square1.x += square1.dx; square1.y += square1.dy; square2.x += square2.dx; square2.y += square2.dy; // 边界检测 if (square1.x < 0 || square1.x + square1.size > GetSystemMetrics(_CXSCREEN)) { square1.dx = -square1.dx; } if (square1.y < 0 || square1.y + square1.size > GetSystemMetrics(_CYSCREEN)) { square1.dy = -square1.dy; } if (square2.x < 0 || square2.x + square2.size > GetSystemMetrics(_CXSCREEN)) { square2.dx = -square2.dx; } if (square2.y < 0 || square2.y + square2.size > GetSystemMetrics(_CYSCREEN)) { square2.dy = -square2.dy; } // 检查碰撞 if (isCollision(square1, square2)) { collisionCount++; if (collisionCount >= 50) { colorIndex = (colorIndex + 1) % 7; square1.color = colors[colorIndex]; square2.color = colors[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); } // 反弹 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 > GetSystemMetrics(_CXSCREEN)) { circle.dx = -circle.dx; } if (circle.y - circle.radius < 0 || circle.y + circle.radius > GetSystemMetrics(_CYSCREEN)) { circle.dy = -circle.dy; } } } // 窗口过程函数 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: { updateShapes(); 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) { // 初始化随机数种子 srand(static_cast<unsigned int>(time(nullptr))); // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = L"CollisionApp"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, L"Collision Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); // 初始化正方形 square1.x = 100; square1.y = 100; square1.size = 50; square1.dx = 2; square1.dy = 2; square1.color = RGB(128, 128, 128); square2.x = 300; square2.y = 300; square2.size = 100; square2.dx = -2; square2.dy = -2; square2.color = RGB(128, 128, 128); // 显示窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 10, NULL); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return static_cast<int>(msg.wParam); } ``` 代码说明: 1. 结构体定义:定义了`Square`和`Circle`结构体,分别表示正方形和圆,包含位置、大小、速度和颜色等信息。 2. 碰撞检测:`isCollision`函数用于检查两个正方形是否碰撞。 3. 绘制图形:`drawShapes`函数用于绘制黑色、正方形和圆。 4. 更新图形位置:`updateShapes`函数用于更新正方形和圆的位置,处理边界检测和碰撞事件。 5. 窗口过程函数:`WndProc`函数处理窗口消息,包括绘制和定时器消息。 6. 主函数:`WinMain`函数是程序的入口点,负责注册窗口类、创建窗口、初始化正方形、设置定时器和进入消息循环。 注意事项: - 该程序使用Windows API,需要在Windows操作系统上编译和运行。 - 定时器的间隔为10毫秒,可以根据需要调整。 - 程序中使用了随机数生成器,每次运行的结果可能不同。 [2026-01-23 15:15:49 | AI问答 | 2212点数解答]

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