虽然循环没有在for语句中破坏

时间:2015-04-09 01:31:17

标签: java for-loop while-loop

我正在尝试编写一个球生成三圈的游戏,如果球越过屏幕底部,则转弯结束。

下面是我写的课程,我可以让球移动,但当它移过屏幕底部时,球不会像我预期的那样在屏幕中间再生。

道歉,如果这个问题很简陋,但我已经对此进行了一些研究并且似乎陷入困境。

private void initiateBall() {
        double bx = (WIDTH / 2) - BALL_RADIUS;
        double by = (HEIGHT / 2) - BALL_RADIUS;
        GOval ball = new GOval(bx, by,BALL_RADIUS, BALL_RADIUS);
        ball.setFilled(true);
        ball.setColor(Color.BLACK);
        vx = rgen.nextDouble(1.0, 1.2);
        if (rgen.nextBoolean(0.5)) vx = -vx;
        vy = 3.0;
        for (int i = 0; i <= NTURNS; i++) {
            add (ball);
            while (ball.getY() < HEIGHT && ball.getX() < HEIGHT) {
            ball.move(vx, vy);
            pause(DELAY);
        }
    }

1 个答案:

答案 0 :(得分:0)

private void initiateBall(){

   for (int k = 0; k < NTURNS; k ++) {
        double bx = (WIDTH / 2) - BALL_RADIUS;
        double by = (HEIGHT / 2) - BALL_RADIUS;
        GOval ball = new GOval(bx, by, BALL_RADIUS, BALL_RADIUS);
        ball.setFilled(true);
        ball.setColor(Color.BLACK);
        vx = rgen.nextDouble(1.0, 1.2);
        if (rgen.nextBoolean(0.5)){
            vx = -vx;
        }
        vy = 3.0;
       double x = bx;
       double y = by;
       add(ball, (x), y);
    while (ball.getY() < HEIGHT) {
    ball.move(vx,vy);
    pause(DELAY);
    }
  }
}