Discuz! Board

 找回密码
 立即注册
查看: 651|回复: 0

20231022

[复制链接]

6

主题

6

帖子

1838

积分

金牌会员

Rank: 6Rank: 6

积分
1838
发表于 2023-10-22 17:04:25 | 显示全部楼层 |阅读模式
#include <graphics.h>//包含EGE的头文件
#include <time.h>//包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int width =800;
int height =600;
int n;
struct ballon{
        int x;//x坐标
        int y;//y坐标
        int vx;//x向速度
        int vy;//y向速度
        int radius;//半径
        int health;//1代表健康,0代表不健康
}ball[1000];
int randx(int min,int max)
{
        int r =rand()%(max-min+1)+min;
        return r;       
}
void startup()//初始化 函数
{
    cin>>n;
   
        srand(time(0));//随机种子函数
        for(int i =0;i<n;i++)
        {
                ball[i].x = randx(30,width-30);
                ball[i].y = randx(30,height-30);
                ball[i].vx = randx(-3,3);
                ball[i].vy = randx(2,5);
                ball[i].radius = randx(10,20);       
                ball[i].health = 1;
        }
        ball[0].health=0;       
        initgraph(width, height);                                //初始化图形界面
        setcolor(BLACK);        //设置画图线为红色
        setbkcolor(WHITE);//设置背景颜色为白色
}
void update() //更新函数
{
        for(int i =0;i<n;i++)
        {
       
                ball[i].x += ball[i].vx;
                ball[i].y += ball[i].vy;
               
                if(ball[i].x<=ball[i].radius || ball[i].x>=width-ball[i].radius)
                        ball[i].vx = -ball[i].vx;
                if(ball[i].y<=ball[i].radius || ball[i].y>=height-ball[i].radius)
                        ball[i].vy = -ball[i].vy;
        }
}
void drawball() //绘制小球
{
        cleardevice();
        for(int i =0;i<n;i++)
        {
                if(ball[i].health==1)
            {
                        setfillcolor(GREEN);//设置填充颜色为红色
        }
        else
        {
                setfillcolor(RED);       
                }
                fillellipse(ball[i].x, ball[i].y, ball[i].radius, ball[i].radius); //画一个实心圆
    }
        Sleep(10);
}
void is_ill()
{
        for(int i=0;i<n;i++)
        {
        for(int j=i+1;j<n;j++)
        {
                if(ball[i].health+ball[j].health==1)
                {
                        int x=abs(ball[i].x-ball[j].x);
                        int y=abs(ball[i].y-ball[j].y);
                        int s=sqrt(pow(x,2)+pow(y,2));
                        if(s<=(ball[i].radius+ball[j].radius))
                        {
                                ball[i].health = 0;//得病
                                ball[j].health = 0;
                        }
                }       
        }
    }
       
}

int main()
{


        startup();//初始化
       
        while(1)
        {
                update(); //更新数据
                drawball();//绘制小球
                is_ill();
        }
       
        return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-5-29 22:19 , Processed in 0.061383 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表