中国网海峡频道:模拟烟花的程序,运行总出错,请c语言大师指点!!!

来源:百度文库 编辑:高校问答 时间:2024/05/03 05:21:33
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
struct star
{
double x;
double y;
double dx;
double dy;
double speed;
int color;
double r;
int IsMainStar;
struct star *next;
}*head=NULL;

int StarN=0;
InitGra()
{
int dr=DETECT,mode=0;
initgraph(&dr,&mode,"d:\\turboc2");
cleardevice();
}
void Push(struct star *new)
{
new->next=head;
head=new;
}

Delete(struct star *pre,struct star *now)
{
setcolor(0);
setfillstyle(1,0);
fillellipse((int )now->x,(int)now->y,now->r,now->r);
if(now == head )
head=now->next;
else
pre->next=now->next;

free(now);
StarN--;

}
void Clear()
{
struct star *now,*next;

now=head;
StarN--;
while(now!=NULL)
{ next=now->next;
free(now);
now=next;
}

}

void CreateMainStar()
{ struct star *new;

new=malloc(sizeof(struct star));
if (new==NULL)
{return;}
new->dx=random(400)+100;
new->dy=random(200)+180;
new->x=new->dx;
new->y=480;
new->speed=random(150)/100.0;
new->color=random(15)+1;
new->r=random(2)+2;
new->IsMainStar=1;

Push(new);
StarN++;

}
int CreateStars(double x,double y,int color)
{
struct star *new;
int n,i,r,cr,a;

n =random(10)+30;

cr=random(10)+30;
for(i=0;i<n;i++)
{a=random(360)+5;
r=random(-30)+cr;
new=malloc(sizeof(struct star ));
if (new==NULL)
{printf("NO MMMMMM!");
return 0;
};
new->dx=cos((a/360.0)*2*3.1415926)*r+x;
new->dy=sin((a/360.0)*2*3.1415926)*r+y;
new->x=x;
new->y=y;
new->speed=random(300)/100.0;
new->color=color;
new->r=random(2)+1;
new->IsMainStar=0;

Push(new);

StarN++;
}

}

void MoveStars()
{ struct star *pre;
struct star *now;

double tx,ty,ts;

for(pre=now=head;now!=NULL;pre=now,now=now->next)
{
tx=now->dx-now->x;
ty=now->dy-now->y;
if((tx < 2 && tx > -2 ) && (ty < 2 && ty > -2 ))
{ if(now->IsMainStar)
{

CreateStars(now->x,now->y,now->color);

}

Delete(pre,now);

continue;

}

setcolor(0);
setfillstyle(1,0);
fillellipse((int )now->x,(int)now->y,now->r,now->r);

ts=sqrt(tx*tx+ty*ty);

if(fabs(ts)<0.1) continue;

now->x+=(tx/ts)*now->speed;
now->y+=(ty/ts)*now->speed;

setcolor(now->color);
setfillstyle(1,now->color);
fillellipse((int )now->x,(int)now->y,now->r,now->r);
}

}

void main()
{
InitGra();
randomize();
CreateMainStar();
CreateMainStar();
while(!kbhit())
{
if(random(10220)%127 == 0 ||StarN<=2)

CreateMainStar();

MoveStars();
MoveStars();

}
Clear();
closegraph();
}
initgraph(&dr,&mode,"d:\\turboc2");
d:\\turboc2要根据tc2.0所在实际路径设计
如c:\\turboc2

选项->目录->输出目录->不要设置和initgraph(&dr,&mode,"d:\\turboc2"); 一样就可以正常执行.

Options->Directories->Output Directory->不要设置和initgraph(&dr,&mode,"d:\\turboc2"); 一样就可以正常执行.

强!为什莫我的tc不能执行graphics的类似的图形输出程序。请相关高手指点。