从2维阵列XNA中删除项目?

时间:2013-06-19 12:08:59

标签: c# xna xna-4.0 monogame

我很难如何删除多维数组中的项目。我正在努力创造一个突破性的游戏。唯一的问题是,一旦瓷砖消失,我想重新开始我的游戏。但我无法弄清楚我的砖块中有多少已经消失了。我计划当某个计数器达到零时我将重新开始,但我不能删除一个二维数组。我只是将每个瓷砖的可见部分设置为假,但实际上并没有将它们移除..

这是我的砖类

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);
    }
}

我的另一课与砖相关

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);
    }
}

在我的游戏类中,我只调用与砖有关的类。有任何想法吗??谢谢

这是我的球类

    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;
                }
            }
}

2 个答案:

答案 0 :(得分:1)

您可以遍历所有砖块并检查其visible属性:

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
}

但这意味着您将反复循环遍历所有砖块以检查是否该结束游戏。效率不高。

相反,为什么不跟踪整数_visibleBrickCount,在开始时将其设置为游戏中的砖块数量,并在每次调用ball.BrickCollision()时将其减少一个?然后,当它达到零时,你知道所有的砖块都消失了。

答案 1 :(得分:1)

为什么不使用动态数组,所以你可以动态删除砖块?从列表中删除的砖将不会被绘制/更新,并且它们保留的内存将被释放,因为不会保留其他引用。我在加载砖块时绝对必须使用伪矩阵,你可以这样做:

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));
}