【问题标题】:Colored output python彩色输出 python
【发布时间】:2018-07-20 20:05:02
【问题描述】:

所以我找到了一些格式化 2048 游戏板的代码,这样当一个数字超过 1 位时它看起来不会很乱:

nlist = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 128, 2, 2, 16, 16]
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
    print('|{:^{width}}'.format(nlist[i], width=widths[i % 4]), end = '')
    count += 1
    if count == 4:
        print("|\n" + '-' * width)
        count = 0
print("")

运行此代码以使事情更清晰,并更改 nlist 的值以查看格式化的工作方式。所以无论如何,在我完成游戏后,我想添加颜色以使游戏在终端中更容易理解,所以我将代码编辑为如下所示:

clist = nlist.copy()
for x in range(16):
    if clist[x] == 2:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 4:
        clist[x] = ' \033[1;37;105m' + str(clist[x]) + '\033[0m '
    if clist[x] == 8:
        clist[x] = ' \033[1;37;104m' + str(clist[x]) + '\033[0m '
    if clist[x] == 16:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 32:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 64:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 128:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 256:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 512:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 1024:
    clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 2048:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
    if clist[x] == 4096:
        clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for 
col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
    print('|{:^{width}}'.format(clist[i], width=widths[i % 4]), end = '')
    count += 1
    if count == 4:
        print("|\n" + '-' * width)
        count = 0

但是现在格式变得一团糟,我的电路板又一次看起来很丑。有没有一种可能的方法来改变这个代码,使它看起来像第一个代码,除了颜色(现在几乎所有的颜色都是一样的,因为时间的缘故。我稍后会改变它)。另外,有没有更简单的方法可以在条件语句中添加颜色?

编辑:

这是没有正确格式颜色的文件的链接:2048 that works(no colors)

这是颜色格式不正确的文件的链接:2048 that does not work(colors)

我运行代码的屏幕截图:Screen shot of messed up format

【问题讨论】:

  • 添加(正确的)ANSI 转义序列不应该改变可见字符的数量。因此,您的计数必须关闭。这是我第一次在 Stack Overflow 上问这个问题:您可以添加彩色文本的屏幕截图吗?
  • 我会在描述的整个代码中放一个链接
  • @J.Doe,已更新,希望对您有所帮助。

标签: python python-3.x colors


【解决方案1】:

颜色编码混乱的原因有两个。

1) 您没有为 0 定义颜色编码,因此在您的代码中,零与其他数字总是存在很大的格式差距。

2) 你的宽度函数没有随着颜色函数的变化而改变,但实际上彩色输出的字符串长度比没有的要大得多。因此,我建议您在代码中使用一个常数,例如 22 或更大的数字。 或者,您可以更改

widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for col in range(4)]

widths = [max(len(str(nlist[row * 4 + col]))+21 for row in range(4)) + 2 for col in range(4)]

为了简化 if 结构,我建议你使用字典来查找所需的颜色:

example:

WHITE_COLOR = "#ffffff"
BACKGROUND_COLOR_GAME = "#92877d"
BACKGROUND_COLOR_CELL_EMPTY = "#9e948a"
BACKGROUND_COLOR_DICT = {   2:"#eee4da", 4:"#ede0c8", 8:"#f2b179", 16:"#f59563", \
                            32:"#f67c5f", 64:"#f65e3b", 128:"#edcf72", 256:"#edcc61", \
                            512:"#edc850", 1024:"#edc53f", 2048:"#edc22e" }
CELL_COLOR_DICT = { 2:"#776e65", 4:"#776e65", 8:"#f9f6f2", 16:"#f9f6f2", \
                    32:"#f9f6f2", 64:"#f9f6f2", 128:"#f9f6f2", 256:"#f9f6f2", \
                    512:"#f9f6f2", 1024:"#f9f6f2", 2048:"#f9f6f2" }
FONT = ("Verdana", 40, "bold")
SCORE_FONT=("Verdana", 20, "bold")

对于你的代码,你可以让它像

nlist = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 128, 2, 2, 16, 16]
color_dict = {0:' \033[1;37;106m0\033[0m ',
              2:' \033[1;37;106m2\033[0m ',4:' \033[1;37;104m4\033[0m ',
              8:' \033[1;37;106m8\033[0m ',16:' \033[1;37;106m16\033[0m ',
              32:' \033[1;37;106m32\033[0m ',64:' \033[1;37;106m64\033[0m ',
              128:' \033[1;37;106m128\033[0m ',256:' \033[1;37;106m256\033[0m ',
              512:' \033[1;37;106m512\033[0m ',1024:' \033[1;37;106m1024\033[0m ',
              2048:' \033[1;37;106m2048\033[0m ',4096:' \033[1;37;106m4096\033[0m '}
count = 0
for i in range(16):
    print('|{:^{width}}'.format(color_dict[nlist[i]], width=22), end = '')   #This line is modified.
    count += 1
    if count == 4:
        print("|\n" + '-')
        count = 0

【讨论】:

  • 如何将其合并到我的代码中(我以前从未使用过字典?
  • @J.Doe,我不知道如何在不导入任何模块的情况下在 python 中实现颜色编码。在上面提供的链接中,部署了 tkinter 模块,颜色是从上面提供的字典中引用的。示例:如果分数 = 2。颜色=BACKGROUND_COLOR_DICT[分数]。颜色是“#eee4da”,它是灰色的。 color-hex.com/color/eee4da
  • 我从来没有说过我不能导入任何东西,但是这在我的条件语句中如何工作而不会使格式混乱
  • @Marcus.Aurelianus 他希望在终端输出中设置颜色格式,而不是在 GUI 应用程序中。他已经在问题中展示了如何做到这一点(使用 ANSI 转义序列)。当我正确理解问题时,用户想知道如何在不弄乱打印网格格式的情况下使用它。
  • @J. Doe,我怀疑你桌子的颜色搞砸了,因为它们都使用一种颜色 '\e[0;106m'。
【解决方案2】:

使用来自termcolor 模块的colored 进行颜色

from termcolor import colored
nlist = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 128, 2, 2, 16, 16]
color = {n:'cyan' for n in nlist}
color[4] = 'red'

widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 16 for col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
    print('|{:^{width}}'.format(colored(nlist[i], 'white', 'on_'+color[nlist[i]]), width=widths[i % 4]), end = '')
    count += 1
    if count == 4:
        print("|\n" + '-' * width)
        count = 0
print("")

输出 我确实看到以下颜色的输出

|  2  |  4   |  8   |  16  |
------------------------------------------------------------------------------------
| 32  |  64  | 128  | 256  |
------------------------------------------------------------------------------------
| 512 | 1024 | 2048 | 4096 |
------------------------------------------------------------------------------------
| 128 |  2   |  2   |  16  |
------------------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2011-11-05
    • 2015-09-01
    相关资源
    最近更新 更多