按照代码,小球坐标ball_x和ball_y都应该发生改变,但从运行的结果来看,小球坐标只有ball_x发生改变,ball_y不变,这是为什么?(代码没错吧?)
代码:
#include<graphics.h>
#include<conio.h>
#define width 640
#define high 480
int main()
{
initgraph(width,high);
float ball_x,ball_y;
float ball_vx,ball_vy;
float radius;
ball_x=width/2;
ball_y=high/2;
ball_vx=1;
ball_vy=1;
radius=20;
while(1)
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
ball_x=ball_x+ball_vx;
ball_y=ball_y+ball_vy;
if((ball_x<=radius)||(ball_x>=width-radius))
ball_vx=-ball_vx;
if((ball_vy<=radius)||(ball_vy>=high-radius))
ball_vy=-ball_vy;
setcolor(RED);
setfillcolor(yellow);
fillcircle(ball_x,ball_y,radius);
Sleep(3);
}
closegraph();
return 0;
}
代码:
#include<graphics.h>
#include<conio.h>
#define width 640
#define high 480
int main()
{
initgraph(width,high);
float ball_x,ball_y;
float ball_vx,ball_vy;
float radius;
ball_x=width/2;
ball_y=high/2;
ball_vx=1;
ball_vy=1;
radius=20;
while(1)
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
ball_x=ball_x+ball_vx;
ball_y=ball_y+ball_vy;
if((ball_x<=radius)||(ball_x>=width-radius))
ball_vx=-ball_vx;
if((ball_vy<=radius)||(ball_vy>=high-radius))
ball_vy=-ball_vy;
setcolor(RED);
setfillcolor(yellow);
fillcircle(ball_x,ball_y,radius);
Sleep(3);
}
closegraph();
return 0;
}