I have a very simple Pong game that I've built in Java. The code is quite long so I've decided to focus this question on the collision that occurs with the ball and the bat and also the effects in the game. I'm using ACM Graphics package to learn Java so most of the methods are from that package. I want to know how I can improve this checking process and if the way I revert the speed and direction is efficient. I'm also open to any suggestions for the game.

//Setting up variables
static final int WAIT = 50;
static final int MV_AMT = 20;
static final int BATWIDTH = 120;
static final int BATHEIGHT = 20;
static final int WINDOWX = 400;
static final int WINDOWY = 400;
static final int BALLRADIUS = 10;
private int batX = 150, batY = 400; //Starting positions
private int ballX = 160, ballY = 370;
private int ballSpeedX = 2; //the ball speed on the X axis
private int ballSpeedY = -9; //the ball speed on the Y axis

public void run(){
    //... Stuff that runs before the game, ie. draw the sceen etc.

    int currentTime = 0;
    //Do all our stuff here
        //Pause dat loop
        currentTime = currentTime + WAIT;

        //Up the speed every 5 seconds
        if (currentTime % 5000 == 0) {
                ballSpeedY += 2;
                ballSpeedY -= 2;

                ballSpeedX += 2;
                ballSpeedX -= 2;

        //Move the ball

        ball.setLocation(ballX, ballY);


    //... Stuff that gets done after game over

public void checkCollisions(){
    //This method is quite long so I won't be posting it all
    //Just the part the calls the collision method

    //Get the bounds
    GRectangle batBounds = bat.getBounds();
    GRectangle ballBounds = ball.getBounds();

    //Where is the ball?
    ballX = (int)ball.getX();
    ballY = (int)ball.getY();

    //Where is the bat?
    batX = (int)bat.getX();
    batY = (int)bat.getY();

    //Did the bat touch the ball?

public void batCollision(){
    if( ballX+BALLRADIUS > batX+(BATWIDTH/2) ){ //Which side of the bat?
        //Which direction is the ball traveling when it hits?
        if(ballSpeedX >> 31 !=0){
            ballSpeedX = ballSpeedX * -1;
        } else {
            ballSpeedX = ballSpeedX;
    } else {
        if(ballSpeedX >> 31 !=0){
            ballSpeedX = ballSpeedX;
        } else {
            ballSpeedX = -ballSpeedX;

    ballSpeedY = -ballSpeedY; //Adjust Y speed
  • \$\begingroup\$ Is it on purpose that none of what seems to be instance variables are private? Also, why aren't WAIT and MV_AMT static? \$\endgroup\$
    – fge
    Jun 5, 2013 at 15:25
  • \$\begingroup\$ They are private, I just didn't want to have to have it written out every time here. I'll just edit those in anyways. \$\endgroup\$ Jun 5, 2013 at 15:34
  • 1
    \$\begingroup\$ There are potential race conditions everywhere throughout this code. See: codereview.stackexchange.com/a/26948/20611 \$\endgroup\$ Jun 5, 2013 at 16:00
  • \$\begingroup\$ How are there race conditions? Everything I can see is done within one single-threaded loop. \$\endgroup\$
    – supercat
    Jun 6, 2013 at 16:04

1 Answer 1


In the original Pong® brand video game, there were two bats which were confined to move vertically. A collision would be detected if ball circuit was triggered at the same time as one of the bat circuits, and if the ball was not already moving in the proper direction for that bat. The ball's vertical speed would be set to an odd number in the range -15 to +15 based upon the number of scan lines of bat that were displayed before the collision was detected (basically the difference between the ball's Y position and the bat's position). What exact bounce behavior are you looking for in your game?

  • \$\begingroup\$ So I've made sort of a hybrid between Pong and Breakout. What I'm looking for is to determine the side of the bat that was hit and in which direction the incoming ball was moving to set the return speed accordingly. \$\endgroup\$ Jun 7, 2013 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.