我正在嘗試在XNA中創建一個俄羅斯方塊遊戲,以便更好地學習在未來製作更好的遊戲。我遇到的唯一問題是遊戲的「刪除行」功能。我有一個整數矩陣來保存塊中的所有行數。每當我創建一個新的塊時,我在指定的行中增加一個計數。然後,我檢查矩陣中的任何值是否達到或超過10.然後,如果有,則刪除該行中的所有塊。我遇到的問題是它不可靠,並且它不會跟蹤計數以及它應該出於某種原因,有時它不會刪除該行中的所有塊。如果您需要查看其他課程,請告訴我。謝謝,幫助表示讚賞。俄羅斯方塊刪除行問題
public class Main : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Texture for the block
public static Texture2D block;
//Font
public static SpriteFont font1;
//Which shape we're dealing with(Moving)
public static int shapeIndex;
//The next shape, the one we can see previewed
public static Shape nextShape;
//The first shape we use
public static Shape s2;
//All the shapes
public static List<Shape> shapes = new List<Shape>();
//All the blocks that have stopped moving (The shapes are converted to blocks when they stop moving to make for easier deletion)
public static List<Block> blocks = new List<Block>();
//The count of blocks in each row
public static int[] rowCount = new int[20];
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 500;
}
protected override void Initialize()
{
this.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
block = Content.Load<Texture2D>("Sprites//TetrisBlock");
font1 = Content.Load<SpriteFont>("Fonts//GameFont");
//Creating a random shape
switch (new Random().Next(1, 3))
{
case 1:
nextShape = new Shape(Shape.Shapes.tShape, new Vector2(550, 0), false);
break;
case 2:
nextShape = new Shape(Shape.Shapes.lineShape, new Vector2(550, 0), false);
break;
}
//The current shape we're dealing with
shapeIndex = 0;
//Creating the first shape
s2 = new Shape(Shape.Shapes.lineShape, new Vector2(), true);
}
protected override void Update(GameTime gameTime)
{
//If the blocks that are still are rainbow, have them cycle through their colors
foreach (Block b in nextShape.Blocks)
{
if (b.RainbowBlock)
b.changeColor(gameTime);
}
//Update all the shapes
for (int i = 0; i < shapes.Count; i++)
{
shapes[i].Update(gameTime);
}
//If the shape has hit another shape and stopped moving
if (!shapes[shapeIndex].MoveDown)
{
//For every block that was in the shape, add it to the block list and increase that row's count by one
foreach (Block b in shapes[shapeIndex].Blocks)
{
blocks.Add(b);
rowCount[b.Row]++;
}
//Remove that shape
shapes.RemoveAt(shapeIndex);
//The current shape we need to move
Shape s3 = nextShape;
s3.Position = new Vector2();
s3.Imaginary = false;
shapes.Add(s3);
//Creating a new random shape for the next shape
switch (new Random().Next(1, 4))
{
case 1:
nextShape = new Shape(Shape.Shapes.tShape, new Vector2(550, 0), false);
break;
case 2:
nextShape = new Shape(Shape.Shapes.lineShape, new Vector2(550, 0), false);
break;
case 3:
nextShape = new Shape(Shape.Shapes.lShape, new Vector2(550, 0), false);
break;
}
}
//Testing whether or not rows have reached their maximum capacity
for (int i = 0; i < rowCount.Length; i++)
{
//If a row has reached its capacity
if (rowCount[i] >= 10)
{
//Remove that row
removeRow(i);
//Move all blocks that are above that row down one
foreach (Block b in blocks)
{
if (b.Row < i)
{
//Subtract the old rowcount by one
rowCount[b.Row]--;
b.Row++;
//Add one to the rowcount(If I remove this, it seems to work a little better but it still has issues)
rowCount[b.Row]++;
}
}
}
}
//Update all the blocks that are still
foreach (Block b in blocks)
b.Update(gameTime);
base.Update(gameTime);
}
//Remove the row specified in the parameters
public void removeRow(int row)
{
//For every block
for (int i = 0; i < blocks.Count; i++)
{
//See if it's in the row the user wants to remove
if (blocks[i].Row.Equals(row))
{
//If it is, remove it and decrement that row's rowcount
blocks.RemoveAt(i);
rowCount[row]--;
//Here was the problem, I wasn't decrementing i to check the next block
i--;
}
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
//Draws every shape in the game
foreach (Shape s in shapes)
s.Draw(spriteBatch);
//Draws all the blocks at the bottom that have stopped moving
foreach (Block b in blocks)
b.Draw(spriteBatch);
//Info for me
spriteBatch.DrawString(font1, "Next Block:", new Vector2(430, 0), Color.Black);
spriteBatch.DrawString(font1, rowCount[19].ToString() + " " + blocks.Count + " Blocks", new Vector2(300, 0), Color.Black);
//For the next shape, draw every block so we know what it looks like
foreach (Block b in nextShape.Blocks)
b.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
[編輯] 向下移動的作品就好了零件,它只是刪除麻煩的行。我也試着盡我所能發表評論,如果您有任何問題,請提問。再次感謝。
要幫助你並不容易。該代碼沒有評論,其結構儘管看起來很好,但並不太好。你不能指望我們去調試/執行,瞭解它並發現問題。你應該包括很多評論或清楚解釋相關部分的作用(例如,名爲removeRow的地方,所有變量涉及的等),你發現的確切問題,什麼工作正常,等等。 – varocarbas
我試圖評論爲最好的我可以,還有其他什麼幫助嗎? – TheUnrealMegashark
好多了,好多了。這段代碼當然很清楚。不幸的是我已經離開了(這裏很晚)。明天如果問題仍未解決,我會回來嘗試幫助你。 – varocarbas