/*### Creates Oval ###*/
 private GOval createOval(int gWidth, int gHeight, int x, int y, int locX, int locY, Color col) {
   GOval Oval = new GOval(gWidth, gHeight, x, y);
   Oval.setLocation(locX, locY);
   Oval.setColor(col);
   Oval.setFilled(true);
   return Oval;
 }
 private void buildEye(double x, double y) {
   GOval Eye = new GOval(x, y, eye_radius * 2, eye_radius * 2);
   Eye.setColor(Color.YELLOW);
   Eye.setFilled(true);
   Eye.setFillColor(Color.YELLOW);
   add(Eye);
 }
Beispiel #3
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();
  }
  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;
    }
  }
Beispiel #5
0
 private void PaintOval(
     int x, int y, int Width, int Height) { // Paint four round in corners? black color
   GOval OvalPaint = new GOval(x, y, Width, Height);
   OvalPaint.setFilled(true);
   OvalPaint.setColor(Color.BLACK);
   add(OvalPaint);
 }
 /** The method takes parameters and returns, oval shape figure */
 private GOval oval(int x, int y, int width, int height, Color colorSetFill, Color colorSet) {
   GOval oval = new GOval(x, y, width, height);
   oval.setFilled(true);
   oval.setFillColor(colorSetFill);
   oval.setColor(colorSet);
   add(oval);
   return oval;
 }
Beispiel #7
0
 public void drawInnerOval() {
   double x = getWidth() / 2.0 - INNER_CIRCLE_WIDTH / 2.0;
   double y = getHeight() / 2.0 - INNER_CIRCLE_WIDTH / 2.0;
   GOval oval = new GOval(x, y, INNER_CIRCLE_WIDTH, INNER_CIRCLE_WIDTH);
   add(oval);
   oval.setFilled(true);
   oval.setFillColor(Color.RED);
   oval.setColor(Color.WHITE);
 }
Beispiel #8
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 GOval createFilledCircle(double x, double y, double r, int i) {
   GOval circle = new GOval(x - r, y - r, 2 * r, 2 * r);
   if (i % 2 != 0) {
     circle.setColor(Color.RED);
   } else {
     circle.setColor(Color.WHITE);
   }
   circle.setFilled(true);
   return circle;
 }
 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 #11
0
  public void drawWhiteOval() {

    double x = getWidth() / 2.0 - WHITE_CIRCLE_WIDTH / 2.0;
    double y = getHeight() / 2.0 - WHITE_CIRCLE_WIDTH / 2.0;
    GOval oval = new GOval(x, y, WHITE_CIRCLE_WIDTH, WHITE_CIRCLE_WIDTH);
    add(oval);
    oval.setFilled(true);
    oval.setFillColor(Color.WHITE);
    oval.setColor(Color.WHITE);
  }
Beispiel #12
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;
   }
 }
 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);
   }
 }
 /**
  * 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;
 }
  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);
    }
  }
 private void drawOval(int x, int y, int size) {
   // create circle and set whether it will be red or white
   GOval circle = new GOval(x, y, size, size);
   if ((size % 2) == 0) { // if the size of the circle divides evenly (remainder of zero)
     // color the circle RED
     circle.setFillColor(Color.RED);
   } else {
     // color the circle white
     circle.setFillColor(Color.WHITE);
   }
   circle.setFilled(true); // add color
   add(circle); // create circle
 }
Beispiel #17
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;
   }
 }
Beispiel #18
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);
 }
 /*this method draws four circles*/
 public void drawCircles() {
   // row
   for (int k = 0; k <= 1; k++) {
     // column
     for (int i = 0; i <= 1; i++) {
       GOval oval =
           new GOval(
               i * (getWidth() - RADIUS * 2),
               k * (getHeight() - RADIUS * 2),
               RADIUS * 2,
               RADIUS * 2);
       oval.setFilled(true);
       oval.setFillColor(Color.black);
       add(oval);
     }
   }
 }
