![](/img/trans.png)
[英]How do I print separate multi-line ascii symbols side by side in output window
[英]How can I print my ascii cards side by side?
我使用 python 制作了一个简单的二十一点游戏。 我制作了游戏的其余部分,但我正在努力放入 ascii 卡,所以这只是代码的一小部分。 我尝试将 * len(phand) 放在追加行的末尾。 虽然这确实为我提供了多张卡片,但它似乎并没有让我编辑它们。
#Phand is the hand you draw so it could be any combination of cards
phand = ["2 of Hearts", "King of Diamonds", "Ace of Clubs"]
print(phand)
for xx in range(0, len(phand)):
pcarddisplay = []
pcarddisplay.append("┌─────────┐")
pcarddisplay.append("│{}{}. . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . {}. .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . .{}{}│")
pcarddisplay.append("└─────────┘")
x = ("│.", phand[xx][:1], ". . . .│")
pcarddisplay[1] = "".join(x)
x = ("│. . . .", phand[xx][:1], ".│")
pcarddisplay[7] = "".join(x)
if "Diamonds" in phand[xx]:
pcarddisplay[4] = "│. . ♦ . .│"
if "Clubs" in phand[xx]:
pcarddisplay[4] = "│. . ♣ . .│"
if "Hearts" in phand[xx]:
pcarddisplay[4] = "│. . ♥ . .│"
if "Spades" in phand[xx]:
pcarddisplay[4] = "│. . ♠ . .│"
print("\n".join(pcarddisplay))
这输出:
['2 of Hearts', 'King of Diamonds', 'Ace of Clubs']
┌─────────┐
│.2. . . .│
│. . . . .│
│. . . . .│
│. . ♥ . .│
│. . . . .│
│. . . . .│
│. . . .2.│
└─────────┘
┌─────────┐
│.K. . . .│
│. . . . .│
│. . . . .│
│. . ♦ . .│
│. . . . .│
│. . . . .│
│. . . .K.│
└─────────┘
┌─────────┐
│.A. . . .│
│. . . . .│
│. . . . .│
│. . ♣ . .│
│. . . . .│
│. . . . .│
│. . . .A.│
└─────────┘
我怎么能把这些打印出来?
您必须更改代码以生成一张卡片(这里是mk_card
函数)。
然后生成所有卡片块并使用zip
和join
的组合来生成行:
phand = ["2 of Hearts", "King of Diamonds", "Ace of Clubs"]
def mk_card(s):
pcarddisplay = []
pcarddisplay.append("┌─────────┐")
pcarddisplay.append("│{}{}. . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . {}. .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . .{}{}│")
pcarddisplay.append("└─────────┘")
x = ("│.", s[:1], ". . . .│")
pcarddisplay[1] = "".join(x)
x = ("│. . . .", s[:1], ".│")
pcarddisplay[7] = "".join(x)
if "Diamonds" in s:
pcarddisplay[4] = "│. . ♦ . .│"
if "Clubs" in s:
pcarddisplay[4] = "│. . ♣ . .│"
if "Hearts" in s:
pcarddisplay[4] = "│. . ♥ . .│"
if "Spades" in s:
pcarddisplay[4] = "│. . ♠ . .│"
return pcarddisplay
print('\n'.join(map(' '.join, zip(*(mk_card(c) for c in phand)))))
输出:
┌─────────┐ ┌─────────┐ ┌─────────┐
│.2. . . .│ │.K. . . .│ │.A. . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . ♥ . .│ │. . ♦ . .│ │. . ♣ . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . .2.│ │. . . .K.│ │. . . .A.│
└─────────┘ └─────────┘ └─────────┘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.