Alright so I got
if ( x >= 1PX + 25 ) - Not sure what else to put.
Printable View
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.
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.
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.
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.
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.
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.
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.Quote:
if the ball hit between the top right to the bottom right of the paddle
How would I go about coding it?
Use the AND operator (&&) to connect all the conditions into a single condition.
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.
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.
Yeah I'm slacked a bit.
But I solved it
Code java:{ 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?
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.