Beispiel #20
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;
     }
   }
 }
  /**
   * 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;
  }
 /** Bind the ball to the mouse cursor */
 private void muvedBall(MouseEvent e) {
   if (startGame == 0) {
     int y = HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET - BALL_RADIUS * 2;
     int x;
     if (e.getX()
         > WIDTH
             - PADDLE_WIDTH
                 / 2) { // Makes it impossible to exit the ball over the right part of the window
       x = WIDTH - PADDLE_WIDTH / 2 - BALL_RADIUS;
     } else if (e.getX()
         < PADDLE_WIDTH
             / 2) { // Makes it impossible to exit the ball over the left part of the window
       x = PADDLE_WIDTH / 2 - BALL_RADIUS;
     } else {
       x = e.getX() - BALL_RADIUS; // Bind the ball to the center of the cursor
     }
     ball.setLocation(x, y);
   }
 }
  /**
   * 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;
  }
Beispiel #24
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);
   }
 }
 public void run() {
   double outerCircleDiameter = 2 * OUTER_CIRCLE_RADIUS;
   double middleCircleDiameter = 2 * MIDDLE_CIRCLE_RADIUS;
   double innerCircleDiameter = 2 * INNER_CIRCLE_RADIUS;
   double xOuterCircle = (getWidth() - outerCircleDiameter) / 2;
   double xMiddleCircle = (getWidth() - middleCircleDiameter) / 2;
   double xInnerCircle = (getWidth() - innerCircleDiameter) / 2;
   double yOuterCircle = (getHeight() - outerCircleDiameter) / 2;
   double yMiddleCircle = (getHeight() - middleCircleDiameter) / 2;
   double yInnerCircle = (getHeight() - innerCircleDiameter) / 2;
   GOval outCircle =
       new GOval(xOuterCircle, yOuterCircle, outerCircleDiameter, outerCircleDiameter);
   GOval middleCircle =
       new GOval(xMiddleCircle, yMiddleCircle, middleCircleDiameter, middleCircleDiameter);
   GOval centreCircle =
       new GOval(xInnerCircle, yInnerCircle, innerCircleDiameter, innerCircleDiameter);
   outCircle.setColor(Color.RED);
   middleCircle.setColor(Color.WHITE);
   centreCircle.setColor(Color.RED);
   outCircle.setFilled(true);
   middleCircle.setFilled(true);
   centreCircle.setFilled(true);
   outCircle.setFillColor(Color.RED);
   middleCircle.setFillColor(Color.WHITE);
   centreCircle.setFillColor(Color.RED);
   add(outCircle);
   add(middleCircle);
   add(centreCircle);
 }
 // Draw oval
 private void drawOval(int x, int y) {
   GOval oval = new GOval(x, y, DIAMETER, DIAMETER);
   oval.setColor(Color.black);
   oval.setFilled(true);
   add(oval);
 }
 // Update/move ball down and sideways. yVel increases each cycle, due to gravity.
 private void moveBall() {
   yVel += GRAVITY;
   ball.move(xVel, yVel);
 }
 // Create and place ball
 private void setup() {
   ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
   ball.setFilled(true);
   add(ball);
 }
 /**
  * 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;
 }
Beispiel #30
0
  public void run() {
    addKeyListeners();

    File boardFile = new File("largerboard.txt");
    try {
      Scanner sc = new Scanner(boardFile);
      int rowNum = 0;
      while (sc.hasNextLine()) {
        String s = sc.nextLine();
        char[] nums = s.toCharArray();
        for (int i = 0; i < nums.length; i++) {
          char2D[i][rowNum] = nums[i];
          System.out.print(char2D[i][rowNum] + " ");
        }
        System.out.println();
        rowNum++;
      }
    } catch (IOException e) {
      System.out.println("File not found.");
    }

    GRect box;
    GOval o;
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        // int rand = (int) (3*Math.random());
        // twoD[j][i] = rand;
        // System.out.print(twoD[j][i] + " ");

        box = new GRect(boxSize * j, boxSize * i, boxSize, boxSize);
        add(box);

        if (char2D[j][i] == '0') {
          box.setFilled(true);
          box.setColor(Color.BLACK);
          o = new GOval(boxSize * j + 10, boxSize * i + 10, 5, 5);
          o.setFilled(true);
          o.setColor(Color.YELLOW);
          add(o);
          food.add(o);
        } else if (char2D[j][i] == '1') {
          box.setFilled(true);
          box.setColor(Color.BLUE);
        }
      }
      // System.out.println();
    }
    // end of nested for loops
    player = new GOval(playerX + 5, playerY + 5, 15, 15);
    player.setFilled(true);
    player.setColor(Color.RED);
    add(player);

    addGhosts();

    while (true) {

      // ghost movement
      // 0: up, 1: down, 2: right, 3: left
      for (int i = 0; i < ghostList.size(); i++) {
        ghost = (Ghost) ghostList.get(i);
        if (timer % 500 == 0) {
          if (playerX < ghost.getGhostX()) {
            ghost.move(3);
          } else if (playerY > ghost.getGhostY()) {
            ghost.move(1);
          } else if (playerX > ghost.getGhostX()) {
            ghost.move(2);
          } else if (playerY < ghost.getGhostY()) {
            ghost.move(0);
          }
          //				int rand =(int) (4*Math.random()); // between 0-3
          //				if(rand == 2 && isCollidable(ghost.getGhostX()+1, ghost.getGhostY())){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 3 && isCollidable(ghost.getGhostX()-1, ghost.getGhostY())){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 1 && isCollidable(ghost.getGhostX(), ghost.getGhostY()+1)){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 0 && isCollidable(ghost.getGhostX(), ghost.getGhostY()-1)){
          //					ghost.move(rand);
          //				}
          // System.out.println(ghost.getGhostX() +" , "+ ghost.getGhostY() );
        }
      }

      if (collision(ghost, player)) {
        remove(player);
        hasLost = true;
        break;
      }

      if (food.size() == 0) {
        break;
      }

      for (int i = 0; i < food.size(); i++) {
        GOval foodPiece = (GOval) food.get(i);
        if (collision(player, foodPiece)) {
          remove(foodPiece);
          food.remove(i);
        }
      }

      pause(10);
      timer = timer + 10;
    }

    GLabel wonLost;

    if (hasLost) {
      wonLost = new GLabel("YOU LOSE", 220, 250);
    } else {
      wonLost = new GLabel("YOU WON AND ARE AWESOME", 220, 250);
    }

    wonLost.setColor(Color.WHITE);
    add(wonLost);
  }