Esempio n. 1
0
  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;
    }
  }
 /**
  * Makes the ball bounce from the top wall and paddle When you hit the upper wall and a paddle
  * changes direction along the y-coordinate.
  */
 private double checkTheTopWallAndPaddle(double vy) {
   if (ball.getY() + 1 <= 0) { // Checks upper wall
     vy = -vy;
   } else if (getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) == paddle
       || getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) == paddle) { // Checks paddle
     if (vy > -2.0) {
       vy = -vy - 0.1; // Makes the the acceleration of the ball at every touch the paddle
     }
   }
   return vy;
 }
Esempio n. 3
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;
   }
 }
Esempio n. 4
0
  /** Check for collision */
  private void checkRebounce() {

    // check for left/right/top wall
    if (ball.getX() < 0) {
      vx *= -1;
      ball.move(vx, 0);
      return;
    } else if ((ball.getX() + 2 * BALL_RADIUS) > getWidth()) {
      vx *= -1;
      ball.move(vx, 0);
      return;
    } else if (ball.getY() < 0) {
      ball.setLocation(ball.getX(), -ball.getY());
      vy *= -1;
      return;
    }

    // check for collision with bricks: from the four vertexs
    lefttop = getElementAt(ball.getX(), ball.getY());
    righttop = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY());
    leftbottom = getElementAt(ball.getX(), ball.getY() + 2 * BALL_RADIUS);
    rightbottom = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY() + 2 * BALL_RADIUS);
    if ((lefttop != null) && (righttop != null) && (lefttop != board) && (lefttop != mylabel)) {
      remove(lefttop);
      num_brick_left--;
      vy *= -1;
    } else if ((righttop != null)
        && (rightbottom != null)
        && (righttop != board)
        && (righttop != mylabel)) {
      remove(righttop);
      num_brick_left--;
      vx *= -1;
    } else if ((rightbottom != null)
        && (leftbottom != null)
        && (rightbottom != board)
        && (rightbottom != mylabel)) {
      remove(rightbottom);
      num_brick_left--;
      vy *= -1;
    } else if ((leftbottom != null)
        && (lefttop != null)
        && (leftbottom != board)
        && (leftbottom != mylabel)) {
      remove(leftbottom);
      num_brick_left--;
      vx *= -1;
    }

    // check for collision with board
    if (getElementAt(ball.getX(), (ball.getY() + BALL_RADIUS / 2)) == board) {
      vy *= -1;
      ball.move(0, vy);
    }
  }
Esempio n. 5
0
 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;
     }
   }
 }
  /**
   * Creates a point at the ball 4 check and verifies the presence of an object near it. Returns
   * null or the name of the object, if it is present
   */
  private GObject getCollidingObject() {
    GObject colliding;

    colliding =
        getElementAt(ball.getX(), ball.getY()); // Creates the upper left point of check ball
    if (colliding != null) {
      Color color = colliding.getColor();
      count(color);
      return colliding;
    }
    colliding =
        getElementAt(
            ball.getX() + BALL_RADIUS * 2,
            ball.getY()); // Creates the top right point of check ball
    if (colliding != null) {
      Color color = colliding.getColor();
      count(color);
      return colliding;
    }
    colliding =
        getElementAt(
            ball.getX(),
            ball.getY() + BALL_RADIUS * 2); // Creates the lower left point of check ball
    if (colliding != null) {
      Color color = colliding.getColor();
      count(color);
      return colliding;
    }
    colliding =
        getElementAt(
            ball.getX() + BALL_RADIUS * 2,
            ball.getY() + BALL_RADIUS * 2); // Creates a check point of the lower right on the ball
    if (colliding != null) {
      Color color = colliding.getColor();
      count(color);
      return colliding;
    }

    return null;
  }
 /**
  * Makes the ball bounce from the left and right walls. At blow the side of the wall, changes
  * direction of the x coordinate. Returns the direction of the ball along the x coordinate
  */
 private double checkTheLeftAndRightWall(double vx) {
   if (ball.getX() - 1 <= 0 || ball.getX() + BALL_RADIUS * 2 + 1 >= WIDTH) {
     vx = -vx;
   }
   return vx;
 }