gpt4 book ai didi

python - 如何使用数组中的 termcolor 使用 Python 打印不同的颜色?

转载 作者:行者123 更新时间:2023-11-28 16:24:47 26 4
gpt4 key购买 nike

我是 python 的新手,并且编写了我的第二个游戏,因为我觉得这是学习一门新语言的最佳方式。我的代码如下:

代码:

#!/usr/bin/env python

from random import randint
from termcolor import colored
import os
import sys
import time
clear = lambda : os.system('tput reset')

clear()

board = []

board_size=5

for x in range(board_size):
board.append(["[W]"] * board_size)

def print_board(board):
for row in board:
print colored(" ".join(row),"cyan")

#print "Let's play Battleship!"
print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board) +1
ship_col = random_col(board) +1

# Prints where the ship is placed
# Do the right and don't cheat!
# print ship_row
# print ship_col


print colored("\nNot bombed: ","yellow") + colored("[W]","cyan")
print colored("Has been bombed: ","yellow") + colored("[","cyan") + colored("X","red") + colored("]\n","cyan")

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

counter=0

state=True

while bool(state):

counter=int(counter)+1

if guess_row == ship_row and guess_col == ship_col:
clear()
print "\n\n Congratulations! You sunk my battleship!\n\n"
print "You got it right after " + str(counter) + " guesses."
state=False
time.sleep(2)
clear()
sys.exit()

else:
if (guess_row -1 < 0 or guess_row > board_size) or (guess_col -1 < 0 or guess_col > board_size):
print "Oops, that's not even in the ocean."
counter=int(counter)-1
time.sleep(1)
clear()

elif(board[guess_row-1][guess_col-1] == "[X]"):
print "You guessed that one already."
counter=int(counter)-1
time.sleep(1)
clear()

else:
print "You missed my battleship!"
clear()
board[guess_row-1][guess_col-1] = "[X]"
#counter=int(counter)+1


print_board(board)

print colored("\nNot bombed: ","yellow") + colored("[W]","cyan")
print colored("Has been bombed: ","yellow") + colored("[","cyan") + colored("X","red") + colored("]\n","cyan")

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

我想,当用户猜测我只想让字母 X 变成红色时,正如 key 所暗示的那样。

当前输出:

enter image description here

注意只有“X”是红色的,方括号是青色的,这基本上是我想在游戏中实现的。

理想输出:

enter image description here

问题:

如何打印如上?

最佳答案

问题代码为:

print colored(" ".join(row),"cyan")

你需要:

print ' '.join(colored(element, 'cyan') if element != 'X'
else colored(element, 'red')
for element in row)

编辑

更一般地说,您可以根据字符查找颜色。一种常用工具是使用 Python 的 dict,它提供键和值之间的映射。

>>> color_key = {
... 'X': 'red',
... 'H': 'magenta'}
>>> color_key['X']
'red'

如果您使用 get,您可以为缺少的键提供一个默认值:

>>> color_key.get('[', 'cyan')
'cyan'

否则你会抛出异常:

>>> color_key['[']
...KeyError...

用法:

print ' '.join(colored(element, color_key.get(element, 'cyan')
for element in row)

关于python - 如何使用数组中的 termcolor 使用 Python 打印不同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37476511/

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