[C++] 纯文本查看 复制代码 #include <graphics.h>
#include<time.h> //包含随机数,时间函数
int high=600,width=800;
struct ballon
{
float x;
float y;
int r;
float vx;
float vy;
float g;
unsigned int R_color,G_color,B_color;
}ball[1000000];
float ranx(int min,int max)
{
float h;
h=rand()%(max-min+min);
return h;
}
void startup()
{
srand(time(0));//随机种子函数
for(int i =0;i<100;i++)
{
ball[i].x = ranx(ball[i].r+1,width-ball[i].r-1);
ball[i].y = ranx(ball[i].r+1,high-ball[i].r-1);
ball[i].vx = ranx(2,3);
ball[i].vy = ranx(2,5);
ball[i].r = 10+rand()%10;
ball[i].R_color = ranx(10,255);
ball[i].G_color = ranx(10,255);
ball[i].B_color = ranx(10,255);
}
initgraph(800, 600);
setcolor(WHITE);
setbkcolor(WHITE);
setfillcolor(YELLOW);
}
void update()
{
for(int i=0;i<100;i++)
{
ball[i].vy=ball[i].vy+ball[i].g;
ball[i].vx=ball[i].vx+ball[i].g;
ball[i].y+=ball[i].vy;
ball[i].x+=ball[i].vx;
if(ball[i].y>=high-ball[i].r)
{
ball[i].vy=-ball[i].vy;
}
if(ball[i].y<=ball[i].r)
{
ball[i].vy=-ball[i].vy;
}
if((ball[i].x<=ball[i].r)||(ball[i].x>=width-ball[i].r))
{
ball[i].vx=-ball[i].vx;
}
}
}
void drawball()
{
for(int i=0;i<100;i++)
{
fillellipse(ball[i].x,ball[i].y,ball[i].r,ball[i].r);
setfillcolor(EGERGB(ball[i].R_color, ball[i].G_color, ball[i].B_color));
Sleep(5);
cleardevice();
}
int main()
{
getch();
closegraph();
return 0;
}
|