/**//// <summary>
/// 向下移动
/// </summary>
public void MoveToDown()
{
if (GameStatus != GameStatus.Play) return;
if (!IsBoundary(_currentPiece.Matrix, 0, 1))
{
RemovePiece();
AddPiece(0, 1);
}
else
{
// 如果触及底边了,则消除可消的行并且创建新的形状
RemoveRow();
CreatePiece();
// 每落下一个形状加 1 分
Score++;
}
}
/**//// <summary>
/// 变形
/// </summary>
public void Rotate()
{
if (GameStatus != GameStatus.Play) return;
if (!IsBoundary(_currentPiece.GetRotate(), 0, 0))
{
RemovePiece();
_currentPiece.Rotate();
AddPiece(0, 0);
}
}
/**//// <summary>
/// 清除俄罗斯方块容器
/// </summary>
public void Clear()
{
for (int x = 0; x < _columns; x++)
{
for (int y = 0; y < _rows; y++)
{
Container[y, x].Color = null;
}
}
}
/**//// <summary>
/// 边界判断(是否超过边界)
/// </summary>
/// <param name="matrix">当前操作的形状的4×4矩阵</param>
/// <param name="offsetX">矩阵 X 方向的偏移量</param>
/// <param name="offsetY">矩阵 Y 方向的偏移量</param>
/// <returns></returns>
private bool IsBoundary(int[,] matrix, int offsetX, int offsetY)
{
RemovePiece();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (matrix[i, j] == 1)
{
if (j + _positionX + offsetX > _columns - 1 // 超过列的右边界
|| i + _positionY + offsetY > _rows - 1 // 超过行的下边界
|| j + _positionX + offsetX < 0 // 超过列的左边界
|| Container[i + _positionY + offsetY, j + _positionX + offsetX].Color != null) // matrix 所需偏移的地方已经有 Block 占着了
{
AddPiece(0, 0);
return true;
} }
}
}
AddPiece(0, 0);
return false;
}
/**//// <summary>
/// 设置“下一个形状的容器”的 UI
/// </summary>
private void SetNextContainerUI()
{
// 清空
foreach (Block block in NextContainer)
{
block.Color = null;
}
// 根据 _nextPiece 的矩阵设置相对应的 Block 对象的呈现
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
if (_nextPiece.Matrix[x, y] == 1)
{
NextContainer[x, y].Color = _nextPiece.Color;
}
}
}
}
/**//// <summary>
/// 移除 _currentPiece 在界面上的呈现
/// </summary>
private void RemovePiece()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_currentPiece.Matrix[i, j] == 1)
{
Container[i + _positionY, j + _positionX].Color = null;
}
}
}
}
/**//// <summary>
/// 增加 _currentPiece 在界面上的呈现
/// </summary>
/// <param name="offsetX">X 方向上的偏移量</param>
/// <param name="offsetY">Y 方向上的偏移量</param>
private void AddPiece(int offsetX, int offsetY)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_currentPiece.Matrix[i, j] == 1)
{
Container[i + _positionY + offsetY, j + _positionX + offsetX].Color = _currentPiece.Color;
}
}
}
_positionX += offsetX;
_positionY += offsetY;
}
/**//// <summary>
/// 根据游戏规则,如果某行出现连续的直线则将其删除,该线以上的部分依次向下移动
/// </summary>
private void RemoveRow()
{
// 删除的行数
int removeRowCount = 0;
// 行的遍历(Y 方向)
for (int y = 0; y < _rows; y++)
{
// 该行是否是一条连续的直线
bool isLine = true;
// 列的遍历(X 方向)
for (int x = 0; x < _columns; x++)
{
if (Container[y, x].Color == null)
{
// 出现断行,则继续遍历下一行
isLine = false;
break;
}
}
// 该行是一条连续的直线则将其删除,并将该行以上的部分依次向下移动
if (isLine)
{
removeRowCount++;
// 删除该行
for (int x = 0; x < _columns; x++)
{
Container[y, x].Color = null;
}
责任编辑:小草