在控制台中绘制它的更好方法

时间:2014-09-14 22:26:24

标签: c# ascii-art

我想知道是否有办法改进代码,在这种情况下,获取门(请不要谈论Windows窗体和更好的游戏方式,我只是尝试一下。)

代码:

Console.WriteLine("\t _____________\t\t" + " _____________\t\t" + " _____________");
Console.WriteLine("\t|  _________  |\t\t" + "|  _________  |\t\t" + "|  _________  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |   " + input + "   |  |\t\t" + "|  |   " + (input + 1) + "   |  |\t\t" + "|  |   " + (input + 2) + "   |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |_______|  |\t\t" + "|  |_______|  |\t\t" + "|  |_______|  |");
Console.WriteLine("\t|             |\t\t" + "|             |\t\t" + "|             |");
Console.WriteLine("\t()------------|\t\t" + "()------------|\t\t" + "()------------|");
Console.WriteLine("\t|  ________   |\t\t" + "|  ________   |\t\t" + "|  ________   |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |       |  |\t\t" + "|  |       |  |\t\t" + "|  |       |  |");
Console.WriteLine("\t|  |_______|  |\t\t" + "|  |_______|  |\t\t" + "|  |_______|  |");
Console.WriteLine("\t|_____________|\t\t" + "|_____________|\t\t" + "|_____________|");

结果:

Image

我正在寻求帮助以获得相同的结果,但使用更好的代码(如果可能的话)。

2 个答案:

答案 0 :(得分:5)

Lol我的尝试

private static string[] what = {
        "\t _____________",
        "\t|  _________  |",
        "\t|  |       |  |",
        "\t|  |   {0}   |  |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |_______|  |",
        "\t|             |",
        "\t()------------|",
        "\t|  ________   |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |       |  |",
        "\t|  |_______|  |",
        "\t|_____________|"
        };

static void Main(string[] args)
{
    GenerateMultipleWhat(new[] { 1, 2, 3 });
    Console.ReadKey();
}

static void GenerateMultipleWhat(int[] values)
{
    for (int i = 0; i < what.Length; i++)
    {
        for(int x = 0; x < values.Length; x++)
            Console.Write(what[i] + "{1}", values[x], (x + 1) == values.Length ? "" : "\t");
        Console.WriteLine();
    }
}

结果:

enter image description here

答案 1 :(得分:0)

您可能希望将ASCII艺术品存储在文本文件中,在应用程序启动时读取它,然后显示其内容。这将使您的代码更清晰,并且它将使艺术更容易修改。毕竟大多数游戏都是这样做的:不是用代码绘制单个像素,而是从图像和模型文件中加载它们的艺术作品。

// At startup:
string art = File.ReadAllText("ascii_art.txt");

// whenever you need to display those doors:
Console.WriteLine(string.Format(art, input, input + 1, input + 2));

示例ascii_art.txt:

 _____   _____   _____
|     | |     | |     |
| {0} | | {1} | | {2} |
|_____| |_____| |_____|

只需确保格式说明符与string.Format参数匹配即可。此外,文本文件中的格式说明符长度为3个字符,但如果它们被单个字符替换,则布局将混乱。防止这种情况的一种方法是在ToString().PadLeft(3)参数上调用string.Format