-4

I have added multiple rows of bricks to my breakout game however i now have the task of removing these bricks once they have been hit. I have added a for loop which seems to rebound off a row of bricks roughly in the middle but doesnt remove any. I am working in the class ActivePart.

class ActivePart
{
    private boolean runGame = true;

    public void stop()
    {
        runGame = false;
    }

    public void runAsSeparateThread()
    {
        final float S = 3; // Units to move (Speed) 
        try
        {
            synchronized ( Model.class ) // Make thread safe 
            {
                GameObj       ball   = getBall();     // Ball in game 
                GameObj       bat    = getBat();      // Bat 
                List<GameObj> bricks = getBricks();   // Bricks 
            }

            while (runGame)
            {
                synchronized ( Model.class ) // Make thread safe 
                {
                    float x = ball.getX();  // Current x,y position 
                    float y = ball.getY();
                    // Deal with possible edge of board hit 
                    if (x >= W - B - BALL_SIZE)  ball.changeDirectionX();
                    if (x <= 0 + B            )  ball.changeDirectionX();
                    if (y >= H - B - BALL_SIZE)  // Bottom 
                    { 
                        ball.changeDirectionY(); addToScore( HIT_BOTTOM ); 
                    }
                    if (y <= 0 + M            )  ball.changeDirectionY();

                    // As only a hit on the bat/ball is detected it is  
                    //  assumed to be on the top or bottom of the object. 
                    // A hit on the left or right of the object 
                    //  has an interesting affect 

                    boolean hit = false;
                    // *[3]******************************************************[3]* 
                    // * Fill in code to check if a visible brick has been hit      * 
                    // *      The ball has no effect on an invisible brick          * 
        // ************************************************************** 

        for ( int i = 0; i <= 60; i++ ){
                GameObj brick1 = bricks.get(i);

                if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
                    hit = true;
                    Debug.trace("BreakOut");

                }


            } // here is what i am working on 


        if (hit)
          ball.changeDirectionY();

        if ( ball.hitBy(bat) )
          ball.changeDirectionY();
      }
      modelChanged();      // Model changed refresh screen 
      Thread.sleep( fast ? 2 : 20 );
      ball.moveX(S);  ball.moveY(S);
    }
  } catch (Exception e) 
  { 
    Debug.error("Model.runAsSeparateThread - Error\n%s", 
                e.getMessage() );
  }
}
}

/**
* Model has changed so notify observers so that they
*  can redraw the current state of the game
*/
public void modelChanged()
{
setChanged(); notifyObservers();
}

}

I dont know how to remove the actual bricks from the game, it is currently just bouncing off a line by the looks off it not actually the bricks either.I take it i will need for loops for each seperate rows.

Code for adding bricks:

bricks = new ArrayList<>();
        // *[1]******************************************************[1]*
        // * Fill in code to place the bricks on the board              *
        // **************************************************************/

        // pink first 2 rows
        for (int i = 0; i < NBRICK_ROWS; i++){
            int y = BRICK_Y_OFFSET + (i * (BRICK_HEIGHT + BRICK_SEP));

            for (int j = 0; j < NBRICKS_PER_ROW; j++){
                int x = (BRICK_X_OFFSET) + (j * (BRICK_WIDTH + BRICK_SEP));
                bricks.add(new GameObj (x, y, BRICK_WIDTH, BRICK_HEIGHT, Colour.PINK));
            }
        }

In the view class in the drawActualPicture method this displays the bricks:

for(GameObj brick : bricks) {
            displayGameObj( g, brick);    

        }

this is what i have for it now :

if( // ball Y pos < brick Y pos = collision )
                    {
                        brick = null;
                    }
                    for ( int i = 0; i <= 60; i++ ){
                        GameObj brick1 = bricks.get(i);

                        if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){

                            bricks.set(i, null); // The brick in the position of i when the ball collides, will be null

                            hit = true;
                            Debug.trace("BreakOut");

                        }
                        List<GameObj> toRemove = new ArrayList<GameObj>();
                        for(GameObj a: bricks){
                            if(a.getY() <= brick1.getY() - (BRICK_HEIGHT/2)){
                                toRemove.add(a);
                            }
                        }
                        bricks.removeAll(toRemove);


                        if (hit)
                            ball.changeDirectionY();

                        if ( ball.hitBy(bat) )
                            ball.changeDirectionY();
                    }
                    modelChanged();      // Model changed refresh screen 
                    Thread.sleep( fast ? 2 : 20 );
                    ball.moveX(S);  ball.moveY(S);
                }
            } catch (Exception e) 
            { 
            Debug.error("Model.runAsSeparateThread - Error\n%s", 
                e.getMessage() );
        }
    }
RHH
  • 9
  • 6

1 Answers1

0

Why do not you just destroy the GameObj brick?

if(<ball collide with brick>)
{
    brick = null;
}

for ( int i = 0; i <= 60; i++ ){
   GameObj brick1 = bricks.get(i);

        if ( brick1.hitBy(ball) ){
             bricks.remove(i);
             //hit = true;
             ball.changeDirectionY();
        }
}
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/169759/discussion-on-answer-by-hector-manuel-martinez-duran-java-breakout-game-remove). – Samuel Liew Apr 25 '18 at 12:12