用c++写烟花代码
时间: 2024-01-27 22:03:31 浏览: 125
C语言/C++ 烟花表白代码
好的,下面是一个简单的 C++ 烟花代码,希望能帮到你:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
void explode(int x, int y) {
int count = 0;
while (count < 50) {
int dx = rand() % 100 - 50;
int dy = rand() % 100 - 50;
int sleepTime = rand() % 50 + 10;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {x, y});
cout << "●";
Sleep(sleepTime);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {x, y});
cout << " ";
x += dx;
y += dy;
count++;
}
}
int main() {
srand(time(0));
while (true) {
int x = rand() % 80;
int y = rand() % 25;
int sleepTime = rand() % 1000 + 500;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {x, y});
cout << "●";
Sleep(sleepTime);
explode(x, y);
}
return 0;
}
```
这个程序会在控制台中不停地产生随机位置的烟花,当烟花到达顶点时,会爆炸成许多小火花。注意,这个程序需要在 Windows 系统中运行。
阅读全文