Add {}s after the if and put the println inside the {}s
Printable View
Add {}s after the if and put the println inside the {}s
if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S {System.out.println (x_inc)}; ?
Did that and I just see 1 when its moving to the right, and -1 when its moving to the left.
You left the assignment statement outside of the {}s
Code :if (..) { x_inc = -1*S ; System.out.println ("x_inc=" +x_inc); }
Do you know what makes the value change and how that changes the direction of movement?
Ermm
Is it, if x is greater than the width - border offset - hall ball size it changes direction? and gives -1 values to x until it reaches the other side
So if x > 790 it reverses and gives x values -1?
Describe the place where x's value is reversed using terms of the shapes, not numeric values,
Do NOT code 790. Use the values of variables. I'm testing with a window that is 300x400 defined using the W and H variables.
What variable? x is the location of the ball.Quote:
gives x values -1?
x >= W - Halfballsize
I know x_inc gives it a -1, but I don't understand how it does that.
x_inc gives what a -1?
Can you explain what variable? How does x_inc give the other variable a -1?
Think about how the position of the ball changes. What should happen when the ball gets to a side?
Just work with x values for now. How does the x value change as the ball moves to the right? What will be different when the ball moves to the left?
When it goes to the right it gets 1 which adds to the x value, while going to the left gives it a -1
It's not really 1, its the value of the variable: S. Change S's value and you'll see.
When does the sign of S change from + to - and from - to +?
Oh is that what S is for....... Yeah I changed S to 5 and when it hit a wall it sped up.
The S changes depending on the condition that is met I'm guessing.
As it is -1 or 1 * S.
The code is poorly written when it comes to meaningful variable names.
When does the sign of x_inc change?
if ( x >= W-B-HALF_BALL_SIZE ) -1
if ( x <= 0+B+HALF_BALL_SIZE ) 1
When either of these conditions are met.
Yeah I was given it and had to make a pong game, the only parts I added were the paddles.
Can you explain in English to show that you understand what the code is doing.
if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S;
if x is greater or equal to the width - border offset - halfballsize, it would then call forth x_inc and multiply S by -1. Therefore creating a making the x_inc = -1
What visual thing do all those numbers represent?
Now think in terms of the paddles. When would you want to change the sign of x_inc if the ball is moving to the left towards paddle1?
When it touches the right side of paddle 1.
I'm not sure what varibles to put in there, would I be using p1X ?
Go to the paper and pencil diagram I asked you to draw. Label it with the variables that define the locations of the paddle and the ball. Then look at that drawing to get the variable names you need in the code.
X variables for paddle is p1X , Y is p1Y.
Ball position is x and y?
Also what does this part mean?
x += x_inc;
y += y_inc;
Thanks for the help btw. Might as well learn how to understand it, but short on time, so hope it gets done soon.
What does a change to the value of x do?
Hmm? Don't understand that question.
What is the variable: x used for in the program? What happens visually when the value of x is changed?
The ball position
Does that answer your question asked in post#43?
Was wondering what the += means.
Also how would I get started on collision detecting the paddle and the ball?
Is it similar to the layout of the wall collision?
Yes, look at the diagram you've drawn on paper and get the variable names from that.Quote:
Is it similar to the layout of the wall collision
var += val is shorthand notation for var = var + val.