如何创建包含前景色等属性的 Cha 信息数组?

发布于 2024-10-01 20:29:11 字数 1283 浏览 0 评论 0原文

我目前正在编写一款 Roguelike 游戏,用于学习/娱乐。我正在控制台中编写它,但我遇到了地图更新滞后的问题。我进行了大量的在线搜索并找到了一个可能的解决方案,但它是用 C++ 编写的(我相信)。

解决方案是使用 WriteConsoleOutput,但我不认为这在 C# 中可用。进一步搜索得出了可能的 C# 解决方案。将数组传递给 Console.Write 方法。然而,这种方法的问题是我无法传递(据我目前所知)有关角色的属性,例如前景色。

我将一些东西放在一起来测试将数组传递给 Console.Write。下面的代码将显示一个数字网格。我希望能够更改网格中每个值的前景色。所以 1 是蓝色,2 是红色,等等...

static void Main(string[] args)
    {
        Console.SetWindowSize(80, 35);
        Console.BufferWidth = 80;
        Console.BufferHeight = 35;
        string temp = "";

        int[,] aryMap = new int[,] {
           {0,0,0,0,0},
           {1,1,1,1,1},
           {2,2,2,2,2},
           {3,3,3,3,3},
           {4,4,4,4,4},
           {5,5,5,5,5}
        };         

        for (int h = 0; h < 5; h++)
        {
            temp += "\n";
            for (int w = 0; w < 5; w++)
            {
                temp += aryMap[h, w];
            }
        }
        Console.SetCursorPosition(0, 0);
        Console.Write(temp);
        string test = Console.ReadLine();
   }

解决方案

我最终使用了 Malison,它是一个用于在 C# 中执行控制台样式界面的库。效果很好,现在我不必创建自己的控制台。

http://bitbucket.org/munificent/malison/wiki/Home

I'm currently writing a roguelike game for learning purposes/fun. I'm writing it in the console, and I'm having issues with laggy updating of the map. I have done tons of online searching and came across a possible solution but it was written in c++ (I believe).

The solution was to use WriteConsoleOutput, however I do not believe this is available in C#. Further searching resulted in a possible C# solution. Pass an array to the Console.Write method. However the issue with this method is that I cannot pass (to my current knowledge) attributes about the character, like foreground color.

I threw something together to test passing an array to Console.Write. The below code will display a grid of numbers. I would like to have the ability to change the foreground color for each value in the grid. So 1 would be blue, and 2 would be red, etc...

static void Main(string[] args)
    {
        Console.SetWindowSize(80, 35);
        Console.BufferWidth = 80;
        Console.BufferHeight = 35;
        string temp = "";

        int[,] aryMap = new int[,] {
           {0,0,0,0,0},
           {1,1,1,1,1},
           {2,2,2,2,2},
           {3,3,3,3,3},
           {4,4,4,4,4},
           {5,5,5,5,5}
        };         

        for (int h = 0; h < 5; h++)
        {
            temp += "\n";
            for (int w = 0; w < 5; w++)
            {
                temp += aryMap[h, w];
            }
        }
        Console.SetCursorPosition(0, 0);
        Console.Write(temp);
        string test = Console.ReadLine();
   }

SOLUTION

I ended up using Malison which is a library for doing console-style interfaces in C#. Works great, and now I don't have to create my own console.

http://bitbucket.org/munificent/malison/wiki/Home

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-10-08 20:29:11

您可以输出 ANSI 转义序列,也可以使用 Control.ForegroundColor 和 Console.BackgroundColor 属性在写入字符之前设置该属性。

如果您需要在单个调用中进行写入,我建议使用 ANSI 转义序列。

You can either output ANSI escape sequences, or use the Control.ForegroundColor and Console.BackgroundColor properties to set the property before writing your character.

I would suggest the ANSI escape sequences if you need to make the write in a single call.

离笑几人歌 2024-10-08 20:29:11

此代码运行良好: http://www.daniweb.com/code/snippet216395.html
我确信您可以修改它以满足您的需要。

This code works nicely: http://www.daniweb.com/code/snippet216395.html
I'm sure you can modify it to fit what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文