Paddle For Pong

So im making a pong game and i have the code for the Right paddle to have the ball get bounced off. Then i copied that code and made some changes for it to "work" for the left paddle. Then i removed the collision for the left and right walls to see if the left paddle worked but i guess it was not. But im not sure what i did wrong. Any help would be great!

float x = 300;
float y = 300;
float velocityX = 2;
float velocityY = 3;
////////////////////
float paddleY;
float paddleX = 20;
float paddleW = 20;
float paddleH = 100;
////////////////////
float ai;
float paddleY2 = 20;
float paddleX2 = 570;
float paddleW2 = 20;
float paddleH2 = 100;

void setup() {
  size(600,680);
}

void draw() {
  x += velocityX;
  y += velocityY;

  background(20);  
  noStroke();
  fill(255);
  rect(x,y,20,20);
  rect(paddleX,paddleY,paddleW,paddleH);
  rect(paddleX2,ai,paddleW2,paddleH2);
  fill(100);
  strokeWeight(2);
  stroke(255);
  rect(-1,0,601,80);
  rect(-1,600,601,80);
  //println(player);
  //println(ballY);
  ai = y-50;
  //paddleY = y-50;

  if (x <= -20) {
    x = 100;
    y = 100;
  } else if (x >= 600) {
    x = 100;
    y = 100;
  }

  /*if (x > 580 || x < 0) {
    velocityX *= -1;
  }*/
  if (y > 580 || y < 80) {
    velocityY *= -1;
  }

  if (x - 20/2 < paddleX + paddleW/2 && y - paddleH/2 < paddleY + 100/2 && y + 100/2 > paddleY - paddleH/2) {
    velocityX /= -1;

  }

  if (x + 20/2 > paddleX2 - paddleW2/2 && y - paddleH2/2 < paddleY + 100/2 && y + 100/2 > paddleY2 - paddleH2/2) {
    velocityX /= -1;

  }
}
Tagged:

Answers

Sign In or Register to comment.