How do I make the ball bounce off the sides (top and bottom) of the 2 paddles?

//Variables
float x = 400;
float y = 300;
float spd = 0.5;
float paddle1Y;
float paddle2Y;
int p1Score = 0;
int p2Score = 0;
int p1Life = 10;
int p2Life = 10;
int dirX = -1;
int dirY = -1;
int state = 0;
import processing.sound.*; //import sound file from data folder
SoundFile file;


void setup() {
  size(800, 600);
  noStroke();
  smooth();
}



void draw() {
  background(0);  //Erase The Screen

  //*-------------------Menu---------------------------------
  if (state == 0) {
    fill(255); // Change text colour to white
    textSize(30); // Change text size
    text("Controls: Player 1: 'w' for up, 's' for down)", 95, 530);
    text("Player 2: 'i' for up, 'k' for down)", 237, 565);
    textSize(45); // Change text size
    text("Pong Assignment by Nathaniel K.", 36, 100);
    text("Press 'E' For Easy", 220, 250);
    text("Press 'M' For Medium", 180, 350);
    text("Press 'H' For Hard", 220, 450);
    fill(255, 0); // Change opacity of rectangles to 0
    stroke(255); // Change stroke colour
    strokeWeight(5); // chnage stroke thickness
    rect(215, 205, 378, 58); //draw rectangle
    rect(175, 305, 469, 58); //draw rectangle
    rect(215, 405, 393, 58); //draw rectangle
    rect(90, 495, 620, 90); //draw rectangle
    p1Life = 10; // Player 1's lives
    p2Life = 10; // Player 2's lives

    //*----------------------------Keyboard Controls------------------------
    if (keyPressed && key == 'e') {
      state = 3;
    }

    if (keyPressed && key == 'm') {
      state = 4;
    }

    if (keyPressed && key == 'h') {
      state = 5;
    }
  }
  //*----------------------Reset (Player 1)------------------------------
  else if (state == 1) {
    fill(255); // Change text colour
    textSize(40); // Change text size
    text("Click to Start", 270, height/2);
    stroke(255); // Change stroke colour
    strokeWeight(5); // Change stroke thickness
    fill(255, 0, 0); // Change fill colour
    ellipse(85, paddle1Y, 30, 30); // draw circle
    rect(40, paddle1Y - 50, 20, 100); //draw rectangle
    rect(740, paddle2Y - 50, 20, 100); //draw rectangle

    //*----------------------------Keyboard Controls------------------------
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 10;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 10;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 10;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 10;

    //*-----------If mouse pressed ball resets on top of paddle--------------
    if (mousePressed) {
      x = 75; 
      y = paddle1Y;
      dirX = 1;  // add or subtract the speed based on the direction to move the ball
      dirY = 1;
      state = 3;
    }
  }

  //*----------------------Reset (Player 2)------------------------------
  else if (state == 2) {
    fill(255); // Change text colour
    textSize(40); // Change text size
    text("Click to Start", 270, height/2);
    stroke(255); // Change stroke colour
    strokeWeight(5); // Change stroke thickness
    fill(255, 0, 0); // Change fill colour
    ellipse(715, paddle2Y, 30, 30); // draw circle
    rect(40, paddle1Y - 50, 20, 100); //draw rectangle
    rect(740, paddle2Y - 50, 20, 100); //draw rectangle

    //*----------------------------Keyboard Controls------------------------
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 10;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 10;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 10;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 10;

    //*-----------If mouse pressed ball resets on top of paddle--------------
    if (mousePressed) {
      x = 715;
      y = paddle2Y;
      dirX = -1;  // add or subtract the speed based on the direction to move the ball
      dirY = -1;
      state = 3;
    }
  }

  //*-------------------Game Play (EASY)-----------------------------
  else if (state == 3) {

    //*----------------------------Keyboard Controls------------------------
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 10;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 10;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 10;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 10;

    fill(255); // Change fill colour
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    //  Check The P1 Paddle
    if (x <= 70 && y >= paddle1Y - 50 &&  y <= paddle1Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += 1;
      p1Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    //  Check The P2 Paddle
    if (x > width - 75 && y >= paddle2Y - 50 &&  y <= paddle2Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += -1;
      p2Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    x += 2*dirX;  // add or subtract the speed based on the direction to move the ball
    y += 2*dirY;

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(x, y, 30, 30);  // draw the ball
    rect(40, paddle1Y - 50, 20, 100);  // draw the paddle

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    rect(740, paddle2Y - 50, 20, 100);  // draw the paddle

    if (x > 740) { // if ball hits right side player 2 loses a life
      p2Life--;
      state = 2;
    }

    if (x < 40) { // if ball hits left side player 1 loses a life
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) { // if player 1 has 0 lives left, then it is game over
      state = 6;
    }
    if (p2Life == 0) { // if player 2 has 0 lives left, then it is game over
      state = 6;
    }


    fill(255); // Change fill colour
    //displays score and lives remaining.
    textSize(30);
    text("P1 Score:" + p1Score + " ", 50, 50);
    text("P1 Lives:" + p1Life + " ", 50, 85);
    text("P2 Score:" + p2Score + " ", 500, 50);
    text("P2 Lives:" + p2Life + " ", 500, 85);
    fill(255, 0);
    stroke(255);
    strokeWeight(2.5);
    rect(50, 23, 173, 30); // draw rectangle
    rect(50, 58, 168, 30); // draw rectangle
    rect(500, 23, 173, 30); // draw rectangle
    rect(500, 58, 168, 30); // draw rectangle
  }

  //*-------------------Game Play (MEDIUM)-----------------------------
  else if (state == 4) {

    //*----------------------------Keyboard Controls------------------------
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 10;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 10;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 10;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 10;

    fill(255); // Change fill colour
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    //  Check The P1 Paddle
    if (x <= 70 && y >= paddle1Y - 50 &&  y <= paddle1Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += 1.2;
      p1Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    //  Check The P2 Paddle
    if (x > width - 75 && y >= paddle2Y - 50 &&  y <= paddle2Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += -1.2;
      p2Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    x += 4*dirX;  // add or subtract the speed based on the direction to move the ball
    y += 4*dirY;

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(x, y, 30, 30);  // draw the ball
    rect(40, paddle1Y - 50, 20, 100);  // draw the paddle

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    rect(740, paddle2Y - 50, 20, 100);  // draw the paddle

    if (x > 740) { // if ball hits right side player 2 loses a life
      p2Life--;
      state = 2;
    }

    if (x < 40) { // if ball hits left side player 1 loses a life
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) { // if player 1 has 0 lives left, then it is game over
      state = 6;
    }
    if (p2Life == 0) { // if player 2 has 0 lives left, then it is game over
      state = 6;
    }


    fill(255);
    //displays score and lives remaining.
    textSize(30);
    text("P1 Score:" + p1Score + " ", 50, 50);
    text("P1 Lives:" + p1Life + " ", 50, 85);
    text("P2 Score:" + p2Score + " ", 500, 50);
    text("P2 Lives:" + p2Life + " ", 500, 85);
    fill(255, 0);
    stroke(255);
    strokeWeight(2.5);
    rect(50, 23, 173, 30);
    rect(50, 58, 168, 30);
    rect(500, 23, 173, 30);
    rect(500, 58, 168, 30);
  }

  //*-------------------Game Play (HARD)-----------------------------
  else if (state == 5) {

    //*----------------------------Keyboard Controls------------------------    
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 10;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 10;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 10;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 10;

    fill(255); // Change fill colour
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "BD.wav");
      file.play();
    }

    //  Check The P1 Paddle
    if (x <= 70 && y >= paddle1Y - 50 &&  y <= paddle1Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += 1.5;
      p1Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    //  Check The P2 Paddle
    if (x > width - 75 && y >= paddle2Y - 50 &&  y <= paddle2Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += -1.5;
      p2Score++; //Adds the score by 1
      // Load a soundfile from the /data folder of the sketch and play it back
      file = new SoundFile(this, "SD.wav");
      file.play();
    }

    x += 6*dirX;  // add or subtract the speed based on the direction to move the ball
    y += 6*dirY;

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(x, y, 30, 30);  // draw the ball
    rect(40, paddle1Y - 50, 20, 100);  // draw the paddle

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    rect(740, paddle2Y - 50, 20, 100);  // draw the paddle

    if (x > 740) { // if ball hits right side player 2 loses a life
      p2Life--;
      state = 2;
    }

    if (x < 40) { // if ball hits left side player 1 loses a life
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) { // if player 1 has 0 lives left, then it is game over
      state = 6;
    }
    if (p2Life == 0) { // if player 2 has 0 lives left, then it is game over
      state = 6;
    }


    fill(255);
    //displays score and lives remaining.
    textSize(30);
    text("P1 Score:" + p1Score + " ", 50, 50);
    text("P1 Lives:" + p1Life + " ", 50, 85);
    text("P2 Score:" + p2Score + " ", 500, 50);
    text("P2 Lives:" + p2Life + " ", 500, 85);
    fill(255, 0);
    stroke(255);
    strokeWeight(2.5);
    rect(50, 23, 173, 30);
    rect(50, 58, 168, 30);
    rect(500, 23, 173, 30);
    rect(500, 58, 168, 30);
  }

  //*-----------------------Game Over-----------------------------
  else if (state == 6) {
    background(255, 0, 0);
    fill(255);
    textSize(80);
    text("GAME OVER", 158, 200);
    fill(0);
    textSize(40);
    text("CLICK TO RESTART", 200, 400);
    if (mousePressed) { // if mouse pressed then the gme if restarted
      state = 0;
    }
  }
}      
Tagged:

Answers

Sign In or Register to comment.