首页 > 解决方案 > 如何制作边界线

问题描述

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

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

我的这个控制台的尺寸是

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

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

标签: c#border

解决方案


看看我的代码,我在这里找到了大部分代码并根据您的需要进行了调整。

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);
    }
}

推荐阅读