Storing color in a string c#

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Storing color in a string c#

Post by mattykins »

Is it at all possible to store color inside of a string variable? so lets say for instance i have a game board that looks like this

Code: Select all

#################
#                          
#  P                      
#                          
#################
And all the spaces are normal color except for the P, say i want the P to be blue. Is there anyway to store this inside of a string variable? Because right now my board update looks like:

Code: Select all

static public void boardWrite()
        {
            Console.Clear();
            board.Remove(0, board.Length);
            for (int y = 0; y < 20; y++)
            {
                int i = 0;
                for (int x = 0; x < 80; x++)
                {
                    i++;
                    
                    if (i < 80)
                    {
                        board.Append(tiles[x, y]);
                    }
                    else
                    {
                        board.Append("\n");
                        i = 0;
                    }
                    
                }

            }
            Console.Write(board);
            

        }
I use stringbuilder to create one long string variable with a \n for a new line every 80 characters, however this method does not let me assign color to individual characters in the string. Any suggestions to either a new board writing method or a way to store color inside the string method?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Storing color in a string c#

Post by Callan S. »

How are you constructing the strings?

I'd think that if instead of recording how many characters there are, as you build the string you have the code just record characters and spaces in an integer and when it gets to 80 make a new row. That way you can just stick in the colour changes.

If you're building it manually, you could have it be that every two spaces is one character, the first space giving the colour (coded to a single character), the second is the character to show. Then have the program go through and convert the colour codes to html colour codes before printing it.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Storing color in a string c#

Post by mattykins »

Hmm, I'm a bit confused, how would i store the color codes?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Storing color in a string c#

Post by Jackolantern »

I believe you could still use the Color structure in a console application. It is part of System.Drawing, which is usually for GUI building, but I don't see why you couldn't import it (which may require an assembly reference; not sure since the only time I have used it VS added it for me) and use it since it is just a regular struct and doesn't actually depend on any GUI frameworks.
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Storing color in a string c#

Post by Callan S. »

I guess it's not spitting it onto a web page, is it? I was kinda thinking that for some reason.

Well, I'd go with the prior character designating the colour, then the program goes through each character and figures the right colour to use to print the next character. A string with

"r#g$b@"

would come out as

#$@

where r=red, g=green, b=blue

C# has functions for getting a sub string from a string, right? You'd use a while loop, each time grabbing character at X for the colour, then X+1 for the character to show, then incrementing X by 2 until you reach the end of the string.

I'm just guessing that I understand the problem. I don't, for example, know the commands in #C to change the font colour used in outputting a string.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Storing color in a string c#

Post by mattykins »

For using system.drawing how would i use that inside a console? I modified the example on MDSN in the link you gave me but still can't figure out how to use that to set different console colors.

FYI Console Colors are normally changed by

Code: Select all

Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("Hello World!"); //This would output Hello World! in blue font on a console.
Console.ResetColor();

Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("Hello World!"); //This would output Hello World in the default font color with a blue background
Console.ResetColor();
//And you can use any combination of background and foreground color for different font colors w/ different backgrounds.
If that helps.

Thanks though everyone :)

EDIT:Oh and its not spitting it out on to a webpage. And i'm not sure if C# has that function...It probably does but idk.
Post Reply

Return to “Beginner Help and Support”