[英]How can I print my ascii cards side by side?
I made a simple blackjack game using python.我使用 python 制作了一个简单的二十一点游戏。 I made the rest of the game but i am struggling to put in the ascii cards, so this is just a fraction of the code.我制作了游戏的其余部分,但我正在努力放入 ascii 卡,所以这只是代码的一小部分。 I have tried putting * len(phand) at the end of the append lines.我尝试将 * len(phand) 放在追加行的末尾。 While this does give me multiple cards side by side, it doesn't seem to let me edit them.虽然这确实为我提供了多张卡片,但它似乎并没有让我编辑它们。
#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))
This outputs:这输出:
['2 of Hearts', 'King of Diamonds', 'Ace of Clubs']
┌─────────┐
│.2. . . .│
│. . . . .│
│. . . . .│
│. . ♥ . .│
│. . . . .│
│. . . . .│
│. . . .2.│
└─────────┘
┌─────────┐
│.K. . . .│
│. . . . .│
│. . . . .│
│. . ♦ . .│
│. . . . .│
│. . . . .│
│. . . .K.│
└─────────┘
┌─────────┐
│.A. . . .│
│. . . . .│
│. . . . .│
│. . ♣ . .│
│. . . . .│
│. . . . .│
│. . . .A.│
└─────────┘
How could I go about making these print out side by side?我怎么能把这些打印出来?
You have to change your code to produce one card (here the mk_card
function).您必须更改代码以生成一张卡片(这里是mk_card
函数)。
Then produce all the cards chunks and use a combination of zip
and join
to produce the lines:然后生成所有卡片块并使用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)))))
output:输出:
┌─────────┐ ┌─────────┐ ┌─────────┐
│.2. . . .│ │.K. . . .│ │.A. . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . ♥ . .│ │. . ♦ . .│ │. . ♣ . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . . .│ │. . . . .│ │. . . . .│
│. . . .2.│ │. . . .K.│ │. . . .A.│
└─────────┘ └─────────┘ └─────────┘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.