请帮我写一个动态烟花的c语言代码
时间: 2023-08-28 21:22:00 浏览: 281
好的,以下是一个简单的动态烟花的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <unistd.h>
#include <time.h>
const int WIDTH = 800;
const int HEIGHT = 600;
void drawFireworks(int x, int y, int color) {
setcolor(color);
for (int i = 0; i < 50; i++) {
int radius = rand() % 20 + 10;
int dx = rand() % 100 - 50;
int dy = rand() % 100 - 50;
circle(x + dx, y + dy, radius);
}
}
int main() {
initwindow(WIDTH, HEIGHT);
srand(time(NULL));
while (1) {
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
int color = rand() % 15 + 1;
drawFireworks(x, y, color);
delay(1000);
cleardevice();
}
closegraph();
return 0;
}
```
这个代码使用了 `graphics.h` 库来绘制图形,需要安装 `graphics.h` 库并在编译时链接 `-lgraph`。
这个代码的原理是不断随机生成烟花的位置和颜色,然后在屏幕上绘制出来,每隔一段时间清除屏幕重新绘制。