yj2000
yj2000

Reputation: 63

Ball going through paddle when increasing velocity

I am trying to create a Pong game and so far I have everything working for me other than the ball. At first I had the ball's x and y axis move up by one space each time. It was working fine until i decided to increase it to 2. I cant figure out what is wrong with my code and I need help.

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Pong extends JPanel implements KeyListener{

int x = 90;
int y = 90;
int rectytop = 30;
int rectytop2 = 30;
int rectybottom = rectytop + 100; 
int rectybottom2 = rectytop2 + 100;

int border = 30;
boolean balldown = true;
boolean ballright = true;
boolean playerborderup = false;
boolean playerborderdown = false;
boolean balltouch1 = false;
boolean balltouch2 = false;


private void moveball() {

    if (balldown == true){
        y = y + 2;
    }

    if (y == getHeight()-border){
        balldown = false;
    }
   if (balldown == false){
        y = y - 2;
    }






   if (ballright == true){
       x = x + 2;
   }
   if (x == getWidth()-border){
       ballright = false;
   }
   if (ballright == false){
       x = x - 2;
   }



   if (y == 0){
       balldown = true;
   }

   if (x == 0){
       ballright = true;
   }



   if (balltouch1 == false){
       if (x == 75){
           if(y < rectybottom && y > rectytop){
               ballright = true;
           }
       }
   }


   if (balltouch2 == false){
       if (x == 390 && y < rectybottom2 && y > rectytop2){
               ballright = false;
       }
   }


}    

@Override
public void paint(Graphics g){
    super.paint(g);

    //drawing ball and paddles 
    g.fillOval(x, y, 30, 30);
    g.fillRect(45 , rectytop, 30, 100);
    g.fillRect(425, rectytop2, 30, 100);

}


public static void main(String[] args) throws InterruptedException {

    //making the window
    JFrame f = new JFrame("Pong Game");
    Pong game = new Pong();
    f.setVisible(true);
    f.setSize(500, 500);//1024, 724
    f.setResizable(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.addKeyListener(game);

    //game code

    f.add(game);        
    while (true){
        game.repaint();
        game.moveball();

        Thread.sleep(10);


    }
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

    //player one
    if (e.getKeyCode() == KeyEvent.VK_W){
        if (rectytop == 0){
            playerborderup = true;
        }
        if (rectytop != 0){
            playerborderup = false;
        }
        if (playerborderup == true){
            rectytop = rectytop + 0;
            rectybottom = rectytop + 100;
            repaint();
        }
        if (playerborderup == false){
            rectytop = rectytop - 5;
            rectybottom = rectytop + 100;
            repaint();
        }

    }

    if (e.getKeyCode() == KeyEvent.VK_S){
        if (rectytop == 585){
            playerborderdown = true;
        }
        if (rectytop != 585){
            playerborderdown = false;
        }
        if (playerborderdown == true){
            rectytop = rectytop - 0;
            rectybottom = rectytop + 100;
            repaint();
        }
        if (playerborderdown == false){
            rectytop = rectytop + 5;
            rectybottom = rectytop + 100;
            repaint();
        }
    }


    //player two
    if (e.getKeyCode() == KeyEvent.VK_UP){
        if (rectytop2 == 0){
            playerborderup = true;
        }
        if (rectytop2 != 0){
            playerborderup = false;
        }
        if (playerborderup == true){
            rectytop2 = rectytop2 + 0;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
        if (playerborderup == false){
            rectytop2 = rectytop2 - 5;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }

    }

    if (e.getKeyCode() == KeyEvent.VK_DOWN){
        if (rectytop2 == 585){
            playerborderdown = true;
        }
        if (rectytop2 != 585){
            playerborderdown = false;
        }
        if (playerborderdown == true){
            rectytop2 = rectytop2 - 0;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
        if (playerborderdown == false){
            rectytop2 = rectytop2 + 5;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
    }







}

@Override
public void keyReleased(KeyEvent e) {
    }





}

Upvotes: 1

Views: 207

Answers (2)

John Targaryen
John Targaryen

Reputation: 1194

Since you are moving by 2 every time, it may "skip" over some coordinates. Thus when you say

if (y == getHeight()-border){
    balldown = false;
}

y might go from getHeight()-border + 1 to getHeight()-border - 1 and never meet this condition. Thus, change it to a range or a less than. Your new code should be

if (y <= getHeight()-border +1){
    balldown = false;     //change the +1 and -1 to have difference at least speed number
}

Note that you must do the same for the other ifs with ==, change

if (x == getWidth()-border){
    ballright = false;
}

if (y == 0){
    balldown = true;
}

if (x == 0){
    ballright = true;
}

to

if (x <= getWidth()-border +1){
    ballright = false;
}

if (y <= 1){
    balldown = true;
}

if (x <= 1){
    ballright = true;
}

Note that you can resolve the problem also by saying if the position is eer exactly one away from the border, temporarily decrement position by 1 instead of 2.

Upvotes: 1

Misha
Misha

Reputation: 28133

You are using if (x == 75) to detect collision with the left paddle. But if speed is 2 than the ball's x is always even and never equals 75. For a quick fix, check x against a range: if (x > 60 && x < 75)

Upvotes: 0

Related Questions