还有几天就要过年了,所以送个烟花来庆祝这值得兴奋的节日!
代码:
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include <math.h>
#define STAR_LEN sizeof(struct s)
#define FIRE_LEN sizeof(struct f)
#define ESC 283
#define STAR_BASE_BLAST_SPEED 20
#define STAR_BASE_DENSITY 100
#define STAR_BASE_MIN_RANGE 20
#define STAR_BASE_MAX_RANGE 100
#define FIRE_COUNT 10
int cur_num_of_fire=0;
struct f
{
int x,y;
int color;
int type;
int height;
int movespeed;
int blastspeed;
int blastdensity;
int minrange;
int maxrange;
struct s *headstar;
struct f *next;
};
struct s
{
int x,y;
struct s *next;
};
struct s *insertStar(struct s *head,struct s *p)
{
p->next=head;
head=p;
return head;
}
void createStar(struct f *fire)
{
long i=0;
int a=fire->x,b=fire->y,x,y;
long r=fire->minrange;
struct s *pstar=NULL;
fire->headstar=NULL;
while(1)
{
x=a-r+random(2*r);
y=b-r+random(2*r);
if((long)(pow((x-a),2)+pow((y-b),2))<=r*r)
{
if(++i%fire->blastdensity==0) {r+=6;i=0;}
pstar=(struct s*)malloc(STAR_LEN);
pstar->x=x;pstar->y=y;
putpixel(x,y,fire->color);
fire->headstar=insertStar(fire->headstar,pstar);
delay(fire->blastspeed);
if(r>fire->maxrange) break;
}
}
}
void delStar(struct f *fire)
{
struct s *p=fire->headstar,*old;
while(p!=NULL)
{
putpixel(p->x,p->y,0);
delay(fire->blastspeed);
old=p;
free(p);
p=old->next;
}
}
struct f *insertFire(struct f *head,struct f *p)
{
p->next=head;
head=p;
return head;
}
struct f *createFire()
{
struct f *p;
p=(struct f*)malloc(FIRE_LEN);
cur_num_of_fire++;
p->x=40+random(500);
p->y=420+random(60);
p->color=2+random(6);
p->type=random(12);
p->height=p->y-(200+random(200));
p->movespeed=1;
p->blastspeed=STAR_BASE_BLAST_SPEED+random(120);
p->blastdensity=STAR_BASE_DENSITY+random(70);
p->minrange=STAR_BASE_MIN_RANGE+random(70);
p->maxrange=STAR_BASE_MAX_RANGE+random(70);
return p;
}
void drawBlack(int x,int y)
{
setcolor(0);
setfillstyle(1,0);
sector(x,y,40,135,5,10);
}
void drawFire(int x,int y,int color,int type)
{
setcolor(color);
setfillstyle(type,color+8);
sector(x,y,40,135,5,10);
}
struct f *delFire(struct f *head,struct f *del)
{
struct f *old;
struct f *p=head;
if(del==head)
{
old=head;
free(head);
head=old->next;
}
else
while(p)
{
if(p->next==del)
{
p->next=del->next;
free(del);
break;
}
p=p->next;
}
return head;
}
struct f *seekFire(struct f *head)
{
struct f *p=head,*old;
while(p)
{p->y-=p->movespeed;
if(p->y<=p->height)
{
cur_num_of_fire--;
drawBlack(p->x,p->y);
createStar(p);
delStar(p);
old=p->next;
head=delFire(head,p);
p=old;
}
else
{
drawBlack(p->x,p->y);
drawFire(p->x,p->y-p->movespeed,p->color,p->type);
p=p->next;
}}
return head;
}
void initGra()
{
int gdriver = DETECT, gmode, errorcode;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
}
int isExit()
{
if(bioskey(1))
if(bioskey(0)==ESC) return 1;
else return 0;
}
main()
{
struct f *head=NULL;
randomize();
initGra();
while(1)
{
while(cur_num_of_fire<FIRE_COUNT)
head=insertFire(head,createFire());
head=seekFire(head);
if(isExit())
return 0;
}
}