-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
283 lines (178 loc) · 6.31 KB
/
Main.py
File metadata and controls
283 lines (178 loc) · 6.31 KB
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
from random import randint
from tkinter import *
from PIL import ImageTk, Image
class Card:
x = 200
y = 0
def __init__(self, num, t=1):
self.number = num
if(self.number == 11 or self.number == 12 or self.number == 13):
self.value = 10
else:
self.value = num
if (t == 1):
self.type = "Hearts"
self.imageName = str(self.number) + "H"
elif (t == 2):
self.type = "Diamonds"
self.imageName = str(self.number) + "D"
elif (t == 3):
self.type = "Clubs"
self.imageName = str(self.number) + "C"
elif (t == 4):
self.type = "Spades"
self.imageName = str(self.number) + "S"
self.canvas = None
self.img = None
self.imageID = 0
def __str__(self):
if(self.number == 1):
return "Ace of " + self.type
elif(self.number == 11):
return "Jack of " + self.type
elif(self.number == 12):
return "Queen of " + self.type
elif(self.number == 13):
return "King of " + self.type
else:
return str(self.number) + " of " + self.type
def makeImage(self, window):
self.canvas = Canvas(window, width=250, height=250, background = "Dark Green")
self.canvas.place(x = Card.x, y = Card.y)
Card.x += 100 #Moves the next card over
self.img = ImageTk.PhotoImage(Image.open("C:\\Users\\psilv\\PycharmProjects\\BlackJackPy\\JPEG\\" + self.imageName + ".jpg").resize((250, 250))) # The resize method takes width and height in pixels
self.canvas.create_image(2, 2, anchor=NW, image=self.img)
class Deck:
def __init__(self):
self.list = []
x = 1
while(x<5):
y = 1
while(y<14):
self.list.append(Card(y,x))
y+=1
x+=1
def __getitem__(self, item):
return self.list[item]
def __len__(self):
return len(self.list)
def dealCard(self):
return self.list.pop(randint(0, len(deck)-1))
class Player:
def __init__(self, deck):
self.hand = []
self.value = 0
self.takeCard(deck)
self.takeCard(deck)
def printHand(self):
for c in self.hand:
print(c)
def takeCard(self, deck):
card = deck.dealCard()
self.hand.append(card)
self.value += card.value
return card
def isOver(self):
return self.value > 21
def giveCardsBack(self, deck):
for c in self.hand:
deck.list.append(c)
self.hand = []
self.value = 0
deck = Deck()
player = Player(deck)
dealer = Player(deck)
def main():
#GUI Stuff
#master window config
master = Tk()
master.title("Black Jack")
master.attributes("-fullscreen", True)
master.configure(background="Dark Green")
#master.geometry("600x400")
playerscards = StringVar(master)
#player.printHand()
Card.x = 200
Card.y = 0
for c in player.hand:
playerscards.set(playerscards.get() + str(c) + "\n")
c.makeImage(master)
#functions
def hitFunction():
c = player.takeCard(deck)
scoreLabel.configure(text="Score: " + str(player.value))
if(player.isOver()):
popUpBox("Bust! You lost!")
playerscards.set(playerscards.get() + str(c) + "\n")
cardsLabel.configure(text=playerscards.get())
c.makeImage(master)
#print("hitFunction")
def holdFunction():
#print("Player's score: " + str(player.value))
#print("Dealer's score: " + str(dealer.value))
if(player.value > dealer.value):
#outcomeLabel.configure(text="You won!")
popUpBox("You won!")
elif(player.value < dealer.value):
#outcomeLabel.configure(text="You lost!")
popUpBox("You lost!")
else:
#outcomeLabel.configure(text="It's a draw!")
popUpBox("It's a draw!")
#outcomeLabel.place(x=25, y=65)
#print("holdFunction")
def yesButtonFunction(popUp, player = player, dealer = dealer):
player.giveCardsBack(deck)
dealer.giveCardsBack(deck)
player.takeCard(deck)
player.takeCard(deck)
dealer.takeCard(deck)
dealer.takeCard(deck)
playerscards.set("")
for c in player.hand:
playerscards.set(playerscards.get() + str(c) + "\n")
c.makeImage(master)
scoreLabel.configure(text="Score: " + str(player.value))
popUp.destroy()
master.destroy()
main()
def popUpBox(message):
popUp = Toplevel()
popUp.geometry("220x120")
popUp.title('')
outcome = Label(popUp, text=message, font=30)
outcome.place(x=5, y=5)
playAgainLabel = Label(popUp, text="Play Again?", font = 30)
playAgainLabel.place(x=5, y=40)
yesButton = Button(popUp, text="Yes", command= lambda: yesButtonFunction(popUp))#lambda allows the function to have arguemnts
yesButton.place(x=60, y = 90)
noButton = Button(popUp, text="No", command = master.destroy)
noButton.place(x=100, y=90)
#Buttons
hitButton = Button(master, text="Hit", command=hitFunction)
hitButton.place(x = 25, y = 300)
holdButton = Button(master, text="Hold", command=holdFunction)
holdButton.place(x = 65, y = 300)
#Labels
scoreLabel = Label(master, text="Score: " + str(player.value), background = "Dark Green")
scoreLabel.place(x = 25, y = 25)
cardsLabel = Label(master, textvariable=playerscards, background = "Dark Green")
cardsLabel.place(x = 25, y = 125)
cardsTitleLabel = Label(master, text="Cards:", font="bold", background = "Dark Green")
cardsTitleLabel.place(x = 25, y = 100)
#Dealer Logic
#1 is easiest, 3 is the hardest
dealerDifficulty = randint(1,3)
def giveDealerCards(max, min):
while(dealer.value < min):
dealer.takeCard(deck)
if(dealer.value > max):
dealer.giveCardsBack(deck)
if(dealerDifficulty == 1):
giveDealerCards(14, 10)
if(dealerDifficulty == 2):
giveDealerCards(18, 14)
if(dealerDifficulty == 3):
giveDealerCards(21, 17)
master.mainloop()
main()