开发者

How can I create an array of Cha info that includes attributes like foreground color?

开发者 https://www.devze.com 2023-01-24 20:51 出处:网络
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

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 s开发者_StackOverflow社区omething 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号