-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
378 lines (264 loc) · 9.83 KB
/
main.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
from card import *
import time
import turtle
import tkinter as tk
import winsound
class BlackJackScreen:
def __init__(self):
self.player = BlackjackPlayer()
self.dealer = Dealer()
self.root = tk.Tk()
self.root.title('Python Tkinter+Turtle BlackJack')
self.root.iconbitmap(r'card.ico')
self.canvas = tk.Canvas(self.root)
self.canvas.config(width=800, height=600)
self.canvas.pack()
self.hitCount = -300
self.dealerCount = -400
self.updateCount = 90
button_hit = tk.Button(self.root, text="Hit", width=5,height=0,command=self.hitButton)
button_hit.pack()
button_stand = tk.Button(self.root, text="Stand",width=5,height=0,command=self.standButton)
button_stand.pack()
def displayStart(self,yourHand,dealerHand):
yourlst = self.displayHand(yourHand)
dealst = self.displayHand(dealerHand)
try:
winsound.PlaySound('swipe', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
self.t.speed(5)
self.screen.addshape('cardbacktest.gif')
self.t.shape('cardbacktest.gif')
self.t.goto(-300,-100)
self.t.stamp()
self.t.forward(100)
previous = 0
for i in yourlst:
try:
self.t.shape(i)
self.t.forward(100)
self.t.stamp()
except:
self.screen.addshape(i)
self.t.shape(i)
self.t.forward(100)
self.t.stamp()
self.t.shape('cardbacktest.gif')
self.t.goto(-300,200)
self.t.stamp()
self.t.speed(4)
self.t.forward(100)
for i in dealst:
try:
self.t.shape(i)
self.t.forward(100)
self.t.stamp()
except:
self.screen.addshape(i)
self.t.shape(i)
self.t.forward(100)
self.t.stamp()
self.t.shape('arrow')
self.t.hideturtle()
self.t.speed(0)
self.t.goto(-105,-300)
self.t.write("Player Score:"+str(self.player.evalHand()), font=("Courier New", 16, "bold"))
self.t.color('red')
self.t.goto(-105, -280)
self.t.write("Dealer Score:" + str(self.dealer.evalHand()), font=("Courier New", 16, "bold"))
# print(self.player.seeHand())
# print(self.dealer.seeHand())
def displayHand(self,hand):
displaylst = []
for card in hand:
displaylst.append(card[0]+str(card[1])+'.gif')
return displaylst
def bustCheck(self,buster):
if buster > 24:
return True
else:
return False
def drawHit(self,card):
card = card[0]+str(card[1])+'.gif'
try:
self.t.shape(card)
try:
winsound.PlaySound('swipe', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
self.t.forward(400)
self.t.stamp()
except:
self.screen.addshape(card)
self.t.shape(card)
try:
winsound.PlaySound('swipe', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
self.t.forward(400)
self.t.stamp()
def dealerHit(self,card):
card = card[0]+str(card[1])+'.gif'
try:
self.t.shape(card)
self.t.fd(100)
self.t.stamp()
except:
self.screen.addshape(card)
self.t.shape(card)
self.t.fd(100)
self.t.stamp()
def RestartScreen(self):
try:
winsound.PlaySound('level', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
self.hitCount = -300
self.updateCount = 90
self.dealerCount = -400
self.player = BlackjackPlayer()
self.dealer = Dealer()
self.screen.clear()
self.run()
def updateScore(self,robusta):
if robusta == True:
self.t.hideturtle()
self.t.color('red')
self.t.goto(self.updateCount, -300)
self.t.write(str(self.player.evalHand())+" Busted!!" , font=("Courier New", 16, "bold"))
self.t.goto(-160,0)
self.t.write("YOU LOSE!!" , font=("Courier New", 40, "bold"))
try:
winsound.PlaySound('lose', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
else:
self.t.color('black')
self.t.hideturtle()
self.t.penup()
self.t.goto(self.updateCount, -300)
self.t.forward(3)
self.t.write("," + str(self.player.evalHand()), font=("Courier New", 16, "bold"))
self.updateCount += 50
def updateDealer(self,robusta):
if robusta == True:
self.t.hideturtle()
self.t.color('red')
self.t.goto(87, -280)
self.t.write(','+str(self.dealer.evalHand()) +"Busted", font=("Courier New", 16, "bold"))
self.t.goto(-160,0)
self.t.write("YOU WIN!!" , font=("Courier New", 40, "bold"))
try:
winsound.PlaySound('win', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
else:
self.t.hideturtle()
self.t.color('red')
self.t.goto(87, -280)
self.t.write(','+str(self.dealer.evalHand()), font=("Courier New", 16, "bold"))
def win_lose(self,det):
if det == 1:
self.t.goto(-160, 0)
self.t.write("YOU WIN!!", font=("Courier New", 40, "bold"))
try:
winsound.PlaySound('win', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
elif det == 2:
self.t.goto(-140, 0)
self.t.write("DRAW!!", font=("Courier New", 40, "bold"))
try:
winsound.PlaySound('win', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
else:
self.t.goto(-160, 0)
self.t.write("YOU LOSE!!", font=("Courier New", 40, "bold"))
try:
winsound.PlaySound('lose', winsound.SND_FILENAME)
except:
print('Warning: Missing Audio File')
def determine(self):
a = self.player.evalHand()
b = self.dealer.evalHand()
if a > b:
self.win_lose(1)
elif b > a:
self.win_lose(0)
elif a == b:
self.win_lose(2)
def initializeScreen(self):
self.screen = turtle.TurtleScreen(self.canvas)
self.t = turtle.RawTurtle(self.screen)
self.screen.bgcolor('#30c744')
self.t.penup()
def hitButton(self):
self.player.Hit()
ourHand = self.player.seeHand()
self.t.hideturtle()
self.t.speed(0)
self.t.showturtle()
self.t.goto(self.hitCount, -100)
self.hitCount+=100
self.t.speed(7)
self.drawHit(ourHand[-1])
self.updateScore(bustCheck(self.player.evalHand()))
if bustCheck(self.player.evalHand()) == True:
time.sleep(1)
self.RestartScreen()
def standButton(self):
dealerChoice = self.dealer.dealerPlay()
print(dealerChoice)
self.t.showturtle()
self.t.goto(self.dealerCount,200)
self.dealerCount+=10
self.t.speed(7)
for i in range(dealerChoice):
self.dealer.Hit()
dealerHand = self.dealer.seeHand()
winsound.PlaySound('swipe', winsound.SND_FILENAME)
for num in range(dealerChoice,0,-1):
self.dealerHit(dealerHand[num])
self.updateDealer(bustCheck(self.dealer.evalHand()))
print(self.dealer.seeHand())
print(self.dealer.evalHand())
if bustCheck(self.dealer.evalHand()) == True:
time.sleep(1)
self.RestartScreen()
else:
self.determine()
time.sleep(1)
self.RestartScreen()
def finalize(self):
self.root.mainloop()
def run(self):
self.initializeScreen()
self.displayStart(self.player.seeHand(),self.dealer.seeHand())
self.finalize()
def mainmenu(self):
self.screen = turtle.TurtleScreen(self.canvas)
self.t = turtle.RawTurtle(self.screen)
self.screen.bgcolor('#30c744')
self.t.penup()
self.t.goto(-270,0)
self.t.hideturtle()
self.t.write("Welcome To BlackJack", font=("Courier New", 36, "bold"))
self.t.goto(-180, -50)
self.t.write("Click here to start the game", font=("Courier New", 16, "normal"))
def startgame(x,y):
self.t.goto(-270,0)
for i in range(12):
self.t.color('#30c744')
self.t.write("Welcome To BlackJack", font=("Courier New", 36, "bold"))
self.t.color('white')
self.t.write("Welcome To BlackJack", font=("Courier New", 36, "bold"))
self.t.color('black')
self.t.write("Welcome To BlackJack", font=("Courier New", 36, "bold"))
winsound.PlaySound('win', winsound.SND_FILENAME)
self.run()
self.screen.onclick(startgame)
self.finalize()
t1= BlackJackScreen()
t1.mainmenu()