c++ console colors

hello! I am trying to make a c++ ASCII console game but I am having a lot of trouble with colors! nothing i have tried worked. Any help is appreciated!!! I want to be able to change the color of different characters like enemies char(2) and the player char(1) and other elements in the game
Here is link to game code. it is too long to keep here! http://pastebin.com/JrxakMv7

Last edited on
@ProGrammarz

Here's a small program that shows how to change text color, when needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Colored Text on Paper.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using std::cout;
using std::endl;
using std::cin;
using std::string;

void ClearScreen();

#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

int main()
{
	string Color[16] = { "Black", "Dark Blue", "Dark Green", "Dark Cyan", "Dark Red", "Dark Magenta", "Dark Yellow", "Light Gray",
		"Dark Gray", "Light Blue", "Light Green", "Light Cyan", "Light Red", "Light Magenta", "Light Yellow", "White" };
	int i, j;

	for (j = 0; j<16; j++)
	{
		for (i = 0; i<16; i++)
		{
			if (j == i) // If text color is same as paper color
			{
				text();
				cout << "---> *** Place-holder --- Text same color as background *** <---" << endl;
				i++; // Skip to next color
			}
			if (i<16) // If not white on white, then continue
			{
				text(i on j);
				cout << Color[i] << " on " << Color[j] << " (Text color=" << i << "+Background color=" << j * 16 << " (" << j << "*16)=" << i + (j * 16) << ")" << endl;
			}
		}
	}
	text(); // Able to use names here, because of enum Colors{};

	cout << endl << endl << "Where a number is skipped, the text would be same color as the backgound.." << endl;
	cout << "To use color, add to your list of ";
	text(light_red on black);
	cout << "#include";
	text();
	cout << "'s, if not already used.." << endl;
	text(light_red on black);
	cout << "#include <windows.h>";
	text();
	cout << "." << endl << "Declare the following, afterwards.." << endl;
	text(light_red on black);
	cout << "HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);" << endl;
	text();
	cout << "and use the folowing, whenever you wish to change colors.." << endl;
	text(light_red on black);
	cout << "SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );" << endl;
	text();
	cout << "Or examine the '";
	text(light_red on black);
	cout << "void text(int text_color on int paper_color);";
	text();
	cout << "' function," << endl << "at the top of this program. Works great, doesn't it ??" << endl;
	cout << "Just make sure to add '";
	text(light_red on black);
	cout << "#define on ,";
	text();
	cout << "', if you want to use the '";
	text(light_red on black);
	cout << "on";
	text();
	cout << "' word," << endl;
	cout << "instead of a comma, to separate the two color choices." << endl << endl;
	cout << "Press enter to exit.." << endl;
	cin.clear();
	cin.sync();
	cin.get();
	ClearScreen();
	return 0;
}


To use, add the color enum to your code, and the void text(int text_color = 7 on int paper_color = 0) function at top above int main().
In a for loop where you are printing out the map, you can have.
1
2
3
4
5
6
7
8
if (map[x][y] == '#') //wall ? 
text(dark_gray on light_yellow)
if (map[x][y] == '\x02') // person
text(white on light_yellow);
// etc. for anything else in your map arrays
else
text(light_gray on light_yellow);// Or whatever you're wanting the normal colors to be
cout << map[x][y];


Look over the program I have shown, and you should be able to understand how to change the text colors where ever they appear in you program.

Ask if you need more help.


closed account (E0p9LyTq)
Add color to your console

http://www.cplusplus.com/articles/Eyhv0pDG/

Just include the header file and you can get colorful in your console.
@whitenite1
I tried your code however it changes the color of the whole console when I do if statements. I don't understand why it works for you but not me. Your code only works if I cout something onto the screen after using text() function. For example I do this:
1
2
3
4
5
6
if(map[y][x] == '@'){
//change the color to yellow
}
if(map[y][x] == '#'){
//change the color to white
}


The entire console becomes white on black.

even when I use else statements it always turns the entire console into a different color. Is there anyway I can cout an '@' symbol into the map array? Maybe that might work but I don't know how to. Thanks for helping!!!
@ProGrammarz

Not sure if you are doing it correctly, since you didn't show too much of what you actually did. Here is the way I would show one of the maps. Is this the way you were doing it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include "string"
#include <Windows.h>

using namespace std;

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

char map[10][21] = {
	"####################",
	"#@     99   # #   9#",
	"#### #   2 9# # 2  #",
	"#  # ##### ## 2  9 #",
	"#9   999      ######",
	"#9 ####### ##2     #",
	"#9 #2      #    99 #",
	"#k ####  # ### #####",
	"# 2   9  # 9     2!#",
	"####################" };


int main(array<System::String ^> ^args)
{
	for (int y = 0; y < 10; y++)
	{
		for (int x = 0; x < 20; x++)
		{
			if (map[y][x] == '#')
				text(light_gray on dark_gray);
			else if (map[y][x] == '@')
				text(white on dark_gray);
			else if (map[y][x] == '9')
				text(light_yellow on dark_gray);
			else if (map[y][x] == '2')
				text(black on dark_gray);
			else if (map[y][x] == '!')
				text(light_green on dark_gray);
			cout << map[y][x];// cout the one char in the map array, after 
       // changing color
		}
		cout << endl;
	}
	cout << endl<< endl;
}
@whitenite1

That worked!!!!! I can't thank you enough man!!!!!!
Topic archived. No new replies allowed.