首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >彩色输出python

彩色输出python
EN

Stack Overflow用户
提问于 2018-07-21 04:05:02
回答 1查看 525关注 0票数 1

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

代码语言:javascript
复制
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的值以查看格式化是如何工作的。所以不管怎样,在我完成游戏后,我想添加一些颜色来使游戏在终端中更容易理解,所以我编辑了我的代码,如下所示:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-21 04:10:24

有两个原因让你的颜色编码变得混乱。

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

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

代码语言:javascript
复制
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for col in range(4)]

代码语言:javascript
复制
widths = [max(len(str(nlist[row * 4 + col]))+21 for row in range(4)) + 2 for col in range(4)]

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

example

代码语言:javascript
复制
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")

对于您的代码,您可以将其设置为

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51449636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档