@Override
  public void update(float pDeltaTime) {
    mGame.getInput().getMouseEvents();
    mGame.getInput().getKeyEvents();

    int len = mBalls.size();
    for (int i = 0; i < len; i++) {
      Ball firstBall = mBalls.get(i);

      for (int j = 0; j < len; j++) {
        Ball secondBall = mBalls.get(i);
        if (secondBall == firstBall
            || !OverlapTester.overlapCircles(firstBall.bounds, secondBall.bounds)) {
          continue;
        }
        System.out.println("overlap");
        float newVelocityX1 =
            (firstBall.velocity.x * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.x * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY1 =
            (firstBall.velocity.y * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.y * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityX2 =
            (secondBall.velocity.x * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.x * firstBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY2 =
            (secondBall.velocity.y * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.y * firstBall.mass))
                / (firstBall.mass + secondBall.mass);

        firstBall.velocity.add(newVelocityX1, newVelocityY1);
        secondBall.velocity.add(newVelocityX2, newVelocityY2);
      }

      // firstBall.velocity.add(mGravity.x * pDeltaTime, mGravity.y * pDeltaTime);

      firstBall.update(pDeltaTime);
      if (firstBall.getX() < firstBall.getWidth() / 2) {
        firstBall.setX(firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getX() > mWidth - firstBall.getWidth() / 2) {
        firstBall.setX(mWidth - firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getY() < firstBall.getWidth() / 2) {
        firstBall.setY(firstBall.getWidth() / 2);
        firstBall.velocity.y *= -1;
      }
      if (firstBall.getY() > mHeight - firstBall.getHeight() / 2) {
        firstBall.setY(mHeight - firstBall.getHeight() / 2);
        firstBall.velocity.y *= -1;
      }
    }
  }
Пример #2
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