Name: Anonymous 2012-02-12 11:08
It takes ProgrammingForums.org days to respond to anything, so I'll just post this here.
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?
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?