[C] 纯文本查看 复制代码 #include <graphics.h>
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int high=600,width=800,num,count1,time_count1;
struct ballon
{
float x;
float y;
int r;
float vx;
float vy;
float g;
unsigned int R_color,G_color,B_color;
COLORS color;
int health;
}ball[1000];
float ranx(int min,int max)
{
float h;
h=rand()%(max-min)+min;
return h;
}
void is_ill()
{
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
if(ball[i].health!=ball[j].health)
{
if((abs(ball[i].x-ball[j].x)<=ball[i].r+ball[j].r)&&
(abs(ball[i].y-ball[j].y)<=ball[i].r+ball[j].r))
{
float l=ranx(0,100);
if(l<30)
{
ball[i].health=0;
ball[j].health=0;
count1++;
}
}
}
}
}
}
void startup()
{
srand(time(0));//随机种子函数
for(int i =0;i<num;i++)
{
ball[i].x = ranx(ball[i].r+1,width-ball[i].r-1);
ball[i].y = ranx(ball[i].r+1,high-ball[i].r-1);
ball[i].vx = ranx(-2,3);
ball[i].vy = ranx(-2,5);
ball[i].r = 10+rand()%10;
ball[i].R_color = ranx(10,255);
ball[i].G_color = ranx(10,255);
ball[i].B_color = ranx(10,255);
ball[i].color=GREEN;
ball[i].health=1;
}
ball[0].color=RED;
ball[0].health=0;
count1=1;
initgraph(800, 600);
setcolor(BLACK);
setbkcolor(WHITE);
setfillcolor(GREEN);
setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
}
void update()
{
for(int i=0;i<num;i++)
{
ball[i].vy=ball[i].vy+ball[i].g;
ball[i].vx=ball[i].vx+ball[i].g;
ball[i].y+=ball[i].vy;
ball[i].x+=ball[i].vx;
if(ball[i].y>=high-ball[i].r)
{
ball[i].y=high-ball[i].r;
ball[i].vy=-ball[i].vy;
}
if(ball[i].y<=ball[i].r)
{
ball[i].y=ball[i].r;
ball[i].vy=-ball[i].vy;
}
if(ball[i].x<=ball[i].r)
{
ball[i].x=ball[i].r;
ball[i].vx=-ball[i].vx;
}
if(ball[i].x>=width-ball[i].r)
{
ball[i].x=width-ball[i].r;
ball[i].vx=-ball[i].vx;
}
}
}
void drawball()
{
for(int i=0;i<num;i++)
{
if(ball[i].health==0)
{
ball[i].color=RED;
}
setfillcolor(ball[i].color);
fillellipse(ball[i].x,ball[i].y,ball[i].r,ball[i].r);
}
xyprintf(20,20,"病人数量:%d",count1);
xyprintf(20,60,"计时:%d", time_count1);//格式化输出文字
Sleep(10);
time_count1+=10;
cleardevice();
}
int main()
{
cout<<"请输入个数:";
cin>>num;
startup();
while(1)
{
is_ill();
update();
drawball();
if(count1==num)
{
break;
}
}
getch();
closegraph();
return 0;
} |