[C++] 纯文本查看 复制代码 #include<bits/stdc++.h>
#include <graphics.h> //包含EGE的头文件
#include<time.h>
using namespace std;
int width=800;
int hight=600;
struct ballon
{
int r;
float vx;
float vy;
float x;
float y;
float g;
unsigned int R_color,G_color,B_color;
}ball[10000];
float inx(int min,int max)
{
float t=rand()%(max-min)+min;
return t;
}
int main()
{
srand(time(0));//随机种子函数
for(int i =0;i<10000;i++)
{
ball[i].x = inx(ball[i].r*2+1,width-ball[i].r);
ball[i].y = inx(ball[i].r*2+1,hight-ball[i].r);
ball[i].vx = rand()%3;
ball[i].vy = rand()%5;
ball[i].r = 10+rand()%10;
ball[i].g=0;
ball[i].R_color = inx(10,255);
ball[i].G_color = inx(10,255);
ball[i].B_color = inx(10,255);
}
initgraph(width, hight); //初始化图形界面
setcolor(RED); //设置画图线为红色
setbkcolor(WHITE);//设置背景颜色为白色
setfillcolor(RED);//设置填充颜色为红色
while(1)
{
for(int i=0;i<10000;i++)
{
ball[i].vy=ball[i].vy+ball[i].g;
ball[i].y=ball[i].vy+ball[i].y;
ball[i].vx=ball[i].vx+ball[i].g;
ball[i].x=ball[i].vx+ball[i].x;
if(ball[i].y>=hight-ball[i].r||ball[i].y<=ball[i].r)
{
//ball[i].y=hight-ball[i].r;
ball[i].vy=-ball[i].vy;
}
if(ball[i].x>=width-ball[i].r||ball[i].x<=ball[i].r)
{
//ball[i].x=width-ball[i].r;
ball[i].vx=-ball[i].vx;
}
fillellipse(ball[i].x, ball[i].y, ball[i].r, ball[i].r); //画一个实心圆
//floodfillsurface(400, 300, RED);
setfillcolor(EGERGB(ball[i].R_color, ball[i].G_color, ball[i].B_color));
}
Sleep(10);
cleardevice();
}
getch(); //暂停,等待键盘按键
closegraph(); //关闭图形界面
return 0;
}
|