-
Notifications
You must be signed in to change notification settings - Fork 345
/
blackjack_4.py
405 lines (329 loc) · 11.2 KB
/
blackjack_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
from tkinter import *
import random
from PIL import Image, ImageTk
from tkinter import messagebox
root = Tk()
root.title('Codemy.com - Card Deck')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("1200x800")
root.configure(background="green")
# Test for blackjack on shuffle
def blackjack_shuffle(player):
global player_total, dealer_total
# Keep track of score totals
player_total = 0
dealer_total = 0
if player == "dealer":
if len(dealer_score) == 2:
if dealer_score[0] + dealer_score[1] == 21:
# Update status
blackjack_status["dealer"] = "yes"
if player == "player":
if len(player_score) == 2:
if player_score[0] + player_score[1] == 21:
# Update status
blackjack_status["player"] = "yes"
else:
# Loop thru player score list and add up cards
for score in player_score:
# Add up score
player_total += score
if player_total == 21:
blackjack_status["player"] = "yes"
elif player_total > 21:
blackjack_status["player"] = "bust"
if len(dealer_score) == 2 and len(player_score) == 2:
# Check For Push/Tie
if blackjack_status["dealer"] == "yes" and blackjack_status["player"] == "yes":
# It's a push - tie
messagebox.showinfo("Push!", "It's a Tie!")
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check for Dealer Win
elif blackjack_status["dealer"] == "yes":
messagebox.showinfo("Dealer Wins!", "Blackjack! Dealer Wins!")
# Disable buttons
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check For Player Win
elif blackjack_status["player"] == "yes":
messagebox.showinfo("Player Wins!", "Blackjack! Player Wins!")
# Disable buttons
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check for 21 during the game
else:
# Check For Push/Tie
if blackjack_status["dealer"] == "yes" and blackjack_status["player"] == "yes":
# It's a push - tie
messagebox.showinfo("Push!", "It's a Tie!")
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check for Dealer Win
elif blackjack_status["dealer"] == "yes":
messagebox.showinfo("Dealer Wins!", "21! Dealer Wins!")
# Disable buttons
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check For Player Win
elif blackjack_status["player"] == "yes":
messagebox.showinfo("Player Wins!", "21! Player Wins!")
# Disable buttons
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Check for player bust
if blackjack_status["player"] == "bust":
messagebox.showinfo("Player Busts!", f"Player Loses! {player_total}")
# Disable buttons
card_button.config(state="disabled")
stand_button.config(state="disabled")
# Resize Cards
def resize_cards(card):
# Open the image
our_card_img = Image.open(card)
# Resize The Image
our_card_resize_image = our_card_img.resize((150, 218))
# output the card
global our_card_image
our_card_image = ImageTk.PhotoImage(our_card_resize_image)
# Return that card
return our_card_image
# Shuffle The Cards
def shuffle():
# Keep track of winning
global blackjack_status, player_total, dealer_total
# Keep track of score totals
player_total = 0
dealer_total = 0
blackjack_status = {"dealer":"no", "player":"no"}
# Enable buttons
card_button.config(state="normal")
stand_button.config(state="normal")
# Clear all the old cards from previous games
dealer_label_1.config(image='')
dealer_label_2.config(image='')
dealer_label_3.config(image='')
dealer_label_4.config(image='')
dealer_label_5.config(image='')
player_label_1.config(image='')
player_label_2.config(image='')
player_label_3.config(image='')
player_label_4.config(image='')
player_label_5.config(image='')
# Define Our Deck
suits = ["diamonds", "clubs", "hearts", "spades"]
values = range(2, 15)
# 11 = Jack, 12=Queen, 13=King, 14 = Ace
global deck
deck =[]
for suit in suits:
for value in values:
deck.append(f'{value}_of_{suit}')
# Create our players
global dealer, player, dealer_spot, player_spot, dealer_score, player_score
dealer = []
player = []
dealer_score = []
player_score = []
dealer_spot = 0
player_spot = 0
# Shuffle Two Cards for player and dealer
dealer_hit()
dealer_hit()
player_hit()
player_hit()
# Put number of remaining cards in title bar
root.title(f'Codemy.com - {len(deck)} Cards Left')
def dealer_hit():
global dealer_spot
if dealer_spot < 5:
try:
# Get the player Card
dealer_card = random.choice(deck)
# Remove Card From Deck
deck.remove(dealer_card)
# Append Card To Dealer List
dealer.append(dealer_card)
# Append to dealer score list and convert facecards to 10 or 11
dcard = int(dealer_card.split("_", 1)[0])
if dcard == 14:
dealer_score.append(11)
elif dcard == 11 or dcard == 12 or dcard == 13:
dealer_score.append(10)
else:
dealer_score.append(dcard)
# Output Card To Screen
global dealer_image1, dealer_image2, dealer_image3, dealer_image4, dealer_image5
if dealer_spot == 0:
# Resize Card
dealer_image1 = resize_cards(f'images/cards/{dealer_card}.png')
# Output Card To Screen
dealer_label_1.config(image=dealer_image1)
# Increment our player spot counter
dealer_spot += 1
elif dealer_spot == 1:
# Resize Card
dealer_image2 = resize_cards(f'images/cards/{dealer_card}.png')
# Output Card To Screen
dealer_label_2.config(image=dealer_image2)
# Increment our player spot counter
dealer_spot += 1
elif dealer_spot == 2:
# Resize Card
dealer_image3 = resize_cards(f'images/cards/{dealer_card}.png')
# Output Card To Screen
dealer_label_3.config(image=dealer_image3)
# Increment our player spot counter
dealer_spot += 1
elif dealer_spot == 3:
# Resize Card
dealer_image4 = resize_cards(f'images/cards/{dealer_card}.png')
# Output Card To Screen
dealer_label_4.config(image=dealer_image4)
# Increment our player spot counter
dealer_spot += 1
elif dealer_spot == 4:
# Resize Card
dealer_image5 = resize_cards(f'images/cards/{dealer_card}.png')
# Output Card To Screen
dealer_label_5.config(image=dealer_image5)
# Increment our player spot counter
dealer_spot += 1
# Put number of remaining cards in title bar
root.title(f'Codemy.com - {len(deck)} Cards Left')
except:
root.title(f'Codemy.com - No Cards In Deck')
# Check for blackjack
blackjack_shuffle("dealer")
def player_hit():
global player_spot
if player_spot < 5:
try:
# Get the player Card
player_card = random.choice(deck)
# Remove Card From Deck
deck.remove(player_card)
# Append Card To Dealer List
player.append(player_card)
# Append to dealer score list and convert facecards to 10 or 11
pcard = int(player_card.split("_", 1)[0])
if pcard == 14:
player_score.append(11)
elif pcard == 11 or pcard == 12 or pcard == 13:
player_score.append(10)
else:
player_score.append(pcard)
# Output Card To Screen
global player_image1, player_image2, player_image3, player_image4, player_image5
if player_spot == 0:
# Resize Card
player_image1 = resize_cards(f'images/cards/{player_card}.png')
# Output Card To Screen
player_label_1.config(image=player_image1)
# Increment our player spot counter
player_spot += 1
elif player_spot == 1:
# Resize Card
player_image2 = resize_cards(f'images/cards/{player_card}.png')
# Output Card To Screen
player_label_2.config(image=player_image2)
# Increment our player spot counter
player_spot += 1
elif player_spot == 2:
# Resize Card
player_image3 = resize_cards(f'images/cards/{player_card}.png')
# Output Card To Screen
player_label_3.config(image=player_image3)
# Increment our player spot counter
player_spot += 1
elif player_spot == 3:
# Resize Card
player_image4 = resize_cards(f'images/cards/{player_card}.png')
# Output Card To Screen
player_label_4.config(image=player_image4)
# Increment our player spot counter
player_spot += 1
elif player_spot == 4:
# Resize Card
player_image5 = resize_cards(f'images/cards/{player_card}.png')
# Output Card To Screen
player_label_5.config(image=player_image5)
# Increment our player spot counter
player_spot += 1
# Put number of remaining cards in title bar
root.title(f'Codemy.com - {len(deck)} Cards Left')
except:
root.title(f'Codemy.com - No Cards In Deck')
# Check for blackjack
blackjack_shuffle("player")
# Deal Out Cards
def deal_cards():
try:
# Get the deler Card
card = random.choice(deck)
# Remove Card From Deck
deck.remove(card)
# Append Card To Dealer List
dealer.append(card)
# Output Card To Screen
global dealer_image
dealer_image = resize_cards(f'images/cards/{card}.png')
dealer_label.config(image=dealer_image)
#dealer_label.config(text=card)
# Get the player Card
card = random.choice(deck)
# Remove Card From Deck
deck.remove(card)
# Append Card To Dealer List
player.append(card)
# Output Card To Screen
global player_image
player_image = resize_cards(f'images/cards/{card}.png')
player_label.config(image=player_image)
#player_label.config(text=card)
# Put number of remaining cards in title bar
root.title(f'Codemy.com - {len(deck)} Cards Left')
except:
root.title(f'Codemy.com - No Cards In Deck')
my_frame = Frame(root, bg="green")
my_frame.pack(pady=20)
# Create Frames For Cards
dealer_frame = LabelFrame(my_frame, text="Dealer", bd=0)
dealer_frame.pack(padx=20, ipadx=20)
player_frame = LabelFrame(my_frame, text="Player", bd=0)
player_frame.pack(ipadx=20, pady=10)
# Put Dealer cards in frames
dealer_label_1 = Label(dealer_frame, text='')
dealer_label_1.grid(row=0, column=0, pady=20, padx=20)
dealer_label_2 = Label(dealer_frame, text='')
dealer_label_2.grid(row=0, column=1, pady=20, padx=20)
dealer_label_3 = Label(dealer_frame, text='')
dealer_label_3.grid(row=0, column=2, pady=20, padx=20)
dealer_label_4 = Label(dealer_frame, text='')
dealer_label_4.grid(row=0, column=3, pady=20, padx=20)
dealer_label_5 = Label(dealer_frame, text='')
dealer_label_5.grid(row=0, column=4, pady=20, padx=20)
# Put Player cards in frames
player_label_1 = Label(player_frame, text='')
player_label_1.grid(row=1, column=0, pady=20, padx=20)
player_label_2 = Label(player_frame, text='')
player_label_2.grid(row=1, column=1, pady=20, padx=20)
player_label_3 = Label(player_frame, text='')
player_label_3.grid(row=1, column=2, pady=20, padx=20)
player_label_4 = Label(player_frame, text='')
player_label_4.grid(row=1, column=3, pady=20, padx=20)
player_label_5 = Label(player_frame, text='')
player_label_5.grid(row=1, column=4, pady=20, padx=20)
# Create Button Frame
button_frame = Frame(root, bg="green")
button_frame.pack(pady=20)
# Create a couple buttons
shuffle_button = Button(button_frame, text="Shuffle Deck", font=("Helvetica", 14), command=shuffle)
shuffle_button.grid(row=0, column=0)
card_button = Button(button_frame, text="Hit Me!", font=("Helvetica", 14), command=player_hit)
card_button.grid(row=0, column=1, padx=10)
stand_button = Button(button_frame, text="Stand!", font=("Helvetica", 14))
stand_button.grid(row=0, column=2)
# Shuffle Deck On Start
shuffle()
root.mainloop()