酷代码 AI
菜单

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

要实现你所描述的功能,我们可以使用C++结合OpenGL库来完成图形的绘制和碰撞检测。以下是一个完整的示例代码: ```cpp #include <GL/glut.h> #include <iostream> #include <vector> #include <cstdlib> #include <ctime> // 定义颜色数组 const float colors[7][3] = { {1.0, 0.0, 0.0}, // 红 {1.0, 0.5, 0.0}, // 橙 {1.0, 1.0, 0.0}, // 黄 {0.0, 1.0, 0.0}, // 绿 {0.0, 1.0, 1.0}, // 青 {0.0, 0.0, 1.0}, // 蓝 {0.5, 0.0, 0.5} // 紫 }; // 定义正方形结构体 struct Square { float x, y; float size; float r, g, b; }; // 定义圆结构体 struct Circle { float x, y; float radius; float dx, dy; }; // 全局变量 Square smallSquare = {100, 100, 50, 0.5, 0.5, 0.5}; Square largeSquare = {300, 300, 500, 0.5, 0.5, 0.5}; std::vector<Circle> circles; int collisionCount = 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 drawSquare(const Square& s) { glColor3f(s.r, s.g, s.b); glBegin(GL_QUADS); glVertex2f(s.x, s.y); glVertex2f(s.x + s.size, s.y); glVertex2f(s.x + s.size, s.y + s.size); glVertex2f(s.x, s.y + s.size); glEnd(); } // 绘制圆 void drawCircle(const Circle& c) { glColor3f(1.0, 1.0, 1.0); glBegin(GL_TRIANGLE_FAN); glVertex2f(c.x, c.y); for (int i = 0; i <= 360; i++) { float angle = i * 3.1415926 / 180.0; float x = c.x + c.radius * cos(angle); float y = c.y + c.radius * sin(angle); glVertex2f(x, y); } glEnd(); } // 移动圆 void moveCircles() { for (auto& c : circles) { c.x += c.dx; c.y += c.dy; // 边界检测 if (c.x - c.radius < 0 || c.x + c.radius > 800) { c.dx = -c.dx; } if (c.y - c.radius < 0 || c.y + c.radius > 600) { c.dy = -c.dy; } } } // 显示回调函数 void display() { glClear(GL_COLOR_BUFFER_BIT); // 检测碰撞 if (isCollision(smallSquare, largeSquare)) { collisionCount++; if (collisionCount <= 100) { Circle newCircle; newCircle.x = (smallSquare.x + largeSquare.x) / 2; newCircle.y = (smallSquare.y + largeSquare.y) / 2; newCircle.radius = 10; newCircle.dx = (rand() % 10 - 5) / 2.0; newCircle.dy = (rand() % 10 - 5) / 2.0; circles.push_back(newCircle); } } // 改变正方形颜色 if (collisionCount >= 100) { int colorIndex = (collisionCount - 100) % 7; smallSquare.r = colors[colorIndex][0]; smallSquare.g = colors[colorIndex][1]; smallSquare.b = colors[colorIndex][2]; largeSquare.r = colors[colorIndex][0]; largeSquare.g = colors[colorIndex][1]; largeSquare.b = colors[colorIndex][2]; } // 绘制正方形 drawSquare(smallSquare); drawSquare(largeSquare); // 绘制圆 for (const auto& c : circles) { drawCircle(c); } // 移动圆 moveCircles(); glutSwapBuffers(); } // 定时器回调函数 void timer(int value) { glutPostRedisplay(); glutTimerFunc(16, timer, 0); } // 初始化OpenGL void init() { glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 800, 0, 600); srand(time(NULL)); } // 主函数 int main(int argc, char argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(800, 600); glutCreateWindow("Collision Detection"); init(); glutDisplayFunc(display); glutTimerFunc(0, timer, 0); glutMainLoop(); return 0; } ``` 代码说明: 1. 结构体定义:定义了`Square`和`Circle`结构体,分别用于表示正方形和圆。 2. 碰撞检测:`isCollision`函数用于检测两个正方形是否发生碰撞。 3. 绘制函数:`drawSquare`和`drawCircle`函数分别用于绘制正方形和圆。 4. 移动圆:`moveCircles`函数用于移动圆,并进行边界检测。 5. 显示回调函数:`display`函数负责绘制图形、检测碰撞、改变正方形颜色和移动圆。 6. 定时器回调函数:`timer`函数用于定时刷新屏幕。 7. 初始化函数:`init`函数用于初始化OpenGL环境。 8. 主函数:`main`函数负责初始化GLUT、创建窗口、设置回调函数并进入主循环。 编译和运行: 要编译和运行这个程序,你需要安装OpenGL和GLUT库。在Linux系统上,可以使用以下命令编译: ```sh g++ -o collision collision.cpp -lGL -lGLU -lglut ``` 然后运行生成的可执行文件: ```sh ./collision ``` 在Windows系统上,你可以使用MinGW或Visual Studio等开发环境进行编译和运行。 [2026-01-23 10:38:38 | AI问答 | 1743点数解答]

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