I went through your source code and I think the probelm is with the color definition within the dictionary.
If you observe carefully, your dictionary value for a color is something like \033[1,30m for white color. However it should be \033[1;30m. Note that you are using a ',(comma) character instead of ';(semicolon) character. As a test I created a subset of the color dictionary and ran these tests.
>>> color = {'white' :'\033[1;37m', 'yellow':'\033[1;33m', 'off' : '\033[0;0m'}
>>> print color['white'] + 'string' + color['off']
string #this string is white in color
>>> print color['yellow'] + 'string' + color['off']
string #this string is yellow in color
>>> color['yellow'] = '\033[1,33m' #incorrect color code - using a , character instead of ;
>>> print color['yellow'] + 'string' + color['off']
string #prints the string in console default color i.e. not in yellow color
>>>
Hope this helps
Answer from Prahalad Deshpande on Stack OverflowPython: octal escape character \033 from a dictionary value translates in a print statement to a UTF-8 character instead - Stack Overflow
Python colored text to the terminal - Stack Overflow
printing - Python output complex line with floats colored by value - Stack Overflow
python - How do I print colored text to the terminal? - Stack Overflow
ANSI Escape Codes
ANSI escape codes allow you to do plenty of beautiful things in the terminal. You can change the color of the font, font style (bold, italic, underlined, etc...), and you can also move the cursor to overwrite previous text, to make text change dynamically.
Building codes
Basically, the codes are built like this:
\033[XXXm
where XXX is a series of semicolon-separated parameters.
- they start with
\033[(or any of the codes specified above + [) - then, the contain an arbitrary number of integers separated by ;
- and they end with m
The code containing only 0 (being \x1B[0m) will reset any style property of the font.
Most of the time, you will print a code changing the style of your terminal, then print a certain string, and then, the reset code.
Here are the codes that you can use to change the color of the font, or the color of the background.
| Color | Font code | Background code |
|----------------------------------------|------------------|------------------|
| Black | \x1B[30m | \x1B[40m |
| Red | \x1B[31m | \x1B[41m |
| Green | \x1B[32m | \x1B[42m |
| Yellow | \x1B[33m | \x1B[43m |
| Blue | \x1B[34m | \x1B[44m |
| Magenta | \x1B[35m | \x1B[45m |
| Cyan | \x1B[36m | \x1B[46m |
| White | \x1B[37m | \x1B[47m |
| Any palette color (with V in [0-255]) | \x1B[38;5;Vm | \x1B[48;5;Vm |
| Any RGB color (with values in [0-255]) | \x1B[38;2;R;G;Bm | \x1B[48;2;R;G;Bm |
- List of ANSI color escape sequences
- Build your own Command-Line with ANSI escape codes
You are dealing here with ANSI escape codes.
All follow the pattern \033[XXXm. You can have multiple instructions related to that, not only the one you listed.
For color codes in terminals, it depends of the system. The most basic terminals have a set of 8 different colors:
- Black: \u001b[30m
- Red: \u001b[31m
- Green: \u001b[32m
- Yellow: \u001b[33m
- Blue: \u001b[34m
- Magenta: \u001b[35m
- Cyan: \u001b[36m
- White: \u001b[37m
- Reset: \u001b[0m
You can use ASCII color codes at the beginning of your print to change the color, as an example '\033[91m' for RED and '\033[94m' for BLUE.
e.g
if array[0][i] > 7:
print '\033[94m' + oreName[i]+"= %.3f \t 5"%(array[0][i])
elif array[0][i] < 7:
print '\033[91m' + oreName[i]+"= %.3f \t 5"%(array[0][i])
You can read about ASCII escape codes here.
Edit: Added additional example code.
Here is a list of some common colors you can use:
Red = '\033[91m'
Green = '\033[92m'
Blue = '\033[94m'
Cyan = '\033[96m'
White = '\033[97m'
Yellow = '\033[93m'
Magenta = '\033[95m'
Grey = '\033[90m'
Black = '\033[90m'
Default = '\033[99m'
Additionally as mentioned in the comment. You can chain these to get different colors on the same line.
print '\033[91m' + 'Red' + "\033[99m" + 'Normal' + '\033[94m' + 'Blue
You could even do a function.
# Store a dictionary of colors.
COLOR = {
'blue': '\033[94m',
'default': '\033[99m',
'grey': '\033[90m',
'yellow': '\033[93m',
'black': '\033[90m',
'cyan': '\033[96m',
'green': '\033[92m',
'magenta': '\033[95m',
'white': '\033[97m',
'red': '\033[91m'
}
def print_with_color(message, color='red'):
print(COLOR.get(color.lower(), COLOR['default']) + message)
print_with_color('hello colorful world!', 'magenta')
print_with_color('hello colorful world!', 'blue')
for Easy copy-paste after print:
for style in range(8):
for fg in range(30,38):
s1 = ''
for bg in range(40,48):
myform = ';'.join([str(style), str(fg), str(bg)])
skel = '\x1b[{};{};{}m'.format(myform.split(';')[0],myform.split(';')[1],myform.split(';')[2])
s1 += f'\x1b[{myform}m {repr(skel)} \x1b[0m'
print(s1)
print('\n')
This code print multiple pretty lines with different colors. example:
'\x1b[5;31;40m' '\x1b[5;31;41m' '\x1b[5;31;42m' '\x1b[5;31;43m' for easy copy-paste to code.
After print add to you code:
mycolor = '\x1b[5;35;43m'
rest = '\x1b[0m'
and print for preview:
print(f"{mycolor}Hello world!{rest}")
for preview: Stack attach
This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some Python code from the Blender build scripts:
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
To use code like this, you can do something like:
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)
Or, with Python 3.6+:
print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
This will work on unixes including OS X, Linux and Windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ANSI codes for setting the color, moving the cursor, and more.
If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction.
If you are not using extended ASCII (i.e., not on a PC), you are stuck with the ASCII characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ASCII character set, you have many more options. Characters 176, 177, 178 and 219 are the "block characters".
Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets).
The Text Mode Demo Contest has more resources for doing graphics in text mode.
There is also the Python termcolor module. Usage is pretty simple:
from termcolor import colored
print(colored('hello', 'red'), colored('world', 'green'))
It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...
To get the ANSI codes working on windows, first run
os.system('color')