-1

i want to create a Pong game. I want to move the ball in a way until it touch a wall. if it touch the wall, it go the other way. The problem is that when I start playing, the ball goes in the right way but when it touch the wall, the ball direction reverse but only for one pixel so the ball reverse for 1 pixel and then the direction change angain and it touch the wall again. My code for the moving ball is in the initBall method. Please help me :(

here is my playPanel class :

private int posX = SCREEN_WIDTH / 2;
private int posY;

public Point posMouse = new Point();
private Point posBall = new Point();

private int playPanelWidth;
private int playPanelHeight;

private int padPanelWidth;
private int padPanelHeight;

private int panPanelWidth;
private int panPanelHeight;

private JLabel player1Score = new JLabel("0");
private JLabel ComputerScore = new JLabel("0");

private JPanel panPlayer1;
public JPanel panComputer;

public JPanel padPlayer1;
public JPanel padComputer;

private JButton but_Escape = new JButton("Press escape to continue !");

/*
 * Constructor
 */
// ==============================================
public PlayPanel() {
    super(new BorderLayout());
    setBackground(PANPLAY_COLOR);

    panPlayer1 = new JPanel();
    panComputer = new JPanel();

    padPlayer1 = new JPanel();
    padComputer = new JPanel();

    padPlayer1.setBackground(Color.DARK_GRAY);
    padComputer.setBackground(Color.DARK_GRAY);

    padPlayer1.setPreferredSize(PADPANEL_SIZE);
    padComputer.setPreferredSize(PADPANEL_SIZE);

    panPlayer1.setBackground(PANPLAY_COLOR);
    panComputer.setBackground(PANPLAY_COLOR);

    panPlayer1.add(padPlayer1);
    panComputer.add(padComputer);

    add(panPlayer1, BorderLayout.WEST);
    add(panComputer, BorderLayout.EAST);

    addMouseMotionListener(this);

    panPlayer1.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {
            setPanPanelWidth(arg0.getComponent().getSize().width);
            setPanPanelHeight(arg0.getComponent().getSize().height);
        }

    });

    addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {

            setPlayPanelWidth(arg0.getComponent().getSize().width);
            setPlayPanelHeight(arg0.getComponent().getSize().height);
        }

    });
}

/*
 * Setters and Getters
 */
// ==============================================

public int getPosX() {
    return posX;
}

public void setPosX(int posX) {
    this.posX = posX;
}

public int getPosY() {
    return posY;
}

public void setPosY(int posY) {
    this.posY = posY;
}

public JPanel getPanPlayer1() {
    return panPlayer1;
}

public void setPanPlayer1(JPanel panPlayer1) {
    this.panPlayer1 = panPlayer1;
}

public JPanel getPanComputer() {
    return panComputer;
}

public void setPanComputer(JPanel panComputer) {
    this.panComputer = panComputer;
}

public int getPlayPanelHeight() {
    return playPanelHeight;
}

public void setPlayPanelHeight(int playPanelHeight) {
    this.playPanelHeight = playPanelHeight;
}

public int getPlayPanelWidth() {
    return playPanelWidth;
}

public void setPlayPanelWidth(int playPanelWidth) {
    this.playPanelWidth = playPanelWidth;
}

public int getPadPanelWidth() {
    return padPanelWidth;
}

public void setPadPanelWidth(int padPanelWidth) {
    this.padPanelWidth = padPanelWidth;
}

public int getPadPanelHeight() {
    return padPanelHeight;
}

public void setPadPanelHeight(int padPanelHeight) {
    this.padPanelHeight = padPanelHeight;
}

public int getPanPanelWidth() {
    return panPanelWidth;
}

public void setPanPanelWidth(int panPanelWidth) {
    this.panPanelWidth = panPanelWidth;
}

public int getPanPanelHeight() {
    return panPanelHeight;
}

public void setPanPanelHeight(int panPanelHeight) {
    this.panPanelHeight = panPanelHeight;
}

/*
 * Add the ball
 */
// ==============================================
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.BLACK);

    initBall(g2);

    // trait épais
    g2.setColor(Color.DARK_GRAY);

    g2.setStroke(new BasicStroke(10));
    g2.drawLine((getPlayPanelWidth() / 2) - 5, getPlayPanelHeight(),
            (getPlayPanelWidth() / 2) - 5, 0);
}

/*
 * Init ball
 */
// ==============================================
private void initBall(Graphics2D graphics2d) {

    int x = getPosX(), y = getPosY();
    boolean backX = false;
    boolean backY = false;

    Graphics2D g2 = graphics2d;

    g2.fillOval(posX, posY, BALL_WIDTH, BALL_HEIGHT);

    //posBall.setLocation(posX + BALL_WIDTH, posY + (BALL_HEIGHT / 2));

    if (x < 1)
        backX = false;
    if (x > getWidth() - 50)
        backX = true;

    if (y < 1)
        backY = false;
    if (y > getHeight() - 50)
        backY = true;


    if (!backX)
        setPosX(++x);

    else {
        setPosX(--x);
    }

    if (!backY)
        setPosY(++y);
    else
        setPosY(--y);

    repaint();

    try {
        Thread.sleep(2);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

@Override
public void mouseDragged(MouseEvent arg0) {
}

@Override
public void mouseMoved(MouseEvent arg0) {

    posMouse.setLocation(arg0.getX(), arg0.getY()
            - (getPadPanelHeight() / 2));

    padPlayer1.setLocation(getPanPanelWidth() - 15, (int) posMouse.getY());
    padComputer.setLocation(5, (int) posMouse.getY());
}

}

MTHeadss
  • 245
  • 1
  • 3
  • 14
  • 6
    http://sscce.org/ – talnicolas Apr 02 '12 at 16:28
  • 5
    No No No :(, never use `Thread.sleep(...)` inside your GUI, always use `Timer`[javax.swing.Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for this purpose. For good help check this thread for all working [examples](http://stackoverflow.com/questions/9849950/something-seems-wrong-with-the-layout-jbutton-showing-unexpected-behaviour-at-r) of this Ball Thingy :-). Best of all check this [WONDERFUL EXAMPLE](http://stackoverflow.com/a/9852739/1057230) – nIcE cOw Apr 02 '12 at 16:34

1 Answers1

3

So you have:

private void initBall(Graphics2D graphics2d) {

int x = getPosX(), y = getPosY();
boolean backX = false;
boolean backY = false;

in the beginning, so that regardless of which direction the ball is going, booth booleans are set to false every time. Then, you don't have an "Else" when it comes to setting the back option in

    if (x < 1)
    backX = false;
if (x > getWidth() - 50)
    backX = true;

if (y < 1)
    backY = false;
if (y > getHeight() - 50)
    backY = true;

What is happening is that the ball is moving in the right direction, until it hits a wall (I'm guessing the top wall). then this is called:

if (y > getHeight() - 50)
    backY = true;

So then for that iteration the ball goes back because of

if (!backY)
    setPosY(++y);
else
    setPosY(--y);

But then you set it back to false right away. I suggest you have

private boolean backX = false; //same for backY

outside your method.

K-man
  • 353
  • 1
  • 13
  • +1, actually for the nice catch, my comment was wrong regarding what I suggested :-), so deleted it. – nIcE cOw Apr 02 '12 at 16:56