Пример #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;
    }
  }
Пример #2
0
  /** getReady: Initialize the game */
  private void getReady() {

    // Make up the bricks
    double brickPerWidth =
        (getWidth() - DIST_BRICK_WALL * 2 - DIST_BRICK_BRICK * (NUM_BRICK_ROW - 1)) / NUM_BRICK_ROW;
    double brickPerHeight = BRICK_HEIGHT;
    double x_start = DIST_BRICK_WALL;
    double y_start;
    for (int i = 1; i <= NUM_BRICK_ROW; i++) {
      for (int j = 1; j <= NUM_BRICK_COL; j++) {
        y_start = 50 + (j - 1) * (DIST_BRICK_BRICK + brickPerHeight);
        GRect tmpbrick = new GRect(brickPerWidth, brickPerHeight);
        tmpbrick.setFilled(true);
        add(tmpbrick, x_start, y_start);
      }
      x_start += DIST_BRICK_BRICK + brickPerWidth;
    }

    // Make up the board
    board = new GRect(BOARD_WIDTH, BOARD_HEIGHT);
    board.setFilled(true);
    add(board, (getWidth() - BOARD_WIDTH) / 2, (getHeight() - 30));

    // Place the ball
    ball = new GOval(BALL_RADIUS * 2, BALL_RADIUS * 2);
    ball.setFilled(true);
    ball.setColor(Color.RED);
    add(ball, (getWidth() - BALL_RADIUS * 2) / 2, (getHeight() - 30 - BALL_RADIUS * 2));

    // Set up random generator
    rgen = RandomGenerator.getInstance();

    // Add Listeners
    addMouseListeners();
  }
Пример #3
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
   }
 }
 public void run() {
   for (int i = 0; i < NCIRCLES; i++) {
     double r = rgen.nextDouble(MIN_RADIUS, MAX_RADIUS);
     double x = rgen.nextDouble(0, getWidth() - 2 * r);
     double y = rgen.nextDouble(0, getHeight() - 2 * r);
     GOval circle = new GOval(x, y, 2 * r, 2 * r);
     circle.setFilled(true);
     circle.setColor(rgen.nextColor());
     add(circle);
   }
 }
Пример #5
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;
   }
 }
Пример #6
0
  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);
    }
  }
Пример #7
0
 private void drawBall() {
   double x = WIDTH / 2 - BALL_RADIUS;
   double y = HEIGHT / 2 - BALL_RADIUS;
   double d = 2 * BALL_RADIUS;
   ball = new GOval(x, y, d, d);
   ball.setFilled(true);
   add(ball);
 }
Пример #8
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;
     }
   }
 }
Пример #9
0
 private void gaming() {
   boolean loseOneTurn;
   boolean win = false;
   boolean stillAlive = true;
   for (int i = NTURNS; i > 0; i--) { // loops for the number of turns.
     GLabel life = showLifeCount(i);
     waitForClick();
     add(life);
     while (stillAlive) { // the ball moves and bounces until the user loses or wins the turn.
       moveBall();
       loseOneTurn =
           (ball.getY() >= HEIGHT); // user loses one turn when the ball falls under the paddle.
       win = (brickCount == 0); // user wins when there is no brick left.
       stillAlive = !(loseOneTurn || win);
     }
     if (win) break; // breaks from the loop if the user wins in one turn.
     remove(ball);
     createBall(); // a new ball appears on the center of the screen after one turn
     stillAlive = true;
     remove(life);
   }
 }
Пример #10
0
 // Update/move ball down and sideways. yVel increases each cycle, due to gravity.
 private void moveBall() {
   yVel += GRAVITY;
   ball.move(xVel, yVel);
 }
Пример #11
0
 // Create and place ball
 private void setup() {
   ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
   ball.setFilled(true);
   add(ball);
 }
Пример #12
0
 private void startFall() {
   ball.move(vx, vy);
   pause(DELAY);
   ball.sendToBack();
 }
Пример #13
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);
    }
  }
Пример #14
0
 /** Move the ball */
 private void moveBall() {
   ball.move(vx, vy);
 }
Пример #15
0
 /** Check whether the game is over */
 private boolean gameover() {
   return (ball.getY() > (getHeight() - 2 * BALL_RADIUS)) || (num_brick_left == 0);
 }
Пример #16
0
 /*Defines and places the Rent state as a circle */
 private void placeRent(int x, int y, int width, int height, Color rentColor) {
   GOval shape = new GOval(x, y, width, height);
   shape.setFilled(true);
   shape.setColor(rentColor);
   add(shape);
 }