我一直在关注如何编写简单的俄罗斯方块游戏的c ++教程。我正在用C#写作,所以这是一个很大的挑战。我正在使用一个由0,1,2组成的数组(让我们说这是一个支点)。我的问题是我想从控制台的顶部开始写1,2,不包括0。我一直在尝试使用GetXInitialPosition()
和GetYInitialPosition()
,它们返回负值,以便在Console.SetCursorPosition()
方法中绘制排除形状上方的零的块。该方法不能采用负值,因为它必须在控制台窗口上方绘制。最后在代码中有一个例子。我想跳过00210以上的所有内容并从那里开始绘图。我真正想做的就是找到一种在控制台上方绘制的方法。
这是我的代码:
class Tile
{
public Tile()
{
pPiece = SetRandomShape();
pRotation = 0;
}
public int GetBlockType(int whichBlock, int pRotation, int pX, int pY)
{
return Block1[whichBlock, pRotation, pX, pY];
}
public int GetXInitialPosition()
{
return BlockInitialPosition[pPiece, pRotation, 0];
}
public int GetYInitialPosition()
{
return BlockInitialPosition[pPiece, pRotation, 1];
}
public void Print(int xPos, int yPos)
{
for (int i = 0; i < Block.GetLength(2); i++)
{
for (int j = 0; j < Block.GetLength(3); j++)
{
if ((GetBlockType(pPiece, pRotation, j, i) == 1))
{
Console.SetCursorPosition(xPos + i, yPos + j);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(GetBlockType(pPiece, pRotation, j, i));
}
else if ((GetBlockType(pPiece, pRotation, j, i) == 2))
{
if (yPos + j >= 0)
{
Console.SetCursorPosition(xPos + i, yPos + j);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(GetBlockType(pPiece, pRotation, j, i));
}
}
else //displaying 00
{
Console.SetCursorPosition(xPos + i, yPos + j);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(GetBlockType(pPiece, pRotation, j, i));
}
}
Console.WriteLine(" ");
}
}
private int[,,,] Block = new int[7, 4, 5, 5] //7 kinds , 4 rotation / 5 horizontal / 5 vertical
{
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 2, 1, 0},
{0, 0, 1, 1, 0},
{0, 0, 0, 0, 0}
},
private int[,,] BlockInitialPosition = new int[7, 4, 2] // 7 kinds , 4 rotations , horizontal value of row that needs to be removed , vertical value of row that needs to be removed
/* Square */
{
{-2, -3},
{-2, -3},
{-2, -3},
{-2, -3}
},
答案 0 :(得分:0)
无法在控制台外绘图。这也不是什么大问题,因为无论如何我们都无法看到它。所以只是不要在控制台外面写。在双循环中,在if-else-if
代码之前,检查是否要绘制外部控制台,如果是,则跳过绘图。像这样:
for (int i = 0; i < Block.GetLength(2); i++)
{
if (xPos + i < 0 || xPos + i >= Console.WindowWidth)
continue;
for (int j = 0; j < Block.GetLength(3); j++)
{
if (yPos + j < 0 || yPos + j >= Console.WindowHeight)
continue;
// if-else-if ...
}
}