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!