C# 我想使用c在控制台中水平和垂直居中显示文本#

C# 我想使用c在控制台中水平和垂直居中显示文本#,c#,terminal,console,C#,Terminal,Console,我想在游戏结束时将文本居中。现在它出现在顶部,我想把它带到屏幕中间 这是我到目前为止的代码及其输出 { SoundEffect(); Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.Red;//Text color for game over //Assign string output when game is over, player points and ent

我想在游戏结束时将文本居中。现在它出现在顶部,我想把它带到屏幕中间

这是我到目前为止的代码及其输出

{
    SoundEffect();
    Console.SetCursorPosition(0, 0);
    Console.ForegroundColor = ConsoleColor.Red;//Text color for game over

    //Assign string output when game is over, player points and enter key 
    String textGameOver = "Game Over!";
    String playerPoints = "Your points are:";
    String enterKey = "Press enter to exit the game!";

    //Output to show on screen
    Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (textGameOver.Length / 2)) + "}", textGameOver));
    int userPoints = (snakeElements.Count - 4) * 100 - negativePoints;//points calculated for player
    userPoints = Math.Max(userPoints, 0); //if (userPoints < 0) userPoints = 0;

    //Output to show player score on screen 
    Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (playerPoints.Length / 2)) + "}", playerPoints + userPoints));

    SavePointsToFile(userPoints);//saving points to files


    //exit game only when enter key is pressed
    Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (enterKey.Length / 2)) + "}", enterKey));
    while (Console.ReadKey().Key != ConsoleKey.Enter) {}
    return 1;
}
{
声音效果();
Console.SetCursorPosition(0,0);
Console.ForegroundColor=ConsoleColor.Red;//游戏结束时的文本颜色
//游戏结束时指定字符串输出,玩家点数并输入键
String textGameOver=“游戏结束!”;
String playerPoints=“你的分数是:”;
String enterKey=“按回车键退出游戏!”;
//要在屏幕上显示的输出
WriteLine(String.Format(“{0,”+((Console.WindowWidth/2)+(textGameOver.Length/2))+“}”,textGameOver));
int userPoints=(snakelements.Count-4)*100-negativePoints;//为玩家计算的点数
userPoints=Math.Max(userPoints,0);//如果(userPoints<0)userPoints=0;
//输出以在屏幕上显示球员得分
WriteLine(String.Format(“{0,”+((Console.WindowWidth/2)+(playerPoints.Length/2))+“}”,playerPoints+userPoints));
SavePointsToFile(userPoints);//将点保存到文件
//仅当按下回车键时退出游戏
WriteLine(String.Format(“{0,”+((Console.WindowWidth/2)+(enterKey.Length/2))+“}”,enterKey));
while(Console.ReadKey().Key!=ConsoleKey.Enter){}
返回1;
}

您需要确定从何处开始垂直打印行:

private static void PrintLinesInCenter(params string[] lines)
{
    int verticalStart = (Console.WindowHeight - lines.Length) / 2; // work out where to start printing the lines
    int verticalPosition = verticalStart;
    foreach (var line in lines)
    {
        // work out where to start printing the line text horizontally
        int horizontalStart = (Console.WindowWidth - line.Length) / 2;
        // set the start position for this line of text
        Console.SetCursorPosition(horizontalStart, verticalPosition);
        // write the text
        Console.Write(line);
        // move to the next line
        ++verticalPosition;
    }
}
用法:

PrintLinesInCenter("hello", "this is a test", "of centered text");
结果:


为什么要将代码作为图像提供?我发现调试这些程序非常困难。“我面临的问题是…”告诉我们您想做什么,但没有告诉我们您在上面找到的代码片段有什么问题。图像中的代码在这里是不可接受的。请参阅,以获取原因列表。请您的文章包括实际的代码作为文本,正确的可读性格式。编辑时,单击“答案”工具栏中的
按钮可获得格式帮助。您是否有获得垂直居中的问题?您有基于字符串的字符宽度(
s.width
)获取水平起始位置的代码。如何根据希望打印的行数获得垂直起始位置?您“引用”的是所问问题的答案。。。所以我用它作为复制品。您可能需要提问,以澄清您在实现该问题的建议时遇到了哪些问题,以便可以考虑重新打开(不要忘了将代码显示为文本)好的,我会将其添加为代码。