生成c++烟花代码
时间: 2023-08-13 21:18:25 浏览: 300
好的,以下是一个简单的C++烟花代码,可以在控制台中运行:
```c++
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void gotoxy(int x, int y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void draw(int x, int y, char c, int color) {
setColor(color);
gotoxy(x, y);
cout << c;
}
int main() {
srand(time(NULL));
int screenWidth = 80;
int screenHeight = 40;
int numParticles = 100;
int gravity = 1;
int colors[] = {7, 9, 10, 11, 12, 13, 14};
while (true) {
// 初始化烟花粒子
int x = rand() % screenWidth;
int y = screenHeight - 1;
int vx = rand() % 6 - 3;
int vy = rand() % 6 - 10;
int color = colors[rand() % 7];
// 烟花爆炸
for (int i = 0; i < 15; i++) {
draw(x, y, '*', color);
x += vx;
y += vy;
vy += gravity;
Sleep(50);
}
// 烟花粒子消失
for (int i = 0; i < numParticles; i++) {
int px = x;
int py = y;
int pvx = rand() % 6 - 3;
int pvy = rand() % 6 - 3;
int pcolor = colors[rand() % 7];
for (int j = 0; j < 10; j++) {
draw(px, py, '.', pcolor);
px += pvx;
py += pvy;
pvy += gravity;
Sleep(50);
}
}
}
return 0;
}
```
这个代码会在控制台中不断产生烟花爆炸效果,每个烟花会分裂成多个粒子,最终逐渐消失。
阅读全文