Assigning colors to individual characters in an array

My problem seems simple yet has confused me for quite a while. I've seen some help on this site but the closest one that I got to the question was never answered.

Basically I am making a console RPG (isn't every beginner nowadays?) and I am having an incredible amount of trouble assigning specific characters their own color. e.g. monsters being red, cash being green, etc.

Here is my code and I'm praying you all can show me some way to do this so that I don't have to overhaul my code lol.

Thanks in advance and God bless.
~Methodius~

EDIT: I took out Left, Right, and Down controls because of character constraint.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <windows.h>


//%%%%%%%%%%%%%% COLOR CALL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void setcolor(unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}
//%%%%%%%%%%%%%% END COLOR CALL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


using namespace std;

char Map[3][10][20] = { "###################",   // MAP 1
                        "#                 #",
                        "#   * * *  +      #",
                        "#   * *           #",
                        "#   * *    $      #",
                        "#   * *           #",
                        "#   * *     +     #",
                        "#                 #",
                        "#@                :",
                        "################:##",
                        "###################",   // MAP 2
                        "#                 #",
                        "#       ^    !    #",
                        "#   !     ^       #",
                        "#         +       #",
                        "#                 #",
                        "#        &   &    #",
                        "#      !          #",
                        ":                 #",
                        "###################",
                        "################:##",   // MAP 3
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "#                 #",
                        "###################" };

//%%%%%%%%%%%% GLOBAL VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int Gamespeed = 150;
int Level = 1;
bool stopgame = false;

//%%%%%%%%%%%% HUD VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int HP = 100;
int MaxHP = 100;
int Cash = 0;
int MaxCash = 99;
int InMap = 0;

//%%%%%%%%%%%% VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%% LEVEL 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    while(stopgame == false)
    {
        system("cls");

        if (HP >= MaxHP)
        {
            HP = MaxHP;
        }

        if (Cash >= MaxCash)
        {
            Cash = MaxCash;
        }

        if (HP <= 0)
        {
            cout << "GAME OVER" << endl;
            Sleep(1000);
            system("cls");
            return 0;
        }

        for (int y = 0; y < 10; y++)
        {
            cout << Map[InMap][y] << endl;
        }

//%%%%%%%%%%%%%%%%%%%%% HUD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        cout << "HP: " << HP << "/" << MaxHP << endl;
        cout << "Cash: " << Cash << "/" << MaxCash << endl;
//%%%%%%%%%%%%%%%%%%%%% HUD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        for (int y = 0; y<10; y++)
        {
            for (int x = 0; x<20; x++)
            {

                switch(Map[InMap][y][x])
                {
                    case '#':
                    {
                        Map[InMap][y][x] = 219;
                    }

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLAYER CONTROLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

                    case '@':
                    {

                //%%%%% UP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                        if (GetAsyncKeyState(VK_UP) != 0 )
                        {
                            int y2 = (y-1);

                            switch(Map[InMap][y2][x])
                            {

                                case ' ':
                                {
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;

                                case ':': //Door
                                {
                                    Map[InMap][y][x] = ' ';
                                    y = 8;
                                    InMap -= 2;
                                    Map[InMap][y][x] = '@';
                                }break;

                                case '*': //Monster
                                {
                                    HP -= 10;
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;

                                case '+': //Health Upgrade
                                {
                                    HP += 5;
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;

                                case '&': //MaxHP Upgrade
                                {
                                    MaxHP += 15;
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;

                                case '$': //NPC
                                {
                                    setcolor(10); cout << "\nIt's dangerous to go alone!\n\n" << endl ;setcolor(7);
                                    system("pause");
                                }break;

                                case '!': //Cash Upgrade
                                {
                                    Cash += 3;
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;

                                case '^': //MaxCash Upgrade
                                {
                                    MaxCash += 200;
                                    Map[InMap][y][x] = ' ';
                                    y -= 1;
                                    Map[InMap][y2][x] = '@';
                                }break;
                            }
                        }
                

//%%%%%%%%%%%%%%%%%%%%%%%%%% END PLAYER CONTROLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


                            }
                        }

                    }break;
                }
            }
        }
        Sleep(Gamespeed);
    }
}
Last edited on
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
#include <iostream>
#include <windows.h>

char print_char(const char inChar);
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
int main()
{
	char arr[] = "Hello World!";

	for (auto element : arr)
		std::cout << print_char(element);


	std::cin.ignore();
	return 0;
}
	
	
char print_char(const char inChar)
{
	switch (inChar)
	{
	case 'H':
		SetConsoleTextAttribute(hcon, FOREGROUND_BLUE);//darkblue
		break;
	case 'e':
		SetConsoleTextAttribute(hcon, FOREGROUND_GREEN);//darkgreen
		break;
	case 'l':
		SetConsoleTextAttribute(hcon, FOREGROUND_RED);//darkred
		break;
	case 'o':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_BLUE);//blue
		break;
	case 'W':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);//Yellow
		break;
	case 'r':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//cyan
		break;
	case 'd':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//white
		break;
	case '!':
		SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//darkgray
		break;
	default:
		SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//darkgray
		break;

	}

	return inChar;
}
Last edited on
If you do it that way you may want to have a default case also. Unless every single character has it's own color.

By the way OP you should look into a Graphical library instead of the console for a game. Graphics may seem hard but they are pretty trivial. The hardest part for me is actually drawing the images.

Oh and you should avoid global variables.
Last edited on
If you do it that way you may want to have a default case also. Unless every single character has it's own color.

i was just about to edit my post and add a default :)
I'm just wondering how to give each character it's own color with the code I've got. I would hate to have to redo all the code that I already have lol.
it only requires a few simple changes
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <windows.h>


