Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pong Paddle Movement in Java

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?

Name: Anonymous 2012-02-12 11:59

So, it seems that 10 the paddle length (I don't know why you use speed*2 for setting the upper limit while you use 10 directly for the downward one).

For what i can see, the error may be that the Y you use for the paddle is the coordinate of the top part. If it is so, you have that you can go up until the top part has y=10 (so it stops right at the top), while if you press down when y=470 the paddle goes to y=475, which means that half of it goes offscreen.

If it is not that, check you haven't misassigned the coordinates (confused X with Y?).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List