Java俄罗斯方块游戏:在移动网格中的块时遇到问题
我正在用爪哇制作俄罗斯方块。我很难让我的区块向下移动,一旦一行被删除(因为它被填满)。我的代码抛出arrayIndexOutOfBounds错误。代码的其余部分起作用,它找到哪一行已满并将该行设置为0。但在靠近底部的if语句中,这就是它不起作用的地方。谢谢!:)Java俄罗斯方块游戏:在移动网格中的块时遇到问题,java,tetris,Java,Tetris,我正在用爪哇制作俄罗斯方块。我很难让我的区块向下移动,一旦一行被删除(因为它被填满)。我的代码抛出arrayIndexOutOfBounds错误。代码的其余部分起作用,它找到哪一行已满并将该行设置为0。但在靠近底部的if语句中,这就是它不起作用的地方。谢谢!:) //删除所有已填充的行 公共静态整型函数() { //变数 int numorrows=tetrisGrid.getnumorrows(); int nummofcols=tetrisGrid.getnummofcols(); int
//删除所有已填充的行
公共静态整型函数()
{
//变数
int numorrows=tetrisGrid.getnumorrows();
int nummofcols=tetrisGrid.getnummofcols();
int numRowsRemoved=0;
int[][]grid=tetrisGrid.getGrid();
//查找已填充的行数
for(int行=0;行
您是否尝试过使用调试器?当您编写行++代码>然后col++代码>,可以将这些索引移到网格之外。grid[row][col]=1并不奇怪代码>将引发异常。您能解释一下这行代码吗:shiftRow<2;在for loop why 2中?当我玩俄罗斯方块时,我没有使用2D数组。相反,我使用ArrayList来保存1D数组。然后,删除行时,只需从ArrayList中删除该行,并在ArrayList的开头添加一个空行。这样,您就不必担心每次删除一行时都会移动所有数据。有关基本工作示例,您可以查看:
// Removes any rows that are filled
public static int removeRows()
{
// Variables
int numOfRows = tetrisGrid.getNumOfRows();
int numOfCols = tetrisGrid.getNumOfCols();
int numRowsRemoved = 0;
int [][] grid = tetrisGrid.getGrid();
// Finds the number of rows that are filled
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
{
int blockValue = tetrisGrid.getBlockValue(row, col);
if (blockValue == 0)
{
break;
}
if (col == numOfCols - 1)
{
numRowsRemoved++;
// Sets the rows filled to zero
for (int change = 0; change < numOfCols; change++)
{
grid [row][change] = 0;
}
// Shifts everything down until it hits blocks below
for (int shiftRow = 0; shiftRow < 2; shiftRow++)
{
for (int shiftCol = 0; shiftCol < numOfCols; shiftCol++)
{
if (blockValue == 1)
{
grid [row][col] = 0;
row++;
col++;
grid [row][col] = 1;
row--;
col--;
}
else if (blockValue == 0)
break;
}
}
// TAKE OUT -------------------------
System.out.println ("--------");
tetrisGrid.printGrid();
System.out.println ("--------");
// -------------------------
}
}
}
tetrisGrid.setGrid(grid);
return numRowsRemoved;
}