//%%%%%%%%%%%%%% COLOR CALL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void setcolor(unsigned short color)
{
	HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hcon, color);
}
//%%%%%%%%%%%%%% END COLOR CALL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


using namespace std;

char print_char(const char inChar);
char Map[3][10][20] = { "###################",   // MAP 1
"#                 #",
"#   * * *  +      #",
"#   * *           #",
"#   * *    $      #",
"#   * *           #",
"#   * *     +     #",
"#                 #",
"#@                :",
"################:##",
"###################",   // MAP 2
"#                 #",
"#       ^    !    #",
"#   !     ^       #",
"#         +       #",
"#                 #",
"#        &   &    #",
"#      !          #",
":                 #",
"###################",
"################:##",   // MAP 3
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"###################" };

//%%%%%%%%%%%% GLOBAL VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int Gamespeed = 150;
int Level = 1;
bool stopgame = false;

//%%%%%%%%%%%% HUD VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int HP = 100;
int MaxHP = 100;
int Cash = 0;
int MaxCash = 99;
int InMap = 0;

//%%%%%%%%%%%% VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main()
{
	//%%%%%%%%%%%%%%%%%%%%%%%%%%% LEVEL 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	while (stopgame == false)
	{
		system("cls");

		if (HP >= MaxHP)
		{
			HP = MaxHP;
		}

		if (Cash >= MaxCash)
		{
			Cash = MaxCash;
		}

		if (HP <= 0)
		{
			cout << "GAME OVER" << endl;
			Sleep(1000);
			system("cls");
			return 0;
		}

		for (int y = 0; y < 10; y++)
		{
			for (int i = 0; i < 20; ++i)
				cout << print_char(Map[InMap][y][i]);
			cout << endl;
		}

		//%%%%%%%%%%%%%%%%%%%%% HUD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		cout << "HP: " << HP << "/" << MaxHP << endl;
		cout << "Cash: " << Cash << "/" << MaxCash << endl;
		//%%%%%%%%%%%%%%%%%%%%% HUD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

		for (int y = 0; y<10; y++)
		{
			for (int x = 0; x<20; x++)
			{

				switch (Map[InMap][y][x])
				{
				case '#':
				{
							Map[InMap][y][x] = 219;
				}

					//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLAYER CONTROLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

				case '@':
				{

							//%%%%% UP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
							if (GetAsyncKeyState(VK_UP) != 0)
							{
								int y2 = (y - 1);

								switch (Map[InMap][y2][x])
								{

								case ' ':
								{
											Map[InMap][y][x] = ' ';
											y -= 1;
											Map[InMap][y2][x] = '@';
								}break;

								case ':': //Door
								{
											  Map[InMap][y][x] = ' ';
											  y = 8;
											  InMap -= 2;
											  Map[InMap][y][x] = '@';
								}break;

								case '*': //Monster
								{
											  HP -= 10;
											  Map[InMap][y][x] = ' ';
											  y -= 1;
											  Map[InMap][y2][x] = '@';
								}break;

								case '+': //Health Upgrade
								{
											  HP += 5;
											  Map[InMap][y][x] = ' ';
											  y -= 1;
											  Map[InMap][y2][x] = '@';
								}break;

								case '&': //MaxHP Upgrade
								{
											  MaxHP += 15;
											  Map[InMap][y][x] = ' ';
											  y -= 1;
											  Map[InMap][y2][x] = '@';
								}break;

								case '$': //NPC
								{
											  setcolor(10); cout << "\nIt's dangerous to go alone!\n\n" << endl; setcolor(7);
											  system("pause");
								}break;

								case '!': //Cash Upgrade
								{
											  Cash += 3;
											  Map[InMap][y][x] = ' ';
											  y -= 1;
											  Map[InMap][y2][x] = '@';
								}break;

								case '^': //MaxCash Upgrade
								{
											  MaxCash += 200;
											  Map[InMap][y][x] = ' ';
											  y -= 1;
											  Map[InMap][y2][x] = '@';
								}break;
								}
							}


							//%%%%%%%%%%%%%%%%%%%%%%%%%% END PLAYER CONTROLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


				}
				}

			}break;
		}
	}

	Sleep(Gamespeed);
}

