import random

deck = []
for suit in ('club', 'diamond', 'heart', 'spade'):
    for rank in range(1, 14):
        deck.append({'suit': suit, 'rank': rank})

def take_card(hand):
    hand.append(deck.pop(random.randrange(len(deck))))

player_hand = []
take_card(player_hand)
take_card(player_hand)

dealer_hand = []
take_card(dealer_hand)
take_card(dealer_hand)

round_over = False

def on_key_down(key):
    global round_over

    if key == keys.H and not round_over:
        take_card(player_hand)
    elif key == keys.S:
        round_over = True

def draw():
    screen.fill((0, 0, 0))

    def get_total(hand):
        total = 0
        has_ace = False

        for card in hand:
            if card['rank'] > 10:
                total += 10
            else:
                total += card['rank']

            if card['rank'] == 1:
                has_ace = True

        if has_ace and total <= 11:
            total += 10

        return total

    output = []

    output.append('Player hand:')
    for card in player_hand:
        output.append('suit: ' + card['suit'] + ', rank: ' + str(card['rank']))
    output.append('Total: ' + str(get_total(player_hand)))

    output.append('')

    output.append('Dealer hand:')
    for card in dealer_hand:
        output.append('suit: ' + card['suit'] + ', rank: ' + str(card['rank']))
    output.append('Total: ' + str(get_total(dealer_hand)))

    if round_over:
        output.append('')

        if (
            get_total(player_hand) <= 21
            and (
                get_total(dealer_hand) > 21
                or get_total(player_hand) > get_total(dealer_hand)
            )
        ):
            output.append('Player wins')
        elif (
            get_total(dealer_hand) <= 21
            and (
                get_total(player_hand) > 21
                or get_total(dealer_hand) > get_total(player_hand)
            )
        ):
            output.append('Dealer wins')
        else:
            output.append('Draw')

    screen.draw.text('\n'.join(output), (15, 15))