|
| 1 | +import random |
| 2 | +import tkinter |
| 3 | + |
| 4 | + |
| 5 | +def load_images(card_images): |
| 6 | + suits = ['heart', 'club', 'diamond', 'spade'] |
| 7 | + face_cards = ['jack', 'queen', 'king'] |
| 8 | + |
| 9 | + if tkinter.TkVersion >= 8.6: |
| 10 | + extension = 'png' |
| 11 | + else: |
| 12 | + extension = 'ppm' |
| 13 | + |
| 14 | + # for each suit, retrieve the image for the cards |
| 15 | + for suit in suits: |
| 16 | + # first the number cards 1 to 10 |
| 17 | + for card in range(1, 11): |
| 18 | + name = f'cards/{str(card)}_{suit}.{extension}' |
| 19 | + image = tkinter.PhotoImage(file=name) |
| 20 | + card_images.append((card, image,)) |
| 21 | + |
| 22 | + # next the face cards |
| 23 | + for card in face_cards: |
| 24 | + name = f'cards/{str(card)}_{suit}.{extension}' |
| 25 | + image = tkinter.PhotoImage(file=name) |
| 26 | + card_images.append((10, image,)) |
| 27 | + |
| 28 | + |
| 29 | +def deal_card(frame): |
| 30 | + # pop the next card off top of the deck |
| 31 | + next_card = deck.pop(0) |
| 32 | + # add the image to a Label and display the label |
| 33 | + tkinter.Label(frame, image=next_card[1], relief='raised').pack(side='left') |
| 34 | + # now return the card's face value |
| 35 | + return next_card |
| 36 | + |
| 37 | + |
| 38 | +def score_hand(hand): |
| 39 | + # Calculate the total score of all cards in the list. |
| 40 | + # Only one ace can have the value 11, and this will be reduced to 1 if the hand would bust. |
| 41 | + score = 0 |
| 42 | + ace = False |
| 43 | + for next_card in hand: |
| 44 | + card_value = next_card[0] |
| 45 | + if card_value == 1 and not ace: |
| 46 | + ace = True |
| 47 | + card_value = 11 |
| 48 | + score += card_value |
| 49 | + # if we would bust, check if there is an ace and subtract 10 |
| 50 | + if score > 21 and ace: |
| 51 | + score -= 10 |
| 52 | + ace = False |
| 53 | + return score |
| 54 | + |
| 55 | + |
| 56 | +def deal_dealer(): |
| 57 | + dealer_score = score_hand(dealer_hand) |
| 58 | + while 0 < dealer_score < 17: |
| 59 | + dealer_hand.append(deal_card(dealer_card_frame)) |
| 60 | + dealer_score = score_hand(dealer_hand) |
| 61 | + dealer_score_label.set(dealer_score) |
| 62 | + |
| 63 | + player_score = score_hand(player_hand) |
| 64 | + if player_score > 21: |
| 65 | + result_text.set("Dealer wins!") |
| 66 | + elif dealer_score > 21 or dealer_score < player_score: |
| 67 | + result_text.set("Player wins!") |
| 68 | + elif dealer_score > player_score: |
| 69 | + result_text.set("Dealer wins!") |
| 70 | + else: |
| 71 | + result_text.set("Draw!") |
| 72 | + |
| 73 | + |
| 74 | +def deal_player(): |
| 75 | + player_hand.append(deal_card(player_card_frame)) |
| 76 | + player_score = score_hand(player_hand) |
| 77 | + |
| 78 | + player_score_label.set(player_score) |
| 79 | + if player_score > 21: |
| 80 | + result_text.set("Dealer wins!") |
| 81 | + |
| 82 | + # global player_score |
| 83 | + # global player_ace |
| 84 | + # card_value = deal_card(player_card_frame)[0] |
| 85 | + # |
| 86 | + # if card_value == 1 and not player_ace: |
| 87 | + # player_ace = True |
| 88 | + # card_value = 11 |
| 89 | + # player_score += card_value |
| 90 | + # |
| 91 | + # # if we would bust, check if there is an ace and subtract |
| 92 | + # if player_score > 21 and player_ace: |
| 93 | + # player_score -= 10 |
| 94 | + # player_score_label.set(player_score) |
| 95 | + # |
| 96 | + # if player_score > 21: |
| 97 | + # result_text.set("Dealer wins!") |
| 98 | + |
| 99 | + |
| 100 | +mainWindow = tkinter.Tk() |
| 101 | + |
| 102 | +# Set up the screen and frames for the dealer and player |
| 103 | +mainWindow.title("Black Jack") |
| 104 | +mainWindow.geometry("640x480") |
| 105 | +mainWindow.configure(background="green") |
| 106 | + |
| 107 | +result_text = tkinter.StringVar() |
| 108 | +result = tkinter.Label(mainWindow, textvariable=result_text) |
| 109 | +result.grid(row=0, column=0, columnspan=3) |
| 110 | + |
| 111 | +card_frame = tkinter.Frame(mainWindow, relief="sunken", borderwidth=1, background="green") |
| 112 | +card_frame.grid(row=1, column=0, sticky='ew', columnspan=3, rowspan=2) |
| 113 | + |
| 114 | +dealer_score_label = tkinter.IntVar() |
| 115 | +tkinter.Label(card_frame, text="Dealer", background="green", fg='white').grid(row=0, column=0) |
| 116 | +tkinter.Label(card_frame, textvariable=dealer_score_label, background="green", fg="white").grid(row=1, column=0) |
| 117 | +# embedded frame to hold the card images |
| 118 | +dealer_card_frame = tkinter.Frame(card_frame, background="green") |
| 119 | +dealer_card_frame.grid(row=0, column=1, sticky="ew", rowspan=2) |
| 120 | + |
| 121 | +player_score_label = tkinter.IntVar() |
| 122 | +# player_score = 0 |
| 123 | +# player_ace = False |
| 124 | +tkinter.Label(card_frame, text="Player", background="green", fg="white").grid(row=2, column=0) |
| 125 | +tkinter.Label(card_frame, textvariable=player_score_label, background="green", fg="white").grid(row=3, column=0) |
| 126 | + |
| 127 | +# embedded frame to hold the card images |
| 128 | +player_card_frame = tkinter.Frame(card_frame, background="green") |
| 129 | +player_card_frame.grid(row=2, column=1, sticky='ew', rowspan=2) |
| 130 | + |
| 131 | +button_frame = tkinter.Frame(mainWindow) |
| 132 | +button_frame.grid(row=3, column=0, columnspan=3, sticky='w') |
| 133 | + |
| 134 | +dealer_button = tkinter.Button(button_frame, text="Dealer", command=deal_dealer) |
| 135 | +dealer_button.grid(row=0, column=0) |
| 136 | + |
| 137 | +player_button = tkinter.Button(button_frame, text="Player", command=deal_player) |
| 138 | +player_button.grid(row=0, column=1) |
| 139 | + |
| 140 | +# load cards |
| 141 | +cards = [] |
| 142 | +load_images(cards) |
| 143 | +print(cards) |
| 144 | +# Create a new deck of cards and shuffle them |
| 145 | +deck = list(cards) |
| 146 | + |
| 147 | +random.shuffle(deck) |
| 148 | + |
| 149 | +# Create the list to store the dealer's and player's hands |
| 150 | +dealer_hand = [] |
| 151 | +player_hand = [] |
| 152 | + |
| 153 | +deal_player() |
| 154 | +dealer_hand.append(deal_card(dealer_card_frame)) |
| 155 | +deal_player() |
| 156 | + |
| 157 | +mainWindow.mainloop() |
0 commit comments