Search code examples
javadrjava

Bouncing ball off of paddle in breakout game ~java


So I am currently writing a code for a breakout game in java. Right now, I have it set up so that it prompts the player with a dialog box that asks the player how many bricks they want per row (a number between 4 and 40). The part that gets messed up is the whole collision with the ball and the paddle. I'm very new to programming so forgive me if it's incredibly easy. Using dr. Java.

    //-------------------------------  animate -------------------------
    /**
     * this should send the ball back after hitting the paddle.
     * 
     */ 
    public void animate( )
    {
            int dX = ball.getXLocation( ); 
            int dY = ball.getYLocation( );
            while( true )
            {

            ball.move( );
            if ( ball.boundsIntersects( paddle) ) 
            {

                dY= -1*dY;
                ball.setLocation( dX,dY );


            }

            for(int i = 0; i < bricks.size(); i++)
            {

            if (ball.boundsIntersects(bricks.get(i)))
            {


                dY = -dY;
                ball.setLocation(dX,dY);
                bricks.get(i).hide();
                bricks.remove(i);
                System.out.println("brick");
            }

}

here is the move method from my ball class. Once again sorry for the horrendous code.

//-------------------------------  move ----------------------------
    /**
     * This will move the ball by deltaX and deltaY
     * and bounce the ball off the edges of the Frame.
     * 
     */ 

    public void move( ) 
    {

        int dX = this.getXLocation( ) + deltaX;
        int dY = this.getYLocation( ) + deltaY;
        this.setLocation( dX, dY );
        if( getYLocation( ) < 0 )
        {
            setLocation( dX, 0 );
            deltaY *=-1;
        }
        else if( getYLocation( ) > ( 500 + size ) )
        {
            setLocation( dX, 500-size);
            deltaY *=-1;
        }
        if( getXLocation() < 0 )
        {
            setLocation( dX , dY );
            deltaX *=-1;
        }
        else if( getXLocation( ) > ( 500 + size ) )
        {
            setLocation( 500-size, dY );
            deltaX *=-1;
        }

   }    

Solution

  • In your animate() function you're setting the ball's Y value to negative what it is when it collided with something. I think you mean to set it's "deltaY" to negative like you do in the move() function. Your variable name is "dY" but it looks like that's just Y.

    What is the current behavior of the ball when it hits the paddle? Does it have the same strange behavior when it hits blocks too? It looks like you're doing the same -Y in the code that checks when it hits the block.

    Edit: Looks like this bit of code:

            if ( ball.boundsIntersects( paddle) ) 
            {
                dY= -1*dY;
                ball.setLocation( dX,dY );
            }
    

    Should be changed to:

    if ( ball.boundsIntersects( paddle) ) 
    {
        ball.deltaY = -ball.deltaY;
    }
    

    And the same here:

    if (ball.boundsIntersects(bricks.get(i)))
    {
        dY = -dY;
        ball.setLocation(dX,dY);
        bricks.get(i).hide();
        bricks.remove(i);
        System.out.println("brick");
     }
    

    to:

    if (ball.boundsIntersects(bricks.get(i)))
    {
        ball.deltaY = -ball.deltaY;
        bricks.get(i).hide();
        bricks.remove(i);
        System.out.println("brick");
     }
    

    Don't forget to mark this as answered if it fixes your problem.