我正在尝试编写一个程序,在窗口内连续弹回一个球来完成作业。这是我想要做的事情的简短youtube video。由于现在正在编写程序,球不会移动,我不明白为什么。
我有两个问题:
import acm.program.*;
import acm.graphics.*;
publc class BallBouncing extends GraphicsProgram{
public void run(){
GOval ball = new GOval(getWidth() / 2, getHeight() / 2, OVAL_SIZE, OVAL_SIZE);
ball.setFilled(true);
add(ball);
double dx = getWidth() / N_STEPS;
double dy = getHeight() / N_STEPS;
while(true){
ball.move(dx, dy);
pause(PAUSE_TIME);
if (ball.getY() > getHeight() - OVAL_SIZE) { dy = dy * -1;}
if (ball.getX() > getWidth() - OVAL_SIZE) { dx = dx * -1;}
if (ball.getY() < 0 + OVAL_SIZE) { dy = dy * -1;}
if (ball.getX() < 0 + OVAL_SIZE) { dx = dx * -1;}
}
}
private static final int STEPS = 1000;
private static final int PAUSE_TIME = 5;
private static final int OVAL_SIZE = 25;
}