...

rebound function not working with java

warhammerdude20
03-01-2004, 04:44 PM
i am trying to make a pong game in java and this is the rebound function for the left paddle. now just to set a few variables:

x_pos is the balls x position
y_pos is the balls y position
x_speed is the x speed of the ball
radius is only for the ball and is the balls radius.
paddle1_x you can quess
paddle1_y you can also guess...

now i did not have a radius for my paddles so the paddle streches 40 y down and 10 x in width... so without any further ado, heres the code:


public void bounceToLeft(){
if(x_pos-radius<=paddle1_x+10&&x_pos-radius>=paddle1_x){
if(y_pos<=paddle1_y-40&&y_pos>=paddle1_y){
x_speed=-1;
}
}
}


i call this function in the run method of the applet. does anyone know why it doesnt work?

thanks

shmoove
03-01-2004, 08:08 PM
I think your problem is here:

if(y_pos<=paddle1_y-40&&y_pos>=paddle1_y){

That condition is a little hard (and I mean impossible) to satisfy.
Don't you mean either:

if(y_pos>=paddle1_y-40&&y_pos<=paddle1_y){

or:

if(y_pos<=paddle1_y+40&&y_pos>=paddle1_y){


In any case, when you run into troubles like this one System.out.println() (or a good debugger) can be your best friend:

public void bounceToLeft(){
System.out.println("Entered bounceToLeft()");
System.out.println("x_pos=" + x_pos + ",y_pos=" + y_pos);
System.out.println("paddle1_x=" + paddle1_x + ",paddle1_y=" + paddle1_y);
System.out.println("(x_pos-radius<=paddle1_x+10&&x_pos-radius>=paddle1_x)=" + (x_pos-radius<=paddle1_x+10&&x_pos-radius>=paddle1_x));
System.out.println("(y_pos<=paddle1_y-40&&y_pos>=paddle1_y)=" + (y_pos<=paddle1_y-40&&y_pos>=paddle1_y));
if(x_pos-radius<=paddle1_x+10&&x_pos-radius>=paddle1_x){
if(y_pos<=paddle1_y-40&&y_pos>=paddle1_y){
System.out.println("All the conditions were met - bouncing to left");
x_speed=-1;
}
}
}

I bet that would've helped you figure it out on your own.

shmoove

warhammerdude20
03-02-2004, 06:51 PM
well gosh darn jeez,

shoove, thanks a heapin heap. i was minusing 40 instead of plusing it... now ive made my first pong game!


YEEEE HAAAAW!


great forum



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum