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. 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. 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:
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|
`-------´ `-------´
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|
`-------´ `-------´ `-------´