Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    rebound function not working with java

    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:

    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

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think your problem is here:
    Code:
    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:
    Code:
    if(y_pos>=paddle1_y-40&&y_pos<=paddle1_y){
    or:
    Code:
    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:
    Code:
    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

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •