Discuz! Board

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

11.10 小球

[复制链接]

54

主题

66

帖子

574

积分

高级会员

Rank: 4

积分
574
发表于 2022-11-10 19:31:01 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <graphics.h>                 //包含EGE的头文件
#include <time.h>
#include <bits/stdc++.h>
using namespace std;
int n,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;
}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)<=ball[i].r+ball[j].r)
				{
					if(abs(ball[i].y-ball[j].y)<=ball[i].r+ball[j].r)
					{
						t=rnx(0,100);
						if(t<=30)
						{
							ball[j].health=0;
						}
					}
				}
			}
		} 
	}
}
void csh()
{
	cout<<"请输入人数<小于1000>:";
	cin>>n;
	initgraph(width, height);               //初始化图形界面
	setcolor(BLACK);  //设置画图线为黑色   
	setbkcolor(YELLOW);//设置背景颜色为黄色
	setfillcolor(RED);//设置填充颜色为红色 
    srand(time(0));//随机种子函数  
    for(int i=0;i<n;i++){
        ball[i].x = rnx(ball[i].r,width-ball[i].r);
        ball[i].y = rnx(ball[i].r,height-ball[i].r);
        ball[i].vx = rnx(3,5);
        ball[i].vy = rnx(3,5);
        ball[i].r = 10+rand()%10;  
        ball[i].health=1;
    }
}
void draw_ball()
{
	 	ball[0].health=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);
        }
        Sleep(10);
}
int main()
{
	csh();
	while(1)
	{
		is_ill();
	    draw_ball();
	}
}
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-7-18 07:45 , Processed in 0.059801 second(s), 27 queries .

Powered by Discuz! X3.4

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

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