Discuz! Board

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

20231022

[复制链接]

11

主题

13

帖子

2万

积分

论坛元老

Rank: 8Rank: 8

积分
26397

袋龙 Lv:22
发表于 2023-10-22 17:05:18 | 显示全部楼层 |阅读模式
#include <graphics.h>//包含EGE的头文件
#include <time.h>//包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;

int sl;

int width =800;
int height =600;

struct ballon{
        int x;//x坐标
        int y;//y坐标
        int vx;//x向速度
        int vy;//y向速度
        int radius;//半径
        int health;//1代表健康,2代表不健康
}ball[1000];

int randx(int min,int max)
{
        int r =rand()%(max-min+1)+min;
        return r;       
}

void startup()//初始化 函数
{
    cin>>sl;

        srand(time(0));//随机种子函数
       
        for(int i =0;i<sl;i++)
        {
                ball[i].health = 1;
                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[0].health = 0;
               
        initgraph(width, height);                                //初始化图形界面
        setcolor(WHITE);        //设置画图线为白色
        setbkcolor(WHITE);//设置背景颜色为白色
        setfillcolor(GREEN);//设置填充颜色为绿色        
}

void update() //更新函数
{
   
        for(int i =0;i<sl;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<sl;i++)
        {
            if(ball[i].health==0)
                    setfillcolor(RED);//设置填充颜色为红色
                else
                    setfillcolor(GREEN);//设置填充颜色为绿色   
                fillellipse(ball[i].x, ball[i].y, ball[i].radius, ball[i].radius); //画一个实心圆
               
        }
        Sleep(10);
}

void is_ill()
{

    for(int i=0;i<sl;i++)
        for(int j=i+1;j<sl;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:15 , Processed in 0.060157 second(s), 24 queries .

Powered by Discuz! X3.4

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

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