Printing with different colours

Hi everybody!

I am new to C++ and currently working on 1 of my assignments. The task for the assignment is to make a maze game. I've got most of the game already working, but 1 of the things that I would like to do is to print out characters in colour.

I have searched the web for a solution and have come across quite a few, but none of them fully worked sadly.

--
What I would like to do is to print the board characters in different colours, depending on what they are, please have a look at my renderBoard method:
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
void GameRenderer::renderBoard(Cell **m_pBoard, int nNewWidth, int nNewHeight)
{
	HANDLE  hConsole;
	int k;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, 7);	//6 - yellow, 2 - green, 4 - red, 7 - standard, 8 - grey
	system("cls");

	for(int nWidth = 0; nWidth < nNewWidth; nWidth++)
	{
		for(int nHeight = 0; nHeight < nNewHeight; nHeight++)
		{
			//print the game rack
			if(m_pBoard[nWidth][nHeight].getVisible() == true)
			{
				//if Cell is visible and has a number of mines surrounding it,
				//isplay its nMinesInRadius instead of standard char
				if(m_pBoard[nWidth][nHeight].getMinesInRadius() != 0)
				{
					//set the colour for the numbers to green
					//SetConsoleTextAttribute(hConsole, 2);
					cout << m_pBoard[nWidth][nHeight].getMinesInRadius();
				}
				else
				{
					if(m_pBoard[nWidth][nHeight].getChar == 'M')
					{
						//set the colour for the (M)ines to red
						//SetConsoleTextAttribute(hConsole, 4);
						cout << m_pBoard[nWidth][nHeight].getChar();
					}
					else
					{
						//set the colour for empty cells to white
						//SetConsoleTextAttribute(hConsole, 7);
						cout << m_pBoard[nWidth][nHeight].getChar();
					}
				}
				
			}
			else
			{
				//SetConsoleTextAttribute(hConsole, 8);
				cout << "-";
			}
			
		}
		cout << endl;
	}

	system("pause");
}


So as you can see, I am trying to print the game board, consisting of Cells, in colour, which depends on the character representation of each individual cell. However, SetConsoleTextAttribute doesn't work properly and sets the whole board to a specific colour, instead of 1 single character 8(

In other words, I am trying to achieve something like this:
http://i.imgur.com/H8pfV.png

Some advice would be greatly appreciated!
How about you remove // from lines 21 29 35 and 43? How does it look then?
...no comment, just no comment...

I added "//" since it doesn't work properly, and it didn't, but right before replying just now, I've tested it again and it worked... 8S

I am 100% positive that I've tested it before yesterday and got some very strange results and I have no idea why it works then ...

WEIRD!

thanks for the reply though 8D
This probably isn't going to get very many responses in the Beginners section. Most people posting in these forums probably don't have a clue about the kind of game development you're doing.
system("pause");
0.o
system("pause");
0.o


Haha no matter how much people try to spread the word, it still doesn't reach enough people...

@GRoss: please read this: http://www.cplusplus.com/forum/articles/11153/ and don't use system commands again plz :)

Cheers!
Thanks a lot for your replies!

I had no idea system(something) was bad.. We were told to use it in our course... I'll try switching over to the methods mentioned in that article.

And I know that 2D arrays are confusing - last assignment I've done, I have used custom made 2D arrays, and they were alright, 2D arrays are more confusing in a way, but the problems begin when using arrays within arrays, when your naming conventions are crap like mine 8/ - Still not sure which is better...
Topic archived. No new replies allowed.