char print_char(const char inChar)
{
	HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
	switch (inChar)
	{
		break;
	case '$':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_GREEN);//darkgreen
		break;
	case '*':
		SetConsoleTextAttribute(hcon, FOREGROUND_RED);//darkred
		break;
		break;
	case '#':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);//Yellow
		break;
	case '+':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//cyan
		break;
	case '@':
		SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//white
		break;
	case '!':
		SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//darkgray
		break;
	default:
		SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);//darkgray
		break;

	}

	return inChar;
}
Last edited on
Mr/Mrs. Yanson, you're a life-saver. Thank you so much for your help.

I noticed your changes and I was wondering if you could take one more minute to explain what exactly your changes did.

Namely this line:

cout << print_char(Map[InMap][y][i]);

I understand it is printing something but what is the [i]?

Thank you again so much, you've helped alot.

God bless,
~Methodius~
in your code you create a three dimensional array char Map[3][10][20]. When you print the array you only use two dimensions cout << Map[InMap][y] << endl;. This Map[inMap][y] results in a pointer to an array. My print_char() function takes a char as a parameter not a pointer to an array. So i needed to use the third dimension to access the individual characters in the array.

I understand it is printing something but what is the [i]

The i specifies the individual character within the array that is being accessed.


run this code and you will see what i mean. The first cout statement prints an array the second prints a char.
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
#include <iostream>
using namespace std;

char Map[3][10][20] = { "###################",   // MAP 1
"#                 #",
"#   * * *  +      #",
"#   * *           #",
"#   * *    $      #",
"#   * *           #",
"#   * *     +     #",
"#                 #",
"#@                :",
"################:##",
"###################",   // MAP 2
"#                 #",
"#       ^    !    #",
"#   !     ^       #",
"#         +       #",
"#                 #",
"#        &   &    #",
"#      !          #",
":                 #",
"###################",
"################:##",   // MAP 3
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"#                 #",
"###################" };

int main()
{
	std::cout << Map[0][0] << std::endl;//this prints an array

	
	std::cout << Map[0][0][0] << std::endl;//this prints a char

	

	std::cout << std::endl;

	std::cin.ignore();
	return 0;
}

Okay, that makes sense! Thank you. One more question:

I noticed before I added your code that I didn't have a whole lot of flicker. There was some but a negligible amount. But now I've got a considerable amount of flicker.

Now I know this sparks a huge debate between using "system()" and other things. But I was wondering if you had any other ways of reducing or eliminating the flicker.

Again, thank you so much for your help.

God bless,
~Methodius~
Topic archived. No new replies allowed.