Colours on map

Hey guys,

I just made a small game where you have some monsters running around randomly, some trees and some gold piles.

I was wondering how to colour items, player and monsters certain colours.

I want it to be so the trees are green, the gold is yellow, etc...

All the symbols for the trees, player, monsters are stored as a char

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

....

char playerChar = '@';
char goldChar = '$';
char treeChar = 'T';

....

int main()
{
    ....
}


Thanks heaps
Standard C++ doesn't have ways to change the text colour. You could use a library like the WinAPI on windows or ncurses on *nix. You might also be able to use escape codes: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Could you show me some examples please?
I would have used

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

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

void setcolor(unsigned short color)
{ 
     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); 
     SetConsoleTextAttribute(hCon,color); 
} 

char playerChar = '@';
char goldChar = '$';
char treeChar = 'T';

....

int main()
{
    ....
    setcolor(47); // white
    cout << playerCar;
    setcolor(32);
    cout << goldChar;
    setcolor(27);
    cout << treeChar;
    setcolor(47);
}


Play around with the stecolor numbers to see the different colours for background and text colour.
Here is a small example using ANSI escape codes:
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
	std::cout << "\033[;1;32mThis text is green.\033[0m" << std::endl;
	std::cout << "\033[;1;34mThis text is blue.\033[0m" << std::endl;
	std::cout << "\033[;1;31mThis text is red.\033[0m" << std::endl;
}


I'm not really good at this so you better search to find more information.

Using a library is probably a more recommended way.
Last edited on
Last edited on
If you're going to use an external library, you might as well go the whole 9 yards and get one that does graphics. Console games are sux0rs.

http://www.sfml-dev.org
I agree with Disch
Topic archived. No new replies allowed.