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-17 3:39

pseudo-code  (dont know java)

if(paddle.y >= 480)
{
 paddle.y = 480; //same for other positions
}

also the "delay" with movement after an keypress is because of as you already said.. windows...
you can however avoid this by doing something like this

function "your key down handler"
{
  speedY = 5;
}

function Update()
{
   paddle.y += speedY;
}

pleasure to help you

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