Ejemplo 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();
 }
Ejemplo 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
Ejemplo 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();
      }
    }
Ejemplo n.º 4
0
  // Constructor//
  public Game_Screen() {
    JFrame game_frame = new JFrame("Legend of Pong"); // creates the jframe
    JLabel Background =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\gameScreen.jpg")); // gets the BG image
    game_frame.setContentPane(Background); // places background image

    // setting the header section//

    // Score//
    long score = getScore(); // int to hold score value
    JLabel scoreL = new JLabel(); // label to display score
    scoreL.setText("Score: " + String.valueOf(score)); // connects label and int
    scoreL.setBounds(25, 15, 200, 35); // sets location of score
    scoreL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    scoreL.setForeground(Color.GREEN); // sets color of Score
    game_frame.add(scoreL); // adds score

    // Timer//
    int time = getTime();
    JLabel timeL = new JLabel(); // label to hold time
    timeL.setText("Time(sec): " + String.valueOf(time)); // connects label and int
    timeL.setBounds(350, 15, 200, 35); // set location of time
    timeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    timeL.setForeground(Color.GREEN); // sets color
    game_frame.add(timeL); // adds time

    // Life//
    int life = getLife(); // int to hold value of lives left
    JLabel lifeL = new JLabel(); // label to hold life
    lifeL.setText("Lives: " + String.valueOf(life)); // connects label and int
    lifeL.setBounds(700, 15, 200, 35); // sets location of life
    lifeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    lifeL.setForeground(Color.GREEN); // sets color of life
    game_frame.add(lifeL); // adds life

    // Paddle//
    JPanel paddle = new JPanel(); // new panel for paddle object and image
    Paddle object = new Paddle(); // creates new paddle object
    JLabel link =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Link.gif")); // new
                                                                                             // paddle image(also link)
    paddle.add(object); // adds the actual object
    paddle.add(link); // adds image to the paddle Panel
    paddle.setBounds(
        object.getX(),
        object.getY(),
        object.getWidth(),
        object.getHeight()); // sets location and size of the paddle
    game_frame.add(paddle); // game_frame gets link image

    // a single ball = test //
    JPanel ball = new JPanel(); // creates the JPanel to hold the ball object and image
    Ball obj = new Ball(); // creates the ball object
    JLabel stone =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Ball.png")); // creates the ball image
    ball.add(obj); // adds the object
    ball.add(stone); // adds the image
    ball.setBounds(750, 250, obj.getWidth(), obj.getHeight()); // sets ball location and size
    game_frame.add(ball); // adds it to the screen

    // final frame things//
    game_frame.pack();
    game_frame.setResizable(false);
    game_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    game_frame.setVisible(true);
  } // end of screen constructor
Ejemplo n.º 5
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);
            }
          }
        }
      }
    }
Ejemplo n.º 6
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();
    }
  }