Beispiel #1
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);
    }
  }
Beispiel #2
0
 public void keyPressed(KeyEvent ke) {
   if (ke.getKeyCode() == KeyEvent.VK_RIGHT && isCollidable(playerX + 1, playerY)) {
     player.move(25, 0);
     playerX = playerX + 1;
   } else if (ke.getKeyCode() == KeyEvent.VK_LEFT && isCollidable(playerX - 1, playerY)) {
     player.move(-25, 0);
     playerX = playerX - 1;
   } else if (ke.getKeyCode() == KeyEvent.VK_DOWN && isCollidable(playerX, playerY + 1)) {
     player.move(0, 25);
     playerY = playerY + 1;
   } else if (ke.getKeyCode() == KeyEvent.VK_UP && isCollidable(playerX, playerY - 1)) {
     player.move(0, -25);
     playerY = playerY - 1;
   }
 }
 public void run() {
   GOval o = new GOval(40, 40);
   o.setColor(Color.RED);
   o.setFilled(true);
   add(o, 100, 100);
   while (true) {
     pause(WAIT);
     o.move(xMove, yMove);
   }
 }
Beispiel #4
0
 private void PrecisionPaddle() {
   if (ball.getY()
       > HEIGHT
           - PADDLE_Y_OFFSET
           - PADDLE_HEIGHT
           - BALL_RADIUS * 2) { // check if the ball drops below the paddle
     double diff = ball.getY() - (HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT - BALL_RADIUS * 2);
     ball.move(0, -2 * diff); // move ball an amount equal to the amount it drops below the paddle
   }
 }
  private void checkForCollision() {
    // check if ball has hit floor. If so, bounce it upwards.
    if (ball.getY() > getHeight() - DIAM_BALL) {
      bounceClip.play();
      yVel = -yVel * BOUNCE_REDUCE; // bounces back almost to BOUNCE_REDUCE * starting height

      // moves ball back above the floor the same distance it would have dropped below the floor in
      // same time
      double diff = ball.getY() - (getHeight() - DIAM_BALL);
      ball.move(0, -2 * diff);
    }
  }
  /**
   * The method which controls the movement the ball, check all the objects with which the ball is
   * faced and performs the appropriate actions to them
   */
  private boolean runBall() {
    double vx;
    double vy = SPEED_Y;
    boolean res = false;
    vx = rgen.nextDouble(1.0, 3.0); // randomly sets horizontal speed
    if (rgen.nextBoolean(
        0.5)) // with a 50 percent chance change the direction of movement of the ball
    vx = -vx;

    while (allDelBricks
        < NBRICK_ROWS * NBRICKS_PER_ROW) { // The game will not end until all the bricks
      GObject collider = getCollidingObject(); // The variable gets test result object
      vx = checkTheLeftAndRightWall(vx); // Check the collision of the ball on the side walls
      vy =
          checkTheTopWallAndPaddle(vy); // Check the collision of the ball on the ceiling and paddle
      vx =
          checkLeftAndRightWallOfPaddle(
              vx); // Check the collision of the ball with the side part of the paddle

      if (collider != null) { // If checkpoints are returned brick, it is then removed
        if (collider != paddle) {
          remove(collider);
          vy = -vy;
          allDelBricks++;
        }
      }
      ball.move(vx, vy);
      pause(TIME_PAUSE);
      if (ball.getY()
          > HEIGHT
              - (PADDLE_Y_OFFSET / 3)
              - BALL_RADIUS * 2) { // The game stops when the ball falls below the window
        res = false;
        return res;
      }
      res = true;
    }
    return res;
  }
 // Update/move ball down and sideways. yVel increases each cycle, due to gravity.
 private void moveBall() {
   yVel += GRAVITY;
   ball.move(xVel, yVel);
 }
Beispiel #8
0
 private void startFall() {
   ball.move(vx, vy);
   pause(DELAY);
   ball.sendToBack();
 }
Beispiel #9
0
 /** Move the ball */
 private void moveBall() {
   ball.move(vx, vy);
 }