Esempio n. 1
0
 public void mouseMoved(MouseEvent e) { // called during motion when no buttons are down
   //  if (e.getY ==
   if ((e.getX() < appletsize_x - pad.width() / 2) && (e.getX() > (pad.width() / 2))) {
     pad.changeX(e.getX() - (pad.width() / 2));
     ball.moveWithPaddle(pad.xPos + (pad.width() / 2) - (ball.radius()));
   }
   e.consume();
   repaint();
 }
Esempio n. 2
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
Esempio n. 3
0
    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;

      // paint ball, paddle, and brick configuration
      g2.drawImage(background.getImage(), 0, 0, null);
      bconfig.paint(g2);
      paddle.paint(g2);
      ball.paint(g2);
      g2.setColor(Color.WHITE);
      g2.setFont(new Font("Serif", Font.PLAIN, 20));
      g2.drawString("Score: " + score, 15, 20);
      if (ball.getY() > 500) {
        g2.setColor(Color.WHITE);
        g2.setFont(new Font("Serif", Font.PLAIN, 30));
        g2.drawString("Game Over!", 200, 300);
        g2.drawString("Your final score was " + score, 150, 330);
        timer.stop();
      }
    }
Esempio n. 4
0
    public void checkForHit() {

      // change ball speed when ball hits paddle
      if (ball.getShape().intersects(paddle.getShape())) {
        int leftSide = paddle.getX();
        int middleLeft = paddle.getX() + (int) (paddle.getWidth() / 3);
        int middleRight = paddle.getX() + (int) (2 * paddle.getWidth() / 3);
        int rightSide = paddle.getX() + paddle.getWidth();

        if ((ball.getX() >= leftSide) && (ball.getX() < middleLeft)) {
          // change ball speed
          ball.setXspeed(-2);
          ball.setYspeed(-2);
        }
        if ((ball.getX() >= middleLeft) && (ball.getX() <= middleRight)) {
          // change ball speed
          ball.setYspeed(-2);
        }
        if ((ball.getX() > middleRight) && (ball.getX() <= rightSide)) {
          // change ball speed
          ball.setXspeed(2);
          ball.setYspeed(-2);
        }
      }

      // change ball speed when ball hits brick
      for (int i = 0; i < bconfig.getRows(); i++) {
        for (int j = 0; j < bconfig.getCols(); j++) {
          if (bconfig.exists(i, j)) {
            if (ball.getShape().intersects(bconfig.getBrick(i, j).getShape())) {
              Point ballLeft =
                  new Point(
                      (int) ball.getShape().getX(),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballRight =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth()),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballTop =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) ball.getShape().getY());
              Point ballBottom =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight()));
              if (bconfig.getBrick(i, j).getShape().contains(ballLeft)) {
                // change ball speed
                ball.setXspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballRight)) {
                // change ball speed
                ball.setXspeed(-2);
                score++;
              }
              if (bconfig.getBrick(i, j).getShape().contains(ballTop)) {
                // change ball speed
                ball.setYspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballBottom)) {
                // change ball speed
                ball.setYspeed(-2);
                score++;
              }

              // remove brick
              bconfig.removeBrick(i, j);
            }
          }
        }
      }
    }
Esempio n. 5
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();
    }
  }