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

Pages: 1-

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:15

boardHeight is wrong?

as for the delay, it depends on the kind of notifications you're getting, if you were polling the keyboard matrix then this problem doesn't exist

Name: Anonymous 2012-02-12 11:26

What do you mean "boardHeight" is wrong? It's the same value I used when I used setSize() on the JFrame.

Name: Anonymous 2012-02-12 11:26

Also, what does "polling the keyboard matrix" mean?

Name: Anonymous 2012-02-12 11:46

Post it in Stack Overflow.

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?).

Name: Anonymous 2012-02-12 12:04

It's not going off-screen by a little bit. It continues to move off-screen until it's not longer visible. Also, it moves up by 5 per tick, not 10. I did 10 so that it will stop just 5 pixels short of the edge of the screen.

And no, I haven't confused X and Y...

Name: Anonymous 2012-02-12 12:09

Eh, if there isn't any problem with coordinates assignments I can't figure it out, sorry, hope someone else will be more helpful.

Name: kodak_gallery_programmer !!qmiXqQhekkGXVVD 2012-02-12 14:00

>>6
Haven't you ever been subjected to the "drawing triangles" or "drawing simple geomtric patterns" with a combination of asterisks and spaces exercises in any programming class?

Name: Anonymous 2012-02-12 14:14

Well, StackOverflow was certainly helpful, and I feel like an idiot for not seeing this before. I neglected to take the size of the paddle into account, considering the origin point that it is checking for collision with is at the top of the paddle.

Name: Anonymous 2012-02-12 14:21

>>10
No one knows this because you didn't post any kind of complete working code. I hate to tell you this, but some of us can't read the mind of an idiot. Now go away. You suck.

Name: Anonymous 2012-02-12 14:25

>>9

6 here.
No, why you asked? Anyway the problem was the one i said, maybe i explained it badly, sorry.

Name: Anonymous 2012-02-12 14:26

>>11

I said that I would post the rest of the code if asked. Nobody asked. Fuck off.

Name: Anonymous 2012-02-12 14:31

>>13
Usually most people would post some complete working code the first time around instead of posting "relevant" code snippets. I believe this is what as known as being clear and concise.

Perhaps you should take a break from your shit programming, and insted, take a class on basic english composition.

Name: Anonymous 2012-02-12 16:29

insted

Name: Anonymous 2012-02-12 19:32

Fuck off.
Inviting to being helped.

I neglected to take the size of the paddle into account
laughing_gabe_newells.jpg

Name: Anonymous 2012-02-12 19:48

>>16
polecat kebabs

Name: Fuck off, !Ep8pui8Vw2 2012-02-12 19:55

>>17
Fuck off, ``faggot''.

Name: Anonymous 2012-02-13 2:58

>>10
That is what >>6 said you fucking moron!

Name: Anonymous 2012-02-13 13:00

>>19
Go scrub another toilet princess.

Name: Anonymous 2012-02-17 1:21

>>22
nice dubs bro

Name: Anonymous 2012-02-17 2:12

>>21
thanks man

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

Name: Anonymous 2012-02-18 10:42

>>23
Go away. Go far far away. And then, don't come back you fucking moron.

Name: Anonymous 2012-02-18 11:05

I can highly suggest using Min and Max function to make sure you don't go out of bounds

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