thenewbie
thenewbie

Reputation: 805

remove item from 2 dimensional Array XNA?

Im having a hardtime how to remove an item in Multidimensional array. I am trying to create a breakout game. The only problem left though is that once the tile is gone i want to start again my game. But i cant figure out if how many are gone in my brick. I plan that when a certain counter reach to zero i will start again but i cant remove a 2d array. Im just setting the visible of each tile to false but not actually removing them..

here is my brick class

public Rectangle Location
{
    get { return location; }
}

public int ID { get; set; }

public Brick(Texture2D texture, Rectangle location, int id)
{
    this.texture = texture;
    this.location = location;
    ID = id;  
}

public void CheckCollision(Ball ball, int i)
{
    if (visible && ball.getBounds().Intersects(location))
    {
        visible = false;
        ball.brickCollision();
    }
}

public void Draw(SpriteBatch spriteBatch)
{
    if (visible)
    {
        // gc.checker.Add(ID);
        spriteBatch.Draw(texture, location, Color.White);
    }
}

my other class related to brick

Brick[,] bricks;

private Texture2D b;
public void loadBricks(Texture2D b, int x, int y)
{
    bricks = new Brick[x, y];
    this.b = b;

    for (int row = 0; row < y; row++)
    {
        for (int col = 0; col < x; col++)
        {
            bricks[col, row] = new Brick(b, new Rectangle(row * b.Width, col * b.Height, b.Width - 2, b.Height),row);
        }
    }
}

public void update(Ball ball)
{
    foreach (Brick brick in bricks)
    {
        brick.CheckCollision(ball, bricks.Length);
    }
}

public void drawBrick(SpriteBatch batch)
{
    foreach (Brick brick in bricks)
    {
        //debugconsole("brck",brick);
        brick.Draw(batch);
    }
}

in my game class i only call the class thats related to brick. Any ideas?? thanks

Here is my ball class

    public Ball(Texture2D ball, Vector2 speed, GraphicsDevice g, Rectangle p)
            {
                this.ball = ball;
                ballSpeed = speed;
                this.g = g;
                this.p = p;
                ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ball.Width, ball.Height);
            }

            public void Update(Paddle p)
            {
                x = p.getBounds().X;
                y = p.getBounds().Y;
                collidedBrick = false; // prevent removing too many bricks at one collision
                ballmove();//move the ball
                wallCollision();// check the ball and wall collision
                paddCollision(p); // check paddle and ball collision
            }

            public void Update()
            {
                throw new NotImplementedException();
            }

            public void Draw(SpriteBatch batch)
            {
                batch.Draw(ball, ballRect, Color.White);
            }

            #region Collisions
            public void wallCollision()
            {
                if (ballPosition.X < 0) //ball collide on left side screen
                {
                    ballSpeed.X *= -1;
                    ballPosition.X = 0;
                }
                if (ballPosition.X > getScreenSize().Width - ballRect.Width) // ball collide on right side of screen
                {
                    ballSpeed.X *= -1;
                    ballRect.X = getScreenSize().Width - ballRect.Width;
                }
                if (ballPosition.Y < 0)// ball collide on top edge of screen
                {
                    ballSpeed.Y *= -1;
                    ballRect.Y = 0;
                }
                if (ballPosition.Y > getScreenSize().Height + ballRect.Height)
                {
                    spacePress = false;
                }
            }
            public void brickCollision()
            {
                if (!collidedBrick)
                {

                    ballSpeed.Y *= -1;
                    collidedBrick = true;
                    counter++;

                }
            }

            public void paddCollision(Paddle p)
            {
                if (p.getBounds().Intersects(ballRect))
                {
                    ballPosition.Y = p.getBounds().Y - ball.Height;
                    ballSpeed.Y *= -1;
                }
            }
}

Upvotes: 1

Views: 808

Answers (2)

Max
Max

Reputation: 3583

Why don't you use a dynamic array for that, so you can remove the bricks on the fly? Bricks that are removed from the list won't be drawn/updated and the memory they hold will be released because no other references would remain. I you absolutely must use a pseudo-matrix when loading the bricks, you can do something like:

List<Bricks> bricks;

public void loadBricks(Texture2D b, int x, int y)
{
    bricks = new List<Bricks>();
    for(int i = 0; i < (x * y); i++)
        bricks.Add(new Brick(b, new Rectangle(b.Width * i / x, b.Height * (i % y), b.Width - 2, b.Height), i % y));
}

Upvotes: 1

Ant P
Ant P

Reputation: 25221

You could loop through all of your bricks and check their visible property:

bool endGame = true;
for (int row = 0; row < y; row++)
{
    for (int col = 0; col < x; col++)
    {
        if (bricks[col, row].visible)
        {
            endGame = false;
        }
    }
}

if (endGame)
{
    // End your game here
}

But this means that you will be repeatedly looping over all of your bricks to check if it's time to end the game or not. Not very efficient.

Instead, why not just track a integer _visibleBrickCount, set it to the number of bricks in your game at the start, and reduce it by one every time you call ball.BrickCollision()? Then, when it reaches zero, you know all the bricks are gone.

Upvotes: 1

Related Questions