[C++] 纯文本查看 复制代码 #include <graphics.h> //包含EGE的头文件
#include <time.h>
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;
unsigned int R_color,G_color,B_color;
}ball[10];
int height=600,width=800;
int main()
{
srand(time(0));//随机种子函数
for(int i=0;i<10;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].R_color = rnx(10,255);
ball[i].G_color = rnx(10,255);
ball[i].B_color = rnx(10,255);
}
initgraph(width, height); //初始化图形界面
setcolor(BLACK); //设置画图线为黑色
setbkcolor(YELLOW);//设置背景颜色为黄色
setfillcolor(RED);//设置填充颜色为红色
while(1)
{
cleardevice();
for(int t=0;t<=9;t++)
{
ball[t].vy+=ball[t].g;
//ball[t].vx+=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;
}
fillellipse(ball[t].x, ball[t].y, ball[t].r, ball[t].r);
setfillcolor(EGERGB(ball[t].R_color, ball[t].G_color, ball[t].B_color));
}
Sleep(10);
}
} |