0

I'm writing a small RPG in Python, and I want my different world objects to have varying colors. I've tried exploring colorama and some similar modules; however, the string always prints as a small box with some numbers and letters in it, instead of, let's say, a blue '~' character for a water tile.

I create my world arrays with a variable that looks like this:

worldArray = [[' ' for i in range(10)] for i in range(10)]

I then add world objects to it through a few different functions, to structure a random-esque world environment, like this one:

def replacetiles(self, grid, row, a, b, char, count):
        while count:
            aShift = 2
            row_col_dict = {row: (a, b)}
            for row in row_col_dict.keys():
                startPos, endPos = row_col_dict[row]

                for i in range(startPos, endPos):
                    grid[row][i] = char
                    
            row -= 1
            if aShift == 0:
                a += random.randint(1, 2)
            
            if count < 3:
                b -= (b / 2)
            
            b += random.randint(0, 1)
            aShift -=1
            count -= 1

This is of course all stored within a class object. This function is how I create randomized oceans within the world, by calling replacetiles(gamemap, 9, 0, 5, '~', 3) which would replace a few of the indexes with '~'.

How would I be able to change the output color of the '~' within the game map?

EDIT: My question seems much more specific than the other thread listed. The problem I am having, is when I print text through colorama, or any of the answers below or in the other thread, in a stand alone file simply printing colored text, it works. However, when I use that same code to change the color of the char argument in replacetiles, I get this odd box icon, regardless of the method I used to change the color of the string text.

Here is a screen shot: enter image description here

Community
  • 1
  • 1
Mochamethod
  • 276
  • 4
  • 14

2 Answers2

0

You can add ASCII escape codes to the strings you add to your worldArray. To make your water blue, just replace grid[row][i] = char with grid[row][i] = "\033[34m" + char + "\033[0m".

For more color codes, see: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

You can then print your world using:

print('\n'.join(' '.join(row) for row in worldArray))
user6457549
  • 3
  • 1
  • 2
0

You can use this package called termcolor, instead of hard coding the values.

import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
wind85
  • 487
  • 2
  • 5
  • 11
  • Also returns the weird box icon. If I run this in its own file, I get the highlighted red text. However, if I try to attach similar code into my function to change the `'~'` blue, I see the box icons. Any idea why that might be? – Mochamethod Jun 13 '16 at 01:20
  • What terminal emulation are you using? – wind85 Jun 13 '16 at 01:26
  • 1
    I have a fresh install of the newest version of Linux Mint, using the standard terminal. I added a screenshot to the main post for clarification. – Mochamethod Jun 13 '16 at 01:29
  • It's an encoding problem, your are most likely trying to print a unicode character but your terminal doesn't support it. Try encoding the string or the single character with https://docs.python.org/3/library/stdtypes.html?highlight=encode#str.encode. – wind85 Jun 13 '16 at 01:34
  • No results from that. My code is now: `grid[row][i] = char.encode('utf-8', 'strict')` Is this correct? The `char` argument being: `colored('w', 'blue', attrs=['reverse', 'blink'])` I also changed the `'~'` symbol to a `'w'` instead to see if for some reason that would make a difference. – Mochamethod Jun 13 '16 at 01:59
  • Once you have encode it, are you still trying to print it colorama? Perhaps the issue lies in the library. If utf-8 doesn't work try ascii. – wind85 Jun 13 '16 at 07:27