C语言烟花代码
时间: 2023-07-23 16:11:01 浏览: 216
以下是一个基于C语言的烟花代码,可以模拟烟花的爆炸效果,并且逐渐消失,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#define PI 3.14159265358979323846
void gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void draw(int x, int y, int color) {
gotoxy(x, y);
printf("\033[1;%dm%c\033[0m", color, '*');
}
void clear(int x, int y) {
gotoxy(x, y);
printf(" ");
}
void explode(int x, int y, int color) {
int i, j, r = 1;
for (i = 0; i < 15; i++) {
for (j = 0; j < 360; j += 10) {
int px = x + r*cos(j*PI/180);
int py = y + r*sin(j*PI/180);
draw(px, py, color);
r++;
}
Sleep(50);
for (j = 0; j < 360; j += 10) {
int px = x + r*cos(j*PI/180);
int py = y + r*sin(j*PI/180);
clear(px, py);
r++;
}
}
}
int main() {
srand(time(NULL));
int x = rand()%80 + 1;
int y = rand()%20 + 1;
int color = rand()%8 + 1;
draw(x, y, color);
Sleep(1000);
clear(x, y);
explode(x, y, color);
return 0;
}
```
该代码使用Windows API函数实现控制台输出,并在屏幕上绘制烟花的爆炸效果。具体来说,我们使用gotoxy函数定位光标位置,使用printf函数输出字符,并使用ANSI颜色代码实现不同颜色的烟花。然后,我们使用explode函数模拟烟花的爆炸效果,通过绘制一系列圆形来模拟烟花的火花。请注意,这仅是一个简单的烟花代码,可以通过添加更多的参数和功能来改进它的效果。