小弟刚刚入门,写了个 4 球碰撞,但是第四个球一直不能碰,求指出错误
如题,小弟刚刚入门,写了个4球碰撞,但是第四个球一直不能碰,求指出错误,代码如下:void mainloop()
{
int x = 0, y = 0, dx = 1, dy = 1, r = 30;
int x1=0,y1=getheight()/2,dx1=1,dy1=1;
int x2=getwidth(),y2=getheight()/2,dx2=-1,dy2=1;
int x3=getwidth()/2,y3=getheight(),dx3=1,dy3=-1;
int i;
double distance1_0,distance2_1,distance2_0,distance3_0;
for ( ; is_run();delay_ms(10))
{
cleardevice();
distance1_0=sqrt((double)(abs((x-x1)*(x-x1)+(y-y1)*(y-y1))));
distance2_1=sqrt((double)(abs((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))));
distance2_0=sqrt((double)(abs((x2-x)*(x2-x)+(y2-y)*(y2-y))));
distance3_0=sqrt((double)(abs((x-x3)*(x-x3)+(y-y3)*(y-y3))));
if(distance1_0<=2*r)
{
dx=-dx;
dy=-dy;
dx1=-dx1;
dy1=-dy1;
}
if(distance2_0<=2*r)
{
dx2=-dx2;
dy2=-dy2;
dx=-dx;
dy=-dy;
}
if(distance2_1<=2*r)
{
dx2=-dx2;
dy2=-dy2;
dx1=-dx1;
dy1=-dy1;
}
if(distance3_0<=2*r)
{
dx3=-dx3;
dy2=-dy3;
dx=-dx;
dy=-dy;
}
{
x += dx;
y += dy;
if (x < r) dx = 1;
if (y < r) dy = 1;
if (x >= ege::getwidth() - r) dx = -1;
if (y >= ege::getheight() - r) dy = -1;
setcolor(GREEN);
setfillcolor(RED);
fillellipse(x, y, r, r);
x1 += dx1;
y1 += dy1;
if (x1 < r) dx1 = 1;
if (y1 < r) dy1 = 1;
if (x1 >= ege::getwidth() - r) dx1 = -1;
if (y1 >= ege::getheight() - r) dy1 = -1;
setcolor(BLUE);
setfillcolor(YELLOW);
fillellipse(x1, y1, r, r);
x2 += dx2;
y2 += dy2;
if (x2 < r) dx2 = 1;
if (y2 < r) dy2 = 1;
if (x2 >= ege::getwidth() - r) dx2 = -1;
if (y2 >= ege::getheight() - r) dy2 = -1;
setcolor(RED);
setfillcolor(BLUE);
fillellipse(x2, y2, r, r);
x3 += dx3;
y3 += dy3;
if (x3 < r) dx3 = 1;
if (y3 < r) dy3 = 1;
if (x3 >= ege::getwidth() - r) dx3 = -1;
if (y3 >= ege::getheight() - r) dy3 = -1;
setcolor(WHITE);
setfillcolor(BLUE);
fillellipse(x3, y3, r, r);
}
}
}
int main(void)
{
setinitmode(INIT_ANIMATION);
initgraph(640, 480);
randomize();
mainloop();
closegraph();
return 0;
}