pythonpython-3.x

Blackjack Game - displaying ASCII Graphics / Multiline Strings


I'm pretty new to Python and currently trying to create a basic blackjack game using ASCII graphics to represent the cards. I've placed the card images in a list of multiline strings and the idea is to call the specific index for each one when a card needs to be displayed.

However I can't get them to print next to each other, and no amount of tinkering seems to get it to work. Here's the code:

CARDS = ['''
 -------
|K      |
|       |
|       |
|       |
|      K|
 ------- ''', '''
 -------
|Q      |
|       |
|       |
|       |
|      Q|
 ------- ''', ''' 
 -------
|J      |
|       |
|       |
|       |
|      J|
 ------- ''', '''
 -------
|10     |
|       |
|       |
|       |
|     10|
 ------- ''', '''
 -------
|9      |
|       |
|       |
|       |
|      9|
 ------- ''', '''
 -------
|8      |
|       |
|       |
|       |
|      8|
 ------- ''', '''
 -------
|7      |
|       |
|       |
|       |
|      7|
 ------- ''', '''
 -------
|6      |
|       |
|       |
|       |
|      6|
 ------- ''', '''
 -------
|5      |
|       |
|       |
|       |
|      5|
 ------- ''', '''
 -------
|6      |
|       |
|       |
|       |
|      6|
 ------- ''', '''
 -------
|5      |
|       |
|       |
|       |
|      5|
 ------- ''', '''
 -------
|4      |
|       |
|       |
|       |
|      4|
 ------- ''', '''
 -------
|3      |
|       |
|       |
|       |
|      3|
 ------- ''', '''
 -------
|2      |
|       |
|       |
|       |
|      2|
 ------- ''', ''' 
 -------
|A      |
|       |
|       |
|       |
|      A|
 ------- '''
]

BLANKCARD = '''
 -------
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
 ------- '''


def displayCards():
    print(CARDS[2] + CARDS[14], end='')

displayCards()

The above code prints the following:

 -------
|J      |
|       |
|       |
|       |
|      J|
 ------- 
 -------
|A      |
|       |
|       |
|       |
|      A|
 ------- 

I've tried using end='' to get rid of the new line, but no joy. Does anyone have any suggestions about how I can get the cards next to each other?

Thanks in advance!


Solution

  • Interesting little problem. Here's a quick solution that I whipped up.

    class Card:

    def topchar(char):
        return '|{}      |'.format(char)
    
    def botchar(char):
        return '|      {}|'.format(char)
    
    def print(char_list):
        top = ' ------- '
        side ='|       |'
        topout = ''
        topchar = ''
        botchar = ''
        blankside = ''
        for char in char_list:
                topout += top + ' '
                topchar += Card.topchar(char) + ' '
                blankside += side + ' '
                botchar += Card.botchar(char) + ' '
        print(topout)
        print(topchar)
        print(blankside)
        print(blankside)
        print(blankside)
        print(botchar)
        print(topout)