编写一个c语言程序,烟花炸裂
时间: 2023-10-30 10:08:20 浏览: 46
下面是一个简单的C语言程序,实现了烟花炸裂的效果:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
void explode(int x, int y, int color);
int main(void)
{
int x, y, color;
srand(time(NULL));
while (1) {
x = rand() % 80; // 随机生成坐标和颜色
y = rand() % 24;
color = rand() % 15 + 1;
explode(x, y, color);
Sleep(50); // 等待一段时间再继续
}
return 0;
}
void explode(int x, int y, int color)
{
int i, j, k;
int radius = 0;
while (radius <= 10) { // 烟花半径从0到10
for (i = -radius; i <= radius; i++) {
for (j = -radius; j <= radius; j++) {
if (i * i + j * j <= radius * radius) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), (COORD){x + j, y + i});
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
putchar('*');
}
}
}
radius++; // 半径增加
Sleep(30); // 等待一段时间再更新
}
for (k = 0; k < 50; k++) { // 烟花消失
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), (COORD){x, y});
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
putchar(' ');
Sleep(20);
}
}
```
程序使用了Windows API来控制控制台的光标位置和文本颜色,需要在Windows系统中运行。程序随机生成烟花的位置和颜色,并且每个烟花都会以逐渐扩大的半径炸裂,然后逐渐消失。程序会不断生成新的烟花,整个过程看起来就像是在黑夜中观赏烟花秀。