简体   繁体   English

如何并排放置两个或多个 ASCII 图像?

[英]How do I place two or more ASCII images side by side?

I wish to build a series of card games.我想建立一系列的纸牌游戏。 The idea is to place ASCII images of cards into dictionary values to be called whenever they are needed.这个想法是将卡片的 ASCII 图像放入字典值中,以便在需要时调用。 However, I cannot figure out how to get the cards to print out side by side, no matter which method that I use.但是,无论我使用哪种方法,我都无法弄清楚如何让卡片并排打印。

I have tried solving this using the for -loop, join and by modifying the print function.我尝试使用for循环、 join和修改print函数来解决这个问题。 Each time the output produces:每次输出产生:

.-------.
|A      |
|       |
|   ♣   |
|       |
|      A|
`-------´

.-------.
|2      |
|       |
|   ♦   |
|       |
|      2|
`-------´

instead of:代替:

.-------.  .-------.
|A      |  |2      |
|       |  |       |
|   ♣   |  |   ♦   |
|       |  |       |
|      A|  |      2|
`-------´  `-------´

I think that the problem has something to do with the cursor staying at the bottom of the image instead of returning to the top of the image.我认为问题与光标停留在图像底部而不是返回图像顶部有关。 I have spent a few hours searching to find information on this, but have produced no results.我花了几个小时寻找这方面的信息,但没有产生任何结果。

Here are two sample variables in a dictionary:以下是字典中的两个示例变量:

ace = r'''
.-------.
|A      |
|       |
|   ♣   |
|       |
|      A|
`-------´
'''

two = r'''
.-------.
|2      |
|       |
|   ♦   |
|       |
|      2|
`-------´
'''
cards = {"Ace":ace, "Two":two}

# modify the print
print(f'{cards["Ace"]} {cards["Two"]}', end=' ')

# using join
space = " "
deck = space.join(cards.values())
print(deck)

# placing the dictionary entries into a list
cards2 = [cards["Ace"], cards["Two"]]
for item in cards2:
    print (item)

You can zip() the lines of the two cards together and print one of each card on each line:您可以将两张卡片的行zip()放在一起,并在每行上打印每张卡片中的一张:

ace = r'''\
.-------.
|A      |
|       |
|   ♣   |
|       |
|      A|
`-------´
'''

two = r'''\
.-------.
|2      |
|   ♦   |
|       |
|   ♦   |
|      2|
`-------´
'''

spacer = ' ' * 5  # Space between cards.
for a, b in zip(ace.splitlines(), two.splitlines()):
    print(f'{a}{spacer}{b}')

Output:输出:

.-------.     .-------.
|A      |     |2      |
|       |     |   ♦   |
|   ♣   |     |       |
|       |     |   ♦   |
|      A|     |      2|
`-------´     `-------´

Update:更新:

Here's a generalized version for two or more cards:这是两张或更多卡的通用版本:

ace = r'''\
.-------.
|A      |
|       |
|   ♣   |
|       |
|      A|
`-------´
'''

two = r'''\
.-------.
|2      |
|   ♦   |
|       |
|   ♦   |
|      2|
`-------´
'''

three = r'''\
.-------.
|3      |
|   ♥   |
|   ♥   |
|   ♥   |
|      3|
`-------´
r'''

spacing = ' ' * 5  # Between cards.
cards = ace, two, three

for pieces in zip(*(card.splitlines() for card in cards)):
    print(spacing.join(pieces))

Output:输出:

.-------.     .-------.     .-------.
|A      |     |2      |     |3      |
|       |     |   ♦   |     |   ♥   |
|   ♣   |     |       |     |   ♥   |
|       |     |   ♦   |     |   ♥   |
|      A|     |      2|     |      3|
`-------´     `-------´     `-------´

If you want to align them, you're going to have to print by text line rather than by card (so, retrieve the first line of text representing each card, print that first line, and so on).如果要对齐它们,则必须按文本行而不是按卡片打印(因此,检索代表每张卡片的第一行文本,打印第一行,依此类推)。

The code below is an example of that, and assumes all cards are represented by the same number of lines of text:下面的代码就是一个例子,假设所有卡片都由相同数量的文本行表示:

def print_cards(card_dict, sep="\t"):
    card_lines = [card.split("\n") for card in card_dict.values()]
    for line_num in range(len(lines[0])):
        for card_line in lines: 
            print(card_line[line_num], end="")
            print(sep, end="")
        print()

The results are as expected:结果如预期:

>>> print_cards(cards)  
.-------.   .-------.   
|A      |   |2      |   
|       |   |       |   
|   ♣   |   |   ♦   |   
|       |   |       |   
|      A|   |      2|   
`-------´   `-------´   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM