gpt4 book ai didi

python - 彩色输出python

转载 作者:行者123 更新时间:2023-11-30 22:11:20 25 4
gpt4 key购买 nike

所以我找到了一些格式化 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

最佳答案

颜色编码困惑的原因有两个。

1) 您没有为 0 定义颜色编码,因此代码中的 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

关于python - 彩色输出python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com