c语言烟花代码简易
时间: 2023-08-04 09:04:34 浏览: 277
下面是一个简单的 C 语言烟花代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
void printColorful(char* str, char* color) {
printf("%s%s%s", color, str, COLOR_RESET);
}
void delay(int milliseconds) {
clock_t start_time = clock();
while (clock() < start_time + milliseconds);
}
int main() {
int i, j, k;
int height = 20;
int width = 60;
srand(time(NULL));
for (i = 0; i < 10; i++) {
int x = rand() % width;
int y = rand() % height;
int color = rand() % 6;
for (j = 0; j < 20; j++) {
for (k = 0; k < 60; k++) {
if (j == y && k == x) {
switch (color) {
case 0:
printColorful("*", COLOR_RED);
break;
case 1:
printColorful("*", COLOR_GREEN);
break;
case 2:
printColorful("*", COLOR_YELLOW);
break;
case 3:
printColorful("*", COLOR_BLUE);
break;
case 4:
printColorful("*", COLOR_MAGENTA);
break;
case 5:
printColorful("*", COLOR_CYAN);
break;
}
} else {
printf(" ");
}
}
printf("\n");
}
delay(500);
}
return 0;
}
```
这个代码会在终端中随机生成 10 个烟花,每个烟花会在一个随机位置爆炸,显示为一个有颜色的星号。每个烟花会持续 0.5 秒钟,然后消失。你可以根据需要修改烟花的数量、持续时间等参数。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)