我很难删除多维数组中的项目。我正在尝试创建一个突破游戏。剩下的唯一问题是,一旦瓷砖消失,我想重新开始我的游戏。但我不知道我的砖里有多少。我计划当某个计数器达到零时,我将重新开始,但我无法删除二维数组。我只是将每个图块的可见性设置为 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;
}
}
}