public void run() { while (true) { // move ball to right // end when ball moves off right side of screen setup(); while (ball.getX() < getWidth()) { moveBall(); checkForCollision(); pause(DELAY); } // move ball to left xVel = -X_VEL; X_START = getWidth() - DIAM_BALL; setup(); while (ball.getX() > -DIAM_BALL - 5) { moveBall(); checkForCollision(); pause(DELAY); } X_START = DIAM_BALL / 2; xVel = X_VEL; yVel = 0.0; } }
private void checkWall() { boolean checkLeftWall = ball.getX() <= 0; boolean checkRightWall = ball.getX() + BALL_RADIUS * 2 >= WIDTH; boolean checkTop = ball.getY() <= 0; if ((checkLeftWall) || (checkRightWall)) { vx = -vx; } if (checkTop) { vy = -vy; } }
private void checkObject() { // x and y parameters for the different corners double leftX = ball.getX(); double rightX = ball.getX() + (2 * BALL_RADIUS); double upperY = ball.getY(); double lowerY = ball.getY() + (2 * BALL_RADIUS); // check the corners for object GObject upperLeft = checkCorner(leftX, upperY); // check upper-left corner if (upperLeft == null) { GObject upperRight = checkCorner(rightX, upperY); // check upper-right corner } GObject lowerLeft = checkCorner(leftX, lowerY); // //check lower-left corner if (lowerLeft == null) { GObject lowerRight = checkCorner(rightX, lowerY); // check lower-right corner if ((lowerLeft == paddle) && (lowerRight == paddle)) { // When both lower corners hit paddle, change direction. vy = -vy; } } }