How can i make my the left sided paddle to move like the right sided paddle?

my w and s keys aren't moving like my up and down keys. I want the left sided paddle to move up and down and i expected to be the same as the up and down codes. I recently started processing and I'm genuinely confused. Can someone help me please?

float pongX = 10;
float pongY = 10;
float pongR = 5;
float dX = random(1, 2);
float dY = random(1, 2);
float paddleX;
float paddleY = 200;
float paddleW = 10;
float paddleH = 30;
float paddleXA;
float paddleYA = 200;
float paddleWA = 10;
float paddleHA = 30;

void reset() {
  pongX = 10;
  pongY = 10;
  pongR = 5;
  dX = random(1, 2);
  dY = random(1, 2);
  paddleX = width - 15;
  paddleY = 10;
  paddleW = 10;
  paddleH = 30; }

void setup() {
  size(500, 500);
  paddleX = width - 15;
  paddleXA= width - 495;

}

void draw() {
  background(0);
  ellipse(pongX, pongY, 2 * pongR, 2 * pongR);

  rect(paddleX, paddleY, paddleW, paddleH);
  rect(paddleXA, paddleYA, paddleWA, paddleHA);

  if (pongRight() > width) {
    fill(255);
    rect(0, 0, width, height);
    noLoop();
  }

  if (collision()) {
    dX = -dX; // if dX == 2, it becomes -2; if dX is -2, it becomes 2
  }

  if (pongLeft() < 0) {
    dX = -dX; // if dX == 2, it becomes -2; if dX is -2, it becomes 2
  }

  if (pongBottom() > height) {
    dY = -dY; // if dY == 2, it becomes -2; if dY is -2, it becomes 2
  }

  if (pongTop() < 0) {
    dY = -dY; // if dY == 2, it becomes -2; if dY is -2, it becomes 2
  }

  pongX = pongX + dX;
  pongY = pongY + dY;
}

boolean collision() {
  boolean returnValue = false; // assume there is no collision
  if ((pongRight() >= paddleX) && (pongLeft() <= paddleX + paddleW)) {
    if ((pongBottom() >= paddleY) && (pongTop() <= paddleY + paddleH)) {
      returnValue = true;
    }
  }
  return returnValue;
}

float pongLeft() {
  return pongX - pongR;
}

float pongRight() {
  return pongX + pongR;
}

float pongTop() {
  return pongY - pongR;
}

float pongBottom() {
  return pongY + pongR;
}

void keyPressed() {
if(keyPressed == true) {
    if (key == 'r' || key == 'R') {
    reset();
  }
    if (key == CODED) {
      if(keyCode == UP) {
        if (paddleY >= 0) {
          paddleY = paddleY - paddleH*0.2;
        }
      }
      if(keyCode == DOWN) {
        if(paddleY <= height - paddleH) {
          paddleY = paddleY + paddleH*0.2;
        }
      }
        if(keyCode == 'w') {
        if (paddleYA >= 0)   {
          paddleYA = paddleYA - paddleHA*0.2;
        }
      }
      if(keyCode == 's') {
        if(paddleYA <= height - paddleHA) {
          paddleYA = paddleYA + paddleHA*0.2;
        }
      } 
}
}
}
Tagged:

Answers

  • Answer ✓

    Not sure, but shouldn't the ifs for w and s be outside the big of CODED?

  • W and s is not coded

    Hit ctrl-t to get a better indent automatically

  • edited March 2017 Answer ✓

    Like this perhaps?

    if (keyPressed) {
      if (key == 'r' || key == 'R') {
        reset();
      }
      if (key == 'w') {
        if (paddleYA >= 0) {
          paddleYA = paddleYA - paddleHA*0.2;
        }
      }
      if (key == 's') {
        if (paddleYA <= height - paddleHA) {
          paddleYA = paddleYA + paddleHA*0.2;
        }
      }
      if (key == CODED) {
        if (keyCode == UP) {
          if (paddleY >= 0) {
            paddleY = paddleY - paddleH*0.2;
          }
        }
        if (keyCode == DOWN) {
          if (paddleY <= height - paddleH) {
            paddleY = paddleY + paddleH*0.2;
          }
        }
      }
    }
    
  • Thank you, it works fine now

Sign In or Register to comment.