Help with Java Pong Game

Soldato
Joined
26 Aug 2005
Posts
6,901
Location
London
Hi guys,

Am abit stuck with some logic. Basically if the ball hits the walls it will change direction obviously. However, I can't get get the ball to change direction when the ball hits the paddle.
Code:
if(inPlay)
{
		if( (x==0) || (x== getWidth() - size) || (x>box.getWidth()+batWidth && x<box.getWidth()+batWidth)  )
		{
			xd *= -1;
		}
		if( (y==20) || (y == getHeight() - size) ||  (y>box.getHeight()+batWidth && y<box.getHeight()+batWidth)    )
		{
			yd *= -1;
		}
			x+= xd;
			y += yd;
}
 
triggerthat said:
Hi guys,

Am abit stuck with some logic. Basically if the ball hits the walls it will change direction obviously. However, I can't get get the ball to change direction when the ball hits the paddle.
Code:
if(inPlay)
{
		if( (x==0) || (x== getWidth() - size) || (x>box.getWidth()+batWidth && x<box.getWidth()+batWidth)  )
		{
			xd *= -1;
		}
		if( (y==20) || (y == getHeight() - size) ||  (y>box.getHeight()+batWidth && y<box.getHeight()+batWidth)    )
		{
			yd *= -1;
		}
			x+= xd;
			y += yd;
}

You're adding batWidth to box.getHeight()... that seems wrong... unless your bat is square?? Also (y>box.getHeight()+batWidth && y<box.getHeight()+batWidth) will always return false -- it's impossible for y to both be greater than and less than a number at the same time. :p

That clause should probably be something like

Code:
(y > box.getHeight() - batHeight - spaceBetweenBatAndBottomOfField && y < box.getHeight() - spaceBetweenBatAndBottomOfField)

HTH
 
Something like this? I've tried it, but it doesn't like..

Code:
((x > box.getWidth() - batWidth - getWidth()/9) && (x < box.getWidth() - batWidth))

((y > box.getHeight() - batHeight - getHeight()/9) && (y < box.getHeight() - getHeight()/9)
 
triggerthat said:
Something like this? I've tried it, but it doesn't like..

Code:
((x > box.getWidth() - batWidth - getWidth()/9) && (x < box.getWidth() - batWidth))

((y > box.getHeight() - batHeight - getHeight()/9) && (y < box.getHeight() - getHeight()/9)

Some more information about the error you're getting would be helpful... (such as the exact error message...)

What is getWidth() and getHeight() above methods of? (I'm not talking about the reference to box.getWidth() or box.getHeight(), I'm talking about the reference to the methods without any object indicator (presumably refering to the "self" or "this" object (whatever it is in Java, I forget which.)
 
Back
Top Bottom