用c语言写一份烟花代码

示例一:

c#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define WIDTH 80
#define HEIGHT 20

void wait(int ms)
{
clock_t start = clock();
while (clock() - start < ms);
}

void clear_screen()
{
system("cls");
}

void set_cursor(int x, int y)
{
printf("\033[%d;%dH", y, x);
}

void draw_firework(int x, int y)
{
int i, j;
set_cursor(x, y);
printf("*\n");
for (i = 1; i < 5; i++) {
set_cursor(x, y + i);
for (j = 0; j < i; j++) {
printf("*");
}
printf("\n");
}
for (i = 3; i > 0; i--) {
set_cursor(x, y + i);
for (j = 0; j < i; j++) {
printf("*");
}
printf("\n");
}
}

int main()
{
int i, j;
int x, y;
srand(time(NULL));
clear_screen();
while (1) {
x = rand() % WIDTH;
y = rand() % HEIGHT;
draw_firework(x, y);
wait(1000);
clear_screen();
}
return 0;
}

该代码将在控制台中显示一个简单的烟花效果。它使用随机数生成器来选择烟花的起始位置,并在该位置绘制一个简单的烟花图案。然后,它等待一秒钟,清除屏幕,并重复该过程。

示例二:

#include <graphics.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    srand(time(NULL));

    while (1) {
        int x = rand() % getmaxx();
        int y = rand() % getmaxy();
        int color = rand() % 16 + 1;
        int radius = rand() % 50 + 10;

        setcolor(color);
        setfillstyle(SOLID_FILL, color);
        fillellipse(x, y, radius, radius);

        delay(100);
    }

    closegraph();
    return 0;
}

这个例子使用了graphics.h库来绘制烟花,其中包括了一个无限循环,每次循环都会在随机位置绘制一个随机大小和颜色的圆形,然后延迟100毫秒。你可以根据自己的需要修改代码来实现更加复杂的效果。

  • 0
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hudie765

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值