Exemplo n.º 1
1
  /**
   * All of the game's logic (per game iteration) is in this function. Given some initial game
   * state, it computes the next game state.
   */
  private void doGameLogic() {
    float px = mBall.x;
    float py = mBall.y;

    mBall.move();

    // Shake it up if it appears to not be moving vertically
    if (py == mBall.y && mBall.serving() == false) {
      mBall.randomAngle();
    }

    // Do some basic paddle AI
    if (!mRed.player) doAI(mRed, mBlue);
    else mRed.move();

    if (!mBlue.player) doAI(mBlue, mRed);
    else mBlue.move();

    handleBounces(px, py);

    // See if all is lost
    if (mBall.y >= getHeight()) {
      mNewRound = true;
      mBlue.loseLife();

      if (mBlue.living()) playSound(mMissSFX);
      else playSound(mWinSFX);
    } else if (mBall.y <= 0) {
      mNewRound = true;
      mRed.loseLife();
      if (mRed.living()) playSound(mMissSFX);
      else playSound(mWinSFX);
    }
  }
Exemplo n.º 2
0
  public void moveBalls(Ball.DIRECTION dir, float x) {
    if (mPaused) return;

    int speed = (int) (m_ballSpeed * x);
    speed /= 3;

    synchronized (m_balls) {
      for (Ball b : m_balls) b.move(speed, dir);
    }

    synchronized (m_bonusBalls) {
      for (Ball b : m_bonusBalls) b.move(speed / 2, dir);
    }
  }
Exemplo n.º 3
0
  @Override
  public void run() {
    while (!Thread.currentThread().isInterrupted()) {
      try {
        if (ball.getName().equals("reader")) {

          if (ball.isEnteringCs()) {
            readWrite.enterReader();
          } else if (ball.isLeavingCs()) {
            readWrite.exitReader();
          }

        } else if (ball.getName().equals("writer")) {
          if (ball.isEnteringCs()) {
            readWrite.enterWriter();
          } else if (ball.isLeavingCs()) {
            readWrite.exitWriter();
          }
        }
        ball.move();

        Thread.sleep(ball.getSpeed());

      } catch (InterruptedException ex) {
        if (ball.getName().equals("reader")) {
          readWrite.interruptedReader(ball);
        } else if (ball.getName().equals("writer")) {
          readWrite.interruptedWriter(ball);
        }
        Thread.currentThread().interrupt();
      }
    }
  }
Exemplo n.º 4
0
 @Override
 public void paint(Graphics g) {
   super.paint(g);
   for (Ball b : ballsList) {
     g.setColor(b.getColor());
     g.fillOval(b.getX(), b.getY(), b.getSize(), b.getSize());
     b.move(getWidth(), getHeight());
   }
 }
Exemplo n.º 5
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (!t.isRunning()) {
      t.start();
    }

    this.detectCollitions();

    for (Ball i : b) {
      int[] oval = i.getOval();
      i.move();
      g.fillOval(oval[0], oval[1], oval[2], oval[3]);
    }
  }
Exemplo n.º 6
0
 private void start() {
   while (true) {
     ball.accelerate();
     ball.interact(paddle);
     ball.move();
     if (ball.isReachToButtom()) {
       System.out.println("Game Over!");
       break;
     }
     repaint();
     try {
       Thread.sleep(INTERVAL);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
Exemplo n.º 7
0
 @Override
 public void updateGeometricState(final double time, final boolean initiator) {
   super.updateGeometricState(time, initiator);
   _ball.move(_areaWidth, _areaHeight);
   setLocalXY((int) _ball._x, (int) _ball._y);
 }
Exemplo n.º 8
0
 public void bounceTop() {
   dy = -dy;
   move();
 }
Exemplo n.º 9
0
 public void bounceSide() {
   dx = -dx;
   dy = (int) (Math.random() * 8) - 4;
   move();
   move();
 }
Exemplo n.º 10
0
 /** Atualiza a movimentacao dos dois componentes do jogo: A racquet e a bola */
 public void move() {
   ball.move();
   racquet.move();
 }
Exemplo n.º 11
0
  public void run() {
    while (true) {
      if (t < 5) {
        t++;
      } else {
        t = 0;
      }
      if (mainMenu) {

        // Ball is bounced if its x - position reaches the right border of the applet
        if (posy_cursor < posy_new) {

          // Change direction of ball movement
          posy_cursor += 50;

        }
        // Ball is bounced if its x - position reaches the left border of the applet
        else if (posy_cursor > posy_highscores) {

          // Change direction of ball movement
          posy_cursor -= 50;
        }
      } else if (gamePlaying) {
        ball.move();
        if (ball.y_Pos() < 0) {
          ball.reflectVertically();

        } else if ((ball.x_Pos() < 0) || (ball.x_Pos() > appletsize_x)) {
          ball.reflectHorizontally();
        }

        if (ball.y_Pos() > 590) {

          if ((ball.x_Pos() > pad.x() - ball.radius())
              && ((ball.x_Pos() < pad.x() + (pad.width() / 2)))) {
            ball.reflectFromPaddle(LEFT);
          } else if ((ball.x_Pos() > pad.x() + (pad.width() / 2))
              && ((ball.x_Pos() < pad.x() + pad.width + ball.radius()))) {
            ball.reflectFromPaddle(RIGHT);
          } else {
            ball.startBall();
          }
        }

        for (int x = 0; x < numBricksX; x++) {
          for (int y = 0; y < numBricksY; y++) {
            if (brickWall[x][y].notBroken()) {
              if (brickWall[x][y].ballContactVertical(ball.x_Pos(), ball.y_Pos())) {
                ball.reflectVertically();
                brickWall[x][y].reduceLife();
                brickWall[x][y].startDropping();
              } else if (brickWall[x][y].ballContactHorizontal(ball.x_Pos(), ball.y_Pos())) {
                ball.reflectHorizontally();
                brickWall[x][y].reduceLife();
                brickWall[x][y].startDropping();
              }
            }
          }
        }
      }
      if (t == 5) {
        for (int x = 0; x < numBricksX; x++) {
          for (int y = 0; y < numBricksY; y++) {
            brickWall[x][y].dropPowerUp();
            if (brickWall[x][y].paddleContact(pad.xPos, 590)) {
              pad.widthIncrease();
            }
          }
        }
      }

      try {
        // Stop thread for 20 milliseconds
        Thread.sleep(2);
      } catch (InterruptedException ex) {
        // do nothing
      }

      repaint();
    }
  }
Exemplo n.º 12
0
 public void mouseClicked(MouseEvent ev) {
   ball.move(ev.getX(), ev.getY());
   repaint();
 }