Alright so I got
if ( x >= 1PX + 25 ) - Not sure what else to put.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Alright so I got
if ( x >= 1PX + 25 ) - Not sure what else to put.
Do NOT use 25. Use the variable that defines the value.
If you don't understand my answer, don't ignore it, ask a question.
That means I have to change the
g.setPaint( Color.black );
g.fillRect( p1X, p1Y, 25, 60);
values?
It allows you to easily change the way the code works. Say you want a longer, thinner paddle. Change the values of two variables and the new version works.
If you don't understand my answer, don't ignore it, ask a question.
Still can't get around how to collide with the ball and paddle.
I've changed the rect to
g.setPaint( Color.black );
g.fillRect( p1X, p1Y, p1Width, p1Height);
Not sure how the format for the if statement goes, because the paddle moves.
The paddle's location is defined by the values of variables. Look at the diagram you drew on paper.
detect one collision at a time: The ball moving to the left towards paddle1
Don't write any code until you can describe what the conditions are for a collision.
If you don't understand my answer, don't ignore it, ask a question.
Well I know it has to hit the right side of the left paddle.
This would require p1X + p1Width since that would equate to the top right side of it.
Do I have to also include the pPU? Since it is moving......I'm so confused
Forget about the paddle's moving for now.
You x value for the paddle's right side looks reasonable. Now you need to see if the paddle's y values overlap where the ball is moving. The ball could go above or below the paddle.
If you don't understand my answer, don't ignore it, ask a question.
Then the y value would be
p1Y + p1Height
Would I need to write seperate things for the x and y values? Or could they be incased in the same ()?
Can you describe the tests to be made for the ball at the same y values as the paddle?
What is the purpose of the expression you posted? It gives 1 y value. The paddle has a top and a bottom.
If you don't understand my answer, don't ignore it, ask a question.
So the code should look something like
if ( x >= p1X+p1Width+HALF_BALL_SIZE ) x_inc = -1*S;
if ( y >= p1Y+p1Height+p1Width+HALF_BALL_SIZE ) y_inc = -1*S;
if ( y >= p1Y+HALF_BALL_SIZE ) y_inc = -1*S;
But if the ball hits the top if stays up there, and the border is locked at the width so its stuck in a small rectangle box.
Can you explain in English what you are trying to write the code to test for?
Your 3 independent if tests do not test the condition correctly.
If you don't understand my answer, don't ignore it, ask a question.
I'm trying to make it so when the ball hits the paddle, it bounces off it. I read about the intersect code but I'm not sure how that works.
So the whole of the paddle should make it bounce back.
So if the ball hit between the top right to the bottom right of the paddle, it will bounce off.
The tests for the location of the ball must all be true. Your 3 if tests were independent of each other.if the ball hit between the top right to the bottom right of the paddle
If you don't understand my answer, don't ignore it, ask a question.
How would I go about coding it?
Use the AND operator (&&) to connect all the conditions into a single condition.
If you don't understand my answer, don't ignore it, ask a question.
No clue on how to use that. My knowledge of java programming is very low.
Then you are trying to run before learning to walk.
Look in the code for &&. It is used in several places.
Last edited by Norm; May 13th, 2012 at 02:55 PM.
If you don't understand my answer, don't ignore it, ask a question.
Hokap (May 13th, 2012)
It was just a code I found that created a paddle.
Oh well, guess I'll send the code like this, only have two hours left before submitting time.
Guess its closed.
Thanks for the help Norm.
Even though I didn't get anything accomplished.
Good luck learning java. You started a little late with so much to learn to get this done so soon.
If you don't understand my answer, don't ignore it, ask a question.
Yeah I'm slacked a bit.
But I solved it
{ if ( x <= p1X+p1Width && x >=p1X) if (y >=p1Y && y <= p1Y+p1Height ) { x_inc = 1*S; y_inc = 1*S; } }
Just figuring out how to make it turn direction on how it hits the box.
How do you know you want to change the y_inc value?
If you don't understand my answer, don't ignore it, ask a question.
Because whenever it hits the paddle box, it goes down every time. I'm wondering if theres a way that the ball hits the paddle makes it go up or down. But if it requires a bit more work I guess I'll leave it.