Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How would I be able to add a sound when the ball hits the paddle or wall?

$
0
0
float x = 700;
float y = 350;
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;

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



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

  //*-------------------Menu---------------------------------
  if (state == 0) {
    fill(255);
    textSize(45);
    text("Pong Assignment by Nathaniel K.", 36, 100);
    text("Press 'E' For Easy", 220, 300);
    text("Press 'M' For Medium", 180, 400);
    text("Press 'H' For Hard", 220, 500);
    fill(255, 0);
    stroke(255);
    strokeWeight(5);
    rect(215, 255, 378, 58);
    rect(175, 355, 469, 58);
    rect(215, 455, 393, 58);
    p1Life = 10;
    p2Life = 10;

    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) {
    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(85, paddle1Y, 30, 30);
    rect(40, paddle1Y - 50, 20, 100);
    rect(740, paddle2Y - 50, 20, 100);

    if (mousePressed) {

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

        paddle1Y = paddle1Y - 9;

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

        paddle1Y = paddle1Y + 9;

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

        paddle2Y = paddle2Y - 9;

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

        paddle2Y = paddle2Y + 9;

      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) {
    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(715, paddle2Y, 30, 30);
    rect(40, paddle1Y - 50, 20, 100);
    rect(740, paddle2Y - 50, 20, 100);

    if (mousePressed) {

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

        paddle1Y = paddle1Y - 9;

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

        paddle1Y = paddle1Y + 9;

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

        paddle2Y = paddle2Y - 9;

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

        paddle2Y = paddle2Y + 9;

      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) {
    if (keyPressed && key == 'w')

      paddle1Y = paddle1Y - 9;

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

      paddle1Y = paddle1Y + 9;

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

      paddle2Y = paddle2Y - 9;

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

      paddle2Y = paddle2Y + 9;

    fill(255);
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
    }

    //  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
    }

    //  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
    }

    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) {
      p2Life--;
      state = 2;
    }
    if (x < 40) {
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) {
      state = 6;
    }
    if (p2Life == 0) {
      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 (MEDIUM)-----------------------------
  else if (state == 4) {
    if (keyPressed && key == 'w')

      paddle1Y = paddle1Y - 9;

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

      paddle1Y = paddle1Y + 9;

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

      paddle2Y = paddle2Y - 9;

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

      paddle2Y = paddle2Y + 9;

    fill(255);
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
    }

    //  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
    }

    //  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
    }

    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) {
      p2Life--;
      state = 2;
    }
    if (x < 40) {
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) {
      state = 6;
    }
    if (p2Life == 0) {
      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) {
    if (keyPressed && key == 'w')

      paddle1Y = paddle1Y - 9;

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

      paddle1Y = paddle1Y + 9;

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

      paddle2Y = paddle2Y - 9;

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

      paddle2Y = paddle2Y + 9;

    fill(255);
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
    }

    //  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
    }

    //  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
    }

    x += 8*dirX;  // add or subtract the speed based on the direction to move the ball
    y += 8*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) {
      p2Life--;
      state = 2;
    }
    if (x < 40) {
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) {
      state = 6;
    }
    if (p2Life == 0) {
      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) {
      state = 0;
    }
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles