Search code examples
javapong

Pong Paddle Movement Issues


I'm working on a Pong game for my portfolio right now in Java, and I'm just having a slight problem with the Pong paddle movement. Obviously, I don't want the paddles moving off-screen, so I'm trying to make it so when it reaches a certain point on its Y-axis, it will not move any farther. I've gotten it to work for the top of the screen, but not the bottom, and I can't figure out why. The conditional I'm using SHOULD work, but it will just move right off the bottom of the screen if I go far enough. Here's the conditionals for each paddle's movement. If you need more of my code, just ask.

// Check for Left Paddle movement.
if( ( wKey ) && ( paddle_left.getY() >= ( paddle_speed * 2 ) ) )
    paddle_left.setY( paddle_left.getY() - paddle_speed ); 
else if( ( sKey ) && ( paddle_left.getY() <= ( boardHeight - 10 ) ) )
    paddle_left.setY( paddle_left.getY() + paddle_speed );

// Check for Right Paddle movement
if( ( upKey ) && ( paddle_right.getY() >= ( paddle_speed * 2 ) ) )
    paddle_right.setY( paddle_right.getY() - paddle_speed );
else if( ( downKey ) && ( paddle_right.getY() <= ( boardHeight - 10 ) ) )
    paddle_right.setY( paddle_right.getY() + paddle_speed );

The variable paddle_speed is a constant equal to 5. Also, boardHeight is the height of the screen on which everything is drawn, which is equal to 480.

Also, this isn't as important, but I've noticed a slight delay from when I start to hold down the key to when the paddle starts moving. I know it's because of the initial key press delay set on the user's computer, but how can I compensate for this?

EDIT: Ok, I changed my code to reflect your answer and I've got this now:

// Check for Left Paddle movement.
if( ( wKey ) && ( paddle_left.getY() >= ( paddle_speed * 2 ) ) ) paddle_left.setY( paddle_left.getY() - paddle_speed );
else if( ( sKey ) && ( paddle_left.getY() <= ( boardHeight - ((paddle_speed * 2) + paddleHeight) ) ) ) paddle_left.setY( paddle_left.getY() + paddle_speed );

// Check for Right Paddle movement.
if( ( upKey ) && ( paddle_right.getY() >= ( paddle_speed * 2 ) ) ) paddle_right.setY( paddle_right.getY() - paddle_speed );
else if( ( downKey ) && ( paddle_right.getY() <= ( boardHeight - ((paddle_speed * 2) + paddleHeight) ) ) ) paddle_right.setY( paddle_right.getY() + paddle_speed );

However, it still goes a little below screen. It does stop now, but it goes maybe 20 pixels beyond the screen still. I don't get it. I even used Paint and drew the coordinates and simulated how the logic should work, and it should stop 5 pixels from the edge of the bottom of the screen. Can you figure out why it's not?


Solution

  • It's probably because you don't take the size of your paddle into account when checking for the bottom of the screen. So if your paddle is bigger than your speed size (5) then it will be draw below the bottom of the screen.

    For the key repeat delay problem you will probably have to keep flags for which keys are down. Set the flags on keyPressed() events and clear it on keyReleased() and then update the paddles using a timer.