[C++] 纯文本查看 复制代码 #include <graphics.h> //包含EGE的头文件
#include <time.h> //包含时间、随机数的头文件
#include <bits/stdc++.h>
using namespace std;
int n,count1=1,count2=0;
float t=0;
int height=600,width=800;
float rnx(int min,int max) //随机数函数
{
float t;
t=rand()%(max-min)+min;
return t;
}
struct ballon
{
int r;
double g,y,x,vy,vx;
COLORS color;
int health;
int kz;
}ball[1000];
void is_ill() //判断得病函数
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(ball[i].health+ball[j].health==1)
{
if(abs(ball[i].x-ball[j].x)+abs(ball[i].y-ball[j].y)<=ball[i].r+ball[j].r) //距离够近
{
t=rnx(0,100);
if(ball[i].kz==1)
{
if(t<=90) //不戴口罩
{
ball[i].health=0;
ball[j].health=0;
count1++;
}
}
if(ball[i].kz==2)
{
if(t<=30) //戴口罩
{
ball[i].health=0;
ball[j].health=0;
count1++;
}
}
if(ball[i].kz==3)
{
if(t<=5) //戴N95
{
ball[i].health=0;
ball[j].health=0;
count1++;
}
}
}
}
}
}
}
void csh() //初始化函数
{
cout<<"请输入人数<小于1000>:";
cin>>n;
initgraph(width, height); //初始化图形界面
setcolor(BLACK); //设置画图线为黑色
setbkcolor(YELLOW); //设置背景颜色为黄色
srand(time(0)); //随机种子函数
setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
for(int i=0;i<n;i++){
ball[i].x = rnx(ball[i].r,width-ball[i].r); //设置x
ball[i].y = rnx(ball[i].r,height-ball[i].r); //设置y
ball[i].vx = rnx(3,5); //设置x的速度
ball[i].vy = rnx(3,5); //设置y的速度
ball[i].r = 10+rand()%10; //设置半径
ball[i].health=1; //健康
ball[i].kz=rnx(1,3); //口罩类型
}
}
void draw_ball() //画球函数
{
ball[0].health=0; //0号球不健康
cleardevice(); //清屏
for(int t=0;t<=n;t++)
{
if(ball[t].health==0)
{
ball[t].color=GREEN; //被感染
}
if(ball[t].health==1)
{
ball[t].color=WHITE; //未感染
}
ball[t].vy+=ball[t].g;
ball[t].x+=ball[t].vx;
ball[t].y+=ball[t].vy;
if(ball[t].y>=height-ball[t].r)
{
ball[t].vy=-ball[t].vy;
ball[t].y=height-ball[t].r;
}
if(ball[t].y<=ball[t].r)
{
ball[t].vy=-ball[t].vy;
}
if(ball[t].x>=width-ball[t].r)
{
ball[t].vx=-ball[t].vx;
ball[t].x=width-ball[t].r;
}
if(ball[t].x<=ball[t].r)
{
ball[t].vx=-ball[t].vx;
}
setfillcolor(ball[t].color);
fillellipse(ball[t].x, ball[t].y, ball[t].r, ball[t].r); //画球
}
xyprintf(20,20,"病人数量:%d",count1); //格式化输出文字
xyprintf(20,60,"耗时:%d",count2);
Sleep(10);
count2+=10;
}
int main()
{
csh();
while(1)
{
is_ill();
draw_ball();
if(count1==n) break; //如果全感染,停止
}
getch();
} |