在 Python 中并排显示显卡
Display graphic cards next to each other in Python
我正在制作纸牌游戏,我想用字符串以图形方式显示纸牌。但是,使用我当前的方法,将它们放在彼此的下方。如何将它们并排放置而不破坏视觉效果?
def display_card(card):
suit = card[0]
value = card[1]
graphic_card = (
'┌─────────┐\n'
'│{} │\n'
'│ │\n'
'│ │\n'
'│ {} │\n'
'│ │\n'
'│ │\n'
'│ {}│\n'
'└─────────┘'
).format(
format(value, ' <2'),
format(suit, ' <2'),
format(value, ' >2')
)
print(graphic_card)
cards= ["♥2", "♥3", "♥4"]
for card in cards:
display_card(card)
您可以试试这个代码:
from collections import defaultdict
def solution(cards):
# Create a dictionary with
# key: the line number
# value: a list contain value of each line from each card
dic = defaultdict(list)
for card in cards:
# Loop through each card
# Get value from each card
suit = card[0]
value = card[1]
for i in range(0, 9):
# Add value to each line
# Example value for key 1 after 3 loops:
# dic[1] = ['│2 │', '│3 │', '│4 │']
if i == 0:
dic[i].append('┌─────────┐')
elif i == 1:
dic[i].append('│{} │'.format(format(value, ' <2')))
elif i == 4:
dic[i].append('│ {} │'.format(format(suit, ' <2')))
elif i == 7:
dic[i].append('│ {}│'.format(format(value, ' <2')))
elif i == 8:
dic[i].append('└─────────┘')
else:
dic[i].append('│ │')
for i in range(0, 9):
for a in range(0, len(cards)):
# end=" " to not add a newline to the end of the string
print(dic[i][a], end=" ")
print("")
a = ["♥2", "♥3", "♥4"]
solution(a)
输出:
┌─────────┐ ┌─────────┐ ┌─────────┐
│2 │ │3 │ │4 │
│ │ │ │ │ │
│ │ │ │ │ │
│ ♥ │ │ ♥ │ │ ♥ │
│ │ │ │ │ │
│ │ │ │ │ │
│ 2 │ │ 3 │ │ 4 │
└─────────┘ └─────────┘ └─────────┘
你可以试试here.
这不是实现它的好方法,但它会让您对您的问题有所了解。使用我的代码,您的输入必须是一个列表。
例如 1 张卡片:a = ["♥2"]
如果您将方法更改为 return 卡片行的列表,可能会更简单,然后您可以 zip
将它们一起打印。
def get_card(card):
suit = card[0]
value = card[1:] # 1: for '10'
return (
'┌─────────┐\n'
'│{} │\n'
'│ │\n'
'│ │\n'
'│ {} │\n'
'│ │\n'
'│ │\n'
'│ {}│\n'
'└─────────┘'
).format(
format(value, ' <2'),
format(suit, ' <2'),
format(value, ' >2')
).splitlines()
def display_cards(cards):
for lines in zip(*map(get_card, cards)):
print(*lines)
cards= ["♥2", "♥3", "♥4"]
display_cards(cards)
我正在制作纸牌游戏,我想用字符串以图形方式显示纸牌。但是,使用我当前的方法,将它们放在彼此的下方。如何将它们并排放置而不破坏视觉效果?
def display_card(card):
suit = card[0]
value = card[1]
graphic_card = (
'┌─────────┐\n'
'│{} │\n'
'│ │\n'
'│ │\n'
'│ {} │\n'
'│ │\n'
'│ │\n'
'│ {}│\n'
'└─────────┘'
).format(
format(value, ' <2'),
format(suit, ' <2'),
format(value, ' >2')
)
print(graphic_card)
cards= ["♥2", "♥3", "♥4"]
for card in cards:
display_card(card)
您可以试试这个代码:
from collections import defaultdict
def solution(cards):
# Create a dictionary with
# key: the line number
# value: a list contain value of each line from each card
dic = defaultdict(list)
for card in cards:
# Loop through each card
# Get value from each card
suit = card[0]
value = card[1]
for i in range(0, 9):
# Add value to each line
# Example value for key 1 after 3 loops:
# dic[1] = ['│2 │', '│3 │', '│4 │']
if i == 0:
dic[i].append('┌─────────┐')
elif i == 1:
dic[i].append('│{} │'.format(format(value, ' <2')))
elif i == 4:
dic[i].append('│ {} │'.format(format(suit, ' <2')))
elif i == 7:
dic[i].append('│ {}│'.format(format(value, ' <2')))
elif i == 8:
dic[i].append('└─────────┘')
else:
dic[i].append('│ │')
for i in range(0, 9):
for a in range(0, len(cards)):
# end=" " to not add a newline to the end of the string
print(dic[i][a], end=" ")
print("")
a = ["♥2", "♥3", "♥4"]
solution(a)
输出:
┌─────────┐ ┌─────────┐ ┌─────────┐
│2 │ │3 │ │4 │
│ │ │ │ │ │
│ │ │ │ │ │
│ ♥ │ │ ♥ │ │ ♥ │
│ │ │ │ │ │
│ │ │ │ │ │
│ 2 │ │ 3 │ │ 4 │
└─────────┘ └─────────┘ └─────────┘
你可以试试here.
这不是实现它的好方法,但它会让您对您的问题有所了解。使用我的代码,您的输入必须是一个列表。
例如 1 张卡片:a = ["♥2"]
如果您将方法更改为 return 卡片行的列表,可能会更简单,然后您可以 zip
将它们一起打印。
def get_card(card):
suit = card[0]
value = card[1:] # 1: for '10'
return (
'┌─────────┐\n'
'│{} │\n'
'│ │\n'
'│ │\n'
'│ {} │\n'
'│ │\n'
'│ │\n'
'│ {}│\n'
'└─────────┘'
).format(
format(value, ' <2'),
format(suit, ' <2'),
format(value, ' >2')
).splitlines()
def display_cards(cards):
for lines in zip(*map(get_card, cards)):
print(*lines)
cards= ["♥2", "♥3", "♥4"]
display_cards(cards)