Question

I'm having trouble with a Ping Pong program we're supposed to make for my java class. We were giving the documents and it needs editing. its and automatic ping pong game so it does require any input, it's all by chance by the computer. I just need the paddle to go up and down. I've gotten it to move and everything.

My problem here is the Y range is from 20 - 465. It counts down from 465 to 20, then instead of counting up from 20 to 465, it just jumps all the way up to 465 again and counts down from 20, thereby making the pad disappeared from the bottom to the top pretty fast. I'm going to attach the project in case anyone wants to check it out for me.

I'm not just looking for someone to help me figure out why its jumping and not counting up. here's the code if the problem can be figure out from it alone. I'm working with the edgeCheck and Update methods.

import java.awt.Point;
import javax.swing.ImageIcon;
public class CharlesPaddle extends Paddle {
    /************************************************** ***************/
    public CharlesPaddle(int _x) {
        super(_x);
        ImageIcon iiPaddle = new ImageIcon(this.getClass().getResource("paddle.png"));
        paddleImage = iiPaddle.getImage();
    } //end CharlesPaddle
    /************************************************** ***************/
    public void update() {
        if (edgeCheck() == false)
            do {
                paddleLocation.y -= 1;
                System.out.println(paddleLocation.y);
            }
            while (paddleLocation.y >= 21);
        else if (edgeCheck() == true)
            do {
                paddleLocation.y += 1;
            }
            while (paddleLocation.y <= 465);

    } //end update

    /************************************************** ***************/
    public void init() {
        paddleLocation = new Point();
        paddleVector = new Point();
        paddleVector.x = 0;
        paddleVector.y = speed;
        paddleLocation.x = xLocation;
        paddleLocation.y = 200;
    } //End init
    /************************************************** ***************/
    public boolean edgeCheck()
    {

        boolean check = false;
        if (paddleLocation.y >= 465)
            check = false;
        else if (paddleLocation.y <= 20)
            check = true;
        return check;

    } // End edgeCheck
}
Was it helpful?

Solution

I'm not just looking for someone to help me figure out why its jumping and not counting up.

Well, it would be too hard for me to figure out anyway.

You have two options as I see it

Sin function (better)

If your top is 465, and your bottom is 20, then your amplitude should be (465 - 20) / 2, or 222.5. Your y-intersect should be your amplitude plus the bottom, so 242.5. Declare a double t, and do this in your update:

public void update() {
    paddleLocation.y = (int)((222.5) * Math.sin(t) + (242.5))
    t += Math.PI * .01; // This number is arbitrary

} 

Fix your code (I probably missed something, but try it out)

Your code is really sloppy and unintuitive, sorry. But I'll try my best to direct you.

I think the problem is that you have an unnecessary loop inside of your update method. This is causing the "jump". The update method should be called frequently, and you're just acting on one variable here, so you don't need the loop. Instead, try this:

import java.awt.Point;
import javax.swing.ImageIcon;
public class CharlesPaddle extends Paddle {
    /************************************************** ***************/
    public CharlesPaddle(int _x) {
        super(_x);
        ImageIcon iiPaddle = new ImageIcon(this.getClass().getResource("paddle.png"));
        paddleImage = iiPaddle.getImage();
    } 
    /************************************************** ***************/
   public void update() {
        goingUp = updateDirection(goingUp);
        if (goingUp)
            paddleLocation.y += 1;
        else
            paddleLocation.y -= 1;

        System.out.println("y position: " + paddleLocation.y);
    }

    /************************************************** ***************/
    public void init() {
        paddleLocation = new Point();
        paddleVector = new Point();
        paddleVector.x = 0;
        paddleVector.y = speed;
        paddleLocation.x = xLocation;
        paddleLocation.y = 200;
    } 
    /************************************************** ***************/
    public boolean updateDirection(boolean goingUp)    // This name makes more sense
    {
        if (goingUp && paddleLocation.y >= 465)      // It's just peaked,
            return false;                            // so return false (going down)
        else if (!goingUp && paddleLocation.y <= 20) // It's just gone to the bottom,
            return true;                             // so return true (going up)
        else                                         // It's somewhere in between,
            return goingUp;                          // so don't change anything!             
    }
}

Either of those should work.

I renamed your methods and variables because they didn't make sense. I also removed some of your redundant commenting.

In your main method, you should then call this update() repeatedly with either a for or while loop. You can also print out the paddleLocation in your update method, or do whatever you want with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top