예제 #1
0
 private void getVelocity() {
   vy = 3.0;
   vx = rgen.nextDouble(1.0, 3.0);
   if (rgen.nextBoolean(0.5)) {
     vx = -vx;
   }
 }
예제 #2
0
 /** When mouse pressed: begin our fantastic game */
 public void mousePressed(MouseEvent e) {
   if (vx == 0) {
     vx = rgen.nextDouble(0.5, 1);
     vy = -1;
     if (rgen.nextBoolean() == true) {
       vx *= -1;
     }
   }
 }
예제 #3
0
 public GLabel makeLetterLabel(int numDivs) {
   boolean get = randgen.nextBoolean();
   String ga;
   if (get) ga = "G";
   else ga = "A";
   GLabel getOrAvoid = new GLabel(ga);
   getOrAvoid.setFont(new Font("Cambria", Font.BOLD, 24));
   double locx = randgen.nextDouble(INDENT, getWidth() - getOrAvoid.getWidth());
   int whichdiv = randgen.nextInt(1, numDivs);
   double locy = whichdiv * getHeight() / numDivs;
   getOrAvoid.setLocation(locx, locy);
   return getOrAvoid;
 }
  public void run() {

    /* Initialising variables. */
    int counter = 0;
    int currentMoney = 1;
    int totalMoneyInGame = 0;

    /* The main iteration for the St.Peterburg's game.
    If total money is > than 20 - ends the game. */
    while (totalMoneyInGame < 20) {

      /* Randoms flip boolean value for each stage of iteration */
      RandomGenerator r = RandomGenerator.getInstance();
      boolean flip = r.nextBoolean();

      /* Condition for the heads flip, if it's true - double current money */
      if (flip == true) {
        currentMoney *= 2;

        /* Condition for the streak of heads. If total money is >= 20 and current money is >=20,
        prints phrases and adds 1 to counter.
         */
        if (totalMoneyInGame >= 20 && currentMoney >= 20) {
          println("This game you earned $" + currentMoney);
          println("Your total is $" + totalMoneyInGame);
          counter++;
        }
        /* Condition for the tails. Prints current money phrase.
        Prints total money phrase and adds 1 to the counter.
        In the end initialize current money to the start bid.
         */
      } else {
        println("This game you earned $" + currentMoney);
        /* If current money >=1 adds them to the total money. */
        if (currentMoney >= 1) totalMoneyInGame = totalMoneyInGame + currentMoney;
        println("Your total is $" + totalMoneyInGame);
        counter++;
        currentMoney = 1;
      }
    }
    /* In the end of the game prints counter phrase */
    println("It took " + counter + " games to earn $20");
  }
  /**
   * 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;
  }