0
votes

I'm trying to make a simple brick breaker game. I have everything created with collision and simple physics. The ball bounces off the paddle and walls, but I'm having trouble with the bricks. I can get the ball to bounce off the bricks, I'm just not experienced enough to know how to make the brick disappear. The bricks are in an array. Is there some way to remove a brick/specific index of an array?

This is how I create the blocks

        int blockCount=0;

        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                blocks[blockCount] = Content.Load<Texture2D>("Block");
                blocksPosition[blockCount].X = 95 + (x * 75);
                blocksPosition[blockCount].Y = 20 + (y * 50);
                blockCount++;
            }
        }

This is how I do collision and how I would implement the brick removal

    public void DetectBlockBallCollision()
    {
        for (int i = 0; i<40; i++)
        {
            if ((ballPosition.Y + ball.Height) >= blocksPosition[i].Y &&
                (ballPosition.Y + ball.Height) < (blocksPosition[i].Y+75) &&
                (ballPosition.X + ball.Width) > blocksPosition[i].X &&
                ballPosition.X < (blocksPosition[i].X + blocks[i].Width))
            {
                movingUp = false;
                //whatever would get rid of block;
            }

        }

    }
2
I would just create an Active or Visible property on the brick and then use that to determine whether or not to draw it and/or calculate collisions for it. You could also just set the element to null and check for null in your loop. - TyCobb

2 Answers

2
votes
  1. Don't load texture in your loop, load it only once in LoadContent.
  2. Put properly Active (as TyCobb suggested) in your brick element and in collsion set it to true and then remove it somewhere outside loop.

    bricks.removeAll(b => !(b.active))

By different properties your bricks could have health, destroyable property, brick that drop some item...

0
votes

Two-dimensional array is useful to align block by grid. Blocks will be arranged by grid cell coords with fixed pixel step in the X and Y, the position is defined as (col * step_x, row * step_y). Then, for an array of int value of 0 means there is no block, and non-zero value mean block ID from the block register. This register should keep data common to all units of this block (with the same ID). This method is best suited for blocks that can be destroyed with one hit. For a more durable can be used several variants of the same total block, such if brick block (1 HP) have ID = 1, stone block (2 HP) have ID = 2 for non-damaged and ID = 3 for damaged, iron block with 3/2/1 HP have ID = 4/5/6 and so on. Then you need to remove the unit set in the array to 0.

The second way is use list. Blocks should be presented in the form of objects, each of which knows its coordinates, max and current HP, special states and so on. Benefits that you can actually remove the blocks from the list when you hit it ant it's HP become 0.