Simple pong issue with right paddle return

Is anyone able to tell me where I've went wrong with the right paddle and why it is not returning the ball like the left one is?

The code is very simple and needs to be followed this way for a project.

int leftPaddleY;
int rightPaddleY; int ballX, ballY;
int ballSpeedX = 5;

int scorel = 0; int scorer = 0;

void setup() { fullScreen(); fill(255); noStroke();

leftPaddleY = height / 2; rightPaddleY = height / 2;

ballX = width / 2; ballY = height / 2;

textSize(30); }

void draw() {

background(0);

rect(150, leftPaddleY, 25, 150); rect(1150, rightPaddleY, 25, 150);

rect(ballX, ballY, 25, 25);

ballX = ballX - ballSpeedX;

if (ballX < 0 || ballX > width) { ballX = width / 2; }

int leftPaddleLeftMargin = 150;
int rightPaddleRightMargin = 150; int leftPaddleRightMargin = 150 + 25; int rightPaddleLeftMargin = 150 + 25; int leftPaddleTopMargin = leftPaddleY;
int rightPaddleTopMargin = rightPaddleY; int leftPaddleBottomMargin = leftPaddleY + 150; int rightPaddleBottomMargin = rightPaddleY + 150;

if (ballX > leftPaddleLeftMargin && ballX < leftPaddleRightMargin) { if (ballY >= leftPaddleTopMargin && ballY < leftPaddleBottomMargin) { ballSpeedX = ballSpeedX * -1; } }

else if (ballX > rightPaddleRightMargin && ballX < rightPaddleLeftMargin) { if (ballY >= rightPaddleBottomMargin && ballY < rightPaddleTopMargin) { ballSpeedX = ballSpeedX * -1; } }

scores ();

}

void scores () { fill(255); text(scorel, 100, 50); text(scorer, width-200, 50);

} void keyPressed() { if (key == 'w') { leftPaddleY = leftPaddleY - 25; } else if (key == 's') { leftPaddleY = leftPaddleY + 25; }

if (key == 'o') 

{ rightPaddleY = rightPaddleY - 25; } else if (key == 'l') { rightPaddleY = rightPaddleY + 25; } }

Answers

  • Answer ✓

    int rightPaddleTopMargin = rightPaddleY;
    int rightPaddleBottomMargin = rightPaddleY + 150;


    Let's say the rightPaddleY is... 200.

    int rightPaddleTopMargin = 200;
    int rightPaddleBottomMargin = 200 + 150; // 350


    if (ballY >= rightPaddleBottomMargin && ballY < rightPaddleTopMargin) { 
    

    Or:

    if ( ballY >= 350 && ballY < 200 ) { 
    

    This condition doesn't evaluate to true in this case. There is no number that is both greater than 350 and less than 200.

    In fact, it will never evaluate to true, because there is no number that is greater than rightPaddleY+150 and also less than rightpaddleY! Thus the bounce condition is never satisfied and the bounce never occurs.

  • Thanks for that. :)

Sign In or Register to comment.