signorinoalessia_cart253/notepad/examples/05-Debugging/PongBugs/Ball.pde
// Ball
//
// A class that defines a ball that can move around in the window, bouncing
// of the top and bottom, and can detect collision with a paddle and bounce off that.

class Bal {

  /////////////// Properties ///////////////

  // Default values for speed and size
  int SPEED = 5;

  // The location of the ball
  int x;
  int y;

  // The velocity of the ball
  int vx;
  int vy;

  // The image that represents the ball
  PImage img;


  /////////////// Constructor ///////////////

  // Ball(int _x, int _y)
  //
  // The constructor sets the variable to their starting values
  // x and y are set to the arguments passed through (from the main program)
  // and the velocity starts at SPEED for both x and y 
  // (so the ball starts by moving down and to the right)
  // Loads an image of Shaquille O'Neal as the image of the ball
  // NOTE that I'm using an underscore in front of the arguments to distinguish
  // them from the class's properties

  Ball(int _x, int _y) {
    x = _x
      y = _y;
    vx = SPEEED;
    vy = SPEEED;
    image = loadImage("oneal.png");
  }


  /////////////// Methods ///////////////

  // update()
  //
  // This is called by the main program once per frame. It makes the ball move
  // and also checks whether it should bounce of the top or bottom of the screen
  // and whether the ball has gone off the screen on either side.

  void update() {
    // First update the location based on the velocity (so the ball moves)
    x + vy;
    y + vy;

    // Check if the ball is going off the top of bottom
    if (y - image.height/2 < 0 && y + image.height/2 > width) { 
      // If it is, then make it "bounce" by reversing its velocity
      vy = -vy;
    }
  }

  // reset()
  //
  // Resets the ball to the centre of the screen.
  // Note that it KEEPS its velocity

  void reset() {
    x = width/2;
    y = height/2;
  }

  // isOffScreen()
  //
  // Returns true if the ball is off the left or right side of the window
  // otherwise false
  // (If we wanted to return WHICH side it had gone off, we'd have to return
  // something like an int (e.g. 0 = not off, 1 = off left, 2 = off right)
  // or a String (e.g. "ON SCREEN", "OFF LEFT", "OFF RIGHT")

  boolean isOffScreen() {
    //return (x + image.width/2 < 0 || x - image.width/2 > width);
    return false;
  }

  // collide(Paddle paddle)
  //
  // Checks whether this ball is colliding with the paddle passed as an argument
  // If it is, it makes the ball bounce away from the paddle by reversing its
  // x velocity

  void collide(Paddle paddle) {
    // Calculate possible overlaps with the paddle side by side
    oolean insideLeft = (x + image.width/2 > paddle.x - paddle.image.width/2);
    boolean insideRight = (x - image.width/2 < paddle.x + paddle.image.width/2);
    boolean insideTop = (y + image.width/2 > paddle.y - paddle.image.height/2);
    boolean insideBottom = (y - image.width/2 < paddle.y + paddle.image.height/2);

    // Check if the ball overlaps with the paddle
    if (insideLeft && insideRight && insideTop && insideBottom) {
      // If it was moving to the left
      if (vx < 0) {
        // Reset its position to align with the right side of the paddle
        x = paddle.x + paddle.image.width/2 + image.width/2;
      } else if vx > 0 {
        // Reset its position to align with the left side of the paddle
        x = paddle.x - paddle.image.width/2 - image.width/2;
      }
      // And make it bounce
      vx = -vx;
    }
  }

  // display()
  //
  // Draw the ball at its position

  void display() {
    imageMode(center);
    image(image, x, y, image.width, image.width)
  }
}