Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
    while(continueGame){
        //Pause dat loop
        pause(WAIT);
        currentTime = currentTime + WAIT;

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

            if(ballSpeedX>0)
                ballSpeedX += 2;
            else
                ballSpeedX -= 2;
        }

        //Move the ball
        ballX=ballX+ballSpeedX;
        ballY=ballY+ballSpeedY;

        ball.setLocation(ballX, ballY);

        //Check
        checkCollisions();
    }

    //... 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?
    if(batBounds.intersects(ballBounds)){
        batCollision();
    }
}


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
}
share|improve this question
    
Is it on purpose that none of what seems to be instance variables are private? Also, why aren't WAIT and MV_AMT static? –  fge Jun 5 '13 at 15:25
    
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. –  Jonny Sooter Jun 5 '13 at 15:34
1  
There are potential race conditions everywhere throughout this code. See: codereview.stackexchange.com/a/26948/20611 –  Dave Jarvis Jun 5 '13 at 16:00
    
How are there race conditions? Everything I can see is done within one single-threaded loop. –  supercat Jun 6 '13 at 16:04
add comment

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?

share|improve this answer
    
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. –  Jonny Sooter Jun 7 '13 at 0:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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