简体   繁体   中英

Java Bouncing Ball Wall

I'm trying to understand these code:

    /*
 * File: BouncingBallWalls.java
 * ---------------------
 * This is exercise 15 in chapter 4 of "The Art and Science of Java."
 * It requires me to write a program that makes an animated bouncing
 * ball on the screen, bouncing off the window edges.
 */

import java.awt.*;
import acm.program.*;
import acm.graphics.*;

public class BouncingBallWalls extends GraphicsProgram {
    public void run() {
        GOval ball = new GOval (getWidth()/2 - BALL_SIZE/2, getHeight()/2 - BALL_SIZE, BALL_SIZE, BALL_SIZE);               /* Centers the ball */
        ball.setFilled(true);
        ball.setFillColor(Color.BLUE);
        ball.setColor(Color.BLUE);
        add(ball);
        double dx = (getWidth()/N_STEPS);
        double dy = (getWidth()/N_STEPS);
        while(true) {
            ball.move(dx, dy);                                   /* Movement for the ball */
            pause(PAUSE_TIME);
            if (ball.getY() > getHeight() - BALL_SIZE) {         /* Each "if" statement reverses the direction of the ball on one */
                dy = dy * -1;                                                         /* axis if it hits a boundry */
            }
            if(ball.getX() > getWidth()- BALL_SIZE) {
                dx = dx * -1;
            }
            if(ball.getY() < 0) {
                dy = dy * -1;
            }
            if(ball.getX() < 0) {
                dx = dx * -1;
            }
        }
    }
    private static final double N_STEPS = 1000;
    private static final double PAUSE_TIME = 2;
    private static final double BALL_SIZE = 50.0;
}

I understand everything except the following, why do you divide your gameWidth by N_STEPS ? and what is N_STEPS ?

        double dx = (getWidth()/N_STEPS);
        double dy = (getWidth()/N_STEPS);

private static final double N_STEPS = 1000;

Reference: http://tenasclu.blogspot.co.uk/2012/12/first-few-days-of-learning-to-program.html

You divide screen size by N_STEPS to get dx and dy, which represent the distance you want the ball to move during each loop. N_STEPS defines the movement in terms of the available screen width so that the relative movement/speed is the same regardless of screen size.

Another way of looking at N_STEPS, is that it controls the smoothness and speed of the ball. It does so in a way that makes the smoothness and speed consistent regardless of screen size. A higher N_STEPS will result in smoother/slower ball movement per loop.

Note that your code:

double dx = (getWidth()/N_STEPS);
double dy = (getWidth()/N_STEPS);

Should probably:

double dx = (getWidth()/N_STEPS);
double dy = (getHeight()/N_STEPS);

Basically what the tuorial is demonstrating is animating the motion of the ball. Now in each iteration of the while loop the ball has to move a certain distance in x and a certain distance in y. This incremental change is denoted as dx and dy.

To calculate how much the ball should move in each increment, the total distance the ball should move is divided by the number of iterations. This is to create the sensation of smooth movement. If the number of steps was too low and the total distance needed to travel was too high, then the ball would jump from one spot to the next.

N-STEPS is the distance in pixels/unit on screen. Different devices have different sizes. Say you have a device that has a screen of 800 pixels in width. Regardless of the device size, you may want to make an imaginary grid. Say if we divide the size by 20, we get 40 (800/20=40). This gives us 40 pixels or 1 unit. So, if we move something, we can move 40 pixels or 1 unit at a time horizontally. This helps when dealing with different screen sizes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM