Blackjack

Tags: pgzero, game

https://simplegametutorials.github.io/pygamezero/

blackjack.py
  1import pgzrun
  2import random
  3import os
  4
  5PATH = 'D:\\Documents\\Marcel\\Projects\\pgzero\\ghoulash\\images\\blackjack\\'
  6
  7SUITS = ('spade', 'heart', 'diamond', 'club',)
  8
  9
 10def reset(): # {{{
 11    global deck
 12    global player_hand, dealer_hand
 13    global round_over
 14
 15
 16    deck = []
 17
 18
 19    for suit in SUITS:
 20        for rank in range(1, 14):
 21            deck.append({ 'suit': suit, 'rank': rank, })
 22
 23    round_over = False
 24
 25    player_hand = []
 26    take_card(player_hand)
 27    take_card(player_hand)
 28
 29
 30    dealer_hand = []
 31    take_card(dealer_hand)
 32    take_card(dealer_hand)
 33
 34# }}}
 35def take_card(hand): # {{{
 36    hand.append(deck.pop(random.randrange(len(deck))))
 37    # }}}
 38def on_key_down(key): # {{{
 39    global  round_over
 40
 41    if not round_over:
 42        if key == keys.H:
 43            take_card(player_hand)
 44            if get_total(player_hand) >= 21:
 45                round_over = True
 46        elif key == keys.S:
 47            round_over = True
 48
 49        if round_over:
 50            while get_total(dealer_hand) < 17:
 51                take_card(dealer_hand)
 52    else: reset()
 53        # }}}
 54def get_total(hand): # {{{
 55    all_cards = [min(i['rank'], 10) for i in hand]
 56    total = sum(all_cards)
 57    if 1 in all_cards and total <= 11: total += 10
 58    return total
 59# }}}
 60def hand_has_won(this_hand, other_hand): # {{{
 61    this_score = get_total(this_hand)
 62    other_score = get_total(other_hand)
 63    return (
 64            this_score <= 21 
 65            and (other_score > 21 or this_score > other_score)
 66            )
 67# }}}
 68def draw(): # {{{
 69    global  round_over
 70    card_spacing = 60
 71    margin_x = 10
 72    margin_y = 10
 73    screen.fill('#005500')
 74
 75
 76    for i, card in enumerate(dealer_hand):
 77        suit = card['suit']
 78        rank = card['rank']
 79        image = '%s_%s' % (suit, rank)
 80        if not round_over and i == 0:
 81            image = 'card_face_down'
 82        screen.blit('%s%s.png' % (PATH, image), (i*card_spacing + margin_x, margin_y + 20))
 83
 84    # output = ['Player hand:']
 85    for i, card in enumerate(player_hand):
 86        suit = card['suit']
 87        rank = card['rank']
 88        # output.append('suit:%s, rank:%s' % (suit, rank))
 89        screen.blit('%s%s_%s.png' % (PATH, suit, rank), (i*card_spacing + margin_x, margin_y + 130))
 90    # output.append('Total: %s' % get_total(player_hand))
 91
 92    # output.append('')
 93    # output.append('Dealer hand:')
 94    for i, card in enumerate(dealer_hand):
 95        suit = card['suit']
 96        rank = card['rank']
 97        # if not round_over and i == 0:
 98        #     output.append('(Card hidden)')
 99        # else:
100        #     output.append('suit:%s, rank:%s' % (suit, rank))
101    if round_over:
102        # output.append('Total: %s' % get_total(dealer_hand))
103        screen.draw.text('Total: %d' % get_total(dealer_hand), (margin_x, 10))
104    else:
105        # output.append('Total: ?')
106        screen.draw.text('Total: %s' % '?', (margin_x, 10))
107
108    screen.draw.text('Total: %d' % get_total(player_hand), (margin_x, 230))
109
110    if round_over:
111    #     output.append('')
112
113    #     if hand_has_won(player_hand, dealer_hand):
114    #         output.append('Player wins')
115    #     elif hand_has_won(dealer_hand, player_hand):
116    #         output.append('Dealer wins')
117    #     else:
118    #         output.append('Draw')
119
120
121        if hand_has_won(player_hand, dealer_hand):
122            message = 'Player wins'
123        elif hand_has_won(dealer_hand, player_hand):
124            message = 'Dealer wins'
125        else:
126            message = 'Draw'
127
128
129
130        screen.draw.text(message, (margin_x, 268))
131        # result = 0 if player_score > dealer_score else 1 if dealer_score > player_score else 2
132        # results = [
133        #         'Player wins',
134        #         'Dealer wins',
135        #         'Draw',
136        #         ]
137        # output.append(results[result])
138
139    # screen.draw.text('\n'.join(output), (300, 15))
140    # }}}
141
142
143
144reset()
145pgzrun.go()
146
147# BCP {{{
148mydir = os.path.dirname('__file__')
149# mydir = os.getcwd()
150mydir = (os.path.dirname(os.path.realpath('__file__')))
151mydir = os.path.join(mydir, 'images', 'tinted', 'clouds_1.png')
152# }}}