Results 1 to 2 of 2
  1. #1
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with rebound function

    Hi, 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:
    Java Code:
    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 guess
    paddle1_y you can also guess...
    Now i did not have a radius for my paddles so the paddle stretches 40 y down and 10 x in width... so without any further ado, heres the code:

    Java 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 doesn't work?

    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public void bounceToLeft(){
        if(y_pos + 2*radius > rightPaddle_y &&            // ball will hit top of paddle
           y_pos < rightPaddle_y + rightPaddle_height &&  // ball will hit paddle bottom
           x_pos + 2*radius > rightPaddle_x)              // ball hits paddle side
            x_speed *= -1;                                // toggle speed direction
    }
    
    public void bounceToRight() {
        if(y_pos + 2*radius > leftPaddle_y &&                 // hit top of paddle
           y_pos < leftPaddle_y + leftPaddle_height &&        // hit paddle bottom
           x_pos + x_speed < leftPaddle_x + leftPaddle_width) // hit paddle side
            x_speed *= -1;
    }

Similar Threads

  1. use of onclick function
    By m4tt in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:03 AM
  2. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:33 PM
  3. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:25 AM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM
  5. Help with the Dummy Function
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-06-2007, 05:03 PM

Posting Permissions

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