1

我很难删除多维数组中的项目。我正在尝试创建一个突破游戏。剩下的唯一问题是,一旦瓷砖消失,我想重新开始我的游戏。但我不知道我的砖里有多少。我计划当某个计数器达到零时,我将重新开始,但我无法删除二维数组。我只是将每个图块的可见性设置为 false,但实际上并没有删除它们..

这是我的积木课

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

2 回答 2

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()?然后,当它达到零时,你知道所有的砖都不见了。

于 2013-06-19T12:17:39.577 回答
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));
}
于 2013-06-19T12:54:31.483 回答