Ejemplo n.º 1
0
 public void paint(Graphics g) {
   if (mainMenu) {
     drawTitleScreen(g);
   } else if (gamePlaying) {
     drawBricks(g);
     g.setColor(Color.green);
     g.fillRect(pad.x(), 590, pad.width(), 10);
     g.fillOval(
         ball.x_Pos() - ball.radius(),
         ball.y_Pos() - ball.radius(),
         ball.radius() * 2,
         ball.radius() * 2);
   }
 } // paint method
Ejemplo n.º 2
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();
    }
  }