如何制作边界线

时间:2019-12-06 03:55:48

标签: c# border

所以我正在做一个游戏,我试图使边界从控制台的顶部延伸到控制台左侧的控制台底部。这是我的代码,用于尝试制作线条

Console.SetCursorPosition(0, Console.WindowWidth + 20);
Console.Write(new string( '|', Console.WindowHeight));

此控制台的尺寸为

Console.SetWindowSize(120, 30);
Console.SetBufferSize(120, 30);

我只是一个初学者,所以如果有一种简单的方法可以帮助我,请

1 个答案:

答案 0 :(得分:0)

看看我的代码,我找到了大多数代码here,并根据您的需要进行了修改。

protected static int origRow;
protected static int origCol;

static void Main(string[] args)
{
    Console.SetWindowSize(120, 30);

    // Clear the screen, then save the top and left coordinates.
    Console.Clear();
    origRow = Console.CursorTop;
    origCol = Console.CursorLeft;

    int height = Console.WindowHeight;

    for (int i = 0; i < height; i++)
        WriteAt("|", 0, i);

    // this is to force to keep the application running. Now visual studio
    // will not put some extra text at the end of the screen
    Console.ReadLine();
}

// write a string as location x,y
public static void WriteAt(string s, int x, int y)
{
    try
    {
        Console.SetCursorPosition(origCol + x, origRow + y);
        Console.Write(s);
    }
    catch (ArgumentOutOfRangeException e)
    {
        Console.Clear();
        Console.WriteLine(e.Message);
    }
}