用c语言写新年烟花代码
时间: 2023-09-10 13:15:07 浏览: 67
好的,这是一个简单的C语言代码,用于绘制新年烟花效果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 80
#define HEIGHT 25
void clear_screen() {
COORD cursor_pos = {0, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursor_pos);
}
void draw_frame() {
printf("+");
for (int i = 0; i < WIDTH - 2; i++) {
printf("-");
}
printf("+\n");
for (int i = 0; i < HEIGHT - 2; i++) {
printf("|");
for (int j = 0; j < WIDTH - 2; j++) {
printf(" ");
}
printf("|\n");
}
printf("+");
for (int i = 0; i < WIDTH - 2; i++) {
printf("-");
}
printf("+\n");
}
void draw_firework(int x, int y) {
int colors[] = {FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE, FOREGROUND_INTENSITY};
int size = sizeof(colors) / sizeof(int);
for (int i = 0; i < 20; i++) {
int color_idx = rand() % size;
int color = colors[color_idx];
COORD cursor_pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursor_pos);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
printf("*");
int dx = (rand() % 3) - 1;
int dy = (rand() % 3) - 1;
x += dx;
y += dy;
Sleep(50);
}
}
int main() {
srand(time(NULL));
clear_screen();
draw_frame();
while (1) {
int x = rand() % (WIDTH - 2) + 1;
int y = rand() % (HEIGHT - 2) + 1;
draw_firework(x, y);
}
return 0;
}
```
这个程序会在控制台中绘制一个边框,然后不断地在边框内随机生成烟花,并让它们爆炸。每个烟花由20个星号组成,每个星号的颜色随机。每个烟花会在随机的方向上移动,并且每次移动后会暂停一段时间,以模拟烟花爆炸的效果。