-
Notifications
You must be signed in to change notification settings - Fork 1
/
poker_gui.py
495 lines (404 loc) · 13.1 KB
/
poker_gui.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import PySimpleGUI as sg
import files
from pathlib import Path
from threading import Thread
from playsound import playsound
import sys
import time
from PIL import Image
import funPIL as df
SOUNDS = Path(__file__).parent / "sounds"
color, highlight, disabled = "#FF0000", "#FFFFFF", "#4a4a4a"
colors = color, highlight, disabled
sg.LOOK_AND_FEEL_TABLE["DarkPoker"] = {
"BACKGROUND": "#252525",
"TEXT": "#FFFFFF",
"INPUT": "#af0404",
"TEXT_INPUT": "#FFFFFF",
"SCROLL": "#af0404",
"BUTTON": ("#FFFFFF", "#252525"),
"BORDER": 1,
"SLIDER_DEPTH": 0,
"PROGRESS_DEPTH": 0,
"COLOR_LIST": ["#252525", "#414141", "#af0404", "#ff0000"],
"PROGRESS": ("# D1826B", "# CC8019"),
}
sg.theme("DarkPoker")
def roundButtonImg(text, color, highlight, disabled, visible=True):
w, h = df.getSize(text, df.fontDefiner("C:\\Windows\\Fonts\\Arial.ttf", 20))
w += 30
h += 20
OUT = df.backgroundPNG(w * 5, h * 5, color)
OUT[0] = df.roundCorners(OUT[0], 4 * 5)
IN = df.replaceColor(OUT[0], color, highlight).resize(
(w, h), resample=Image.ANTIALIAS
)
DISABLED = df.replaceColor(OUT[0], color, disabled).resize(
(w, h), resample=Image.ANTIALIAS
)
OUT = OUT[0].resize((w, h), resample=Image.ANTIALIAS)
OUT, IN, DISABLED = [df.image_to_data(each) for each in [OUT, IN, DISABLED]]
button = sg.Button(
text,
border_width=0,
button_color=sg.theme_background_color(),
disabled_button_color=sg.theme_background_color(),
image_data=OUT,
visible=visible,
)
return button, IN, OUT, DISABLED
buttons = {
# "START" : [*roundButtonImg('START', *colors)],
"Bet": [*roundButtonImg("Bet", *colors)],
"Check": [*roundButtonImg("Check", *colors)],
"Fold": [*roundButtonImg("Fold", *colors)],
"New Game": [*roundButtonImg("New Game", *colors)],
"Continue": [*roundButtonImg("Continue", *colors, False)],
}
def in_out(window, event, buttonDict):
if "+IN+" in event:
element = event.replace("+IN+", "")
if window[element].Disabled == "ignore":
return True
window[element].Update(
button_color=(color, sg.theme_background_color()),
image_data=buttonDict[element][1],
)
window.refresh()
return True
elif "+OUT+" in event:
element = event.replace("+OUT+", "")
if window[element].Disabled == "ignore":
return True
window[element].Update(
button_color=(highlight, sg.theme_background_color()),
image_data=buttonDict[element][2],
)
window.refresh()
return True
return False
def disable(window, element, disabled):
if disabled:
state, index = sg.BUTTON_DISABLED_MEANS_IGNORE, 3
else:
state, index = False, 2
window[element].Update(
button_color=(highlight, sg.theme_background_color()),
disabled=state,
image_data=buttons[element][index],
)
window.refresh()
def startGame():
buttons2 = {
"START": [*roundButtonImg("START", *colors)],
}
startGameGUI = [
[
sg.Column(
[
[sg.Text("Player Name")],
[sg.Text("Computer Name")],
[sg.Text("Starting Money")],
[sg.Text("Minimal Bet")],
]
),
sg.Column(
[
[sg.Input("Nobody", key="PLAYERNAME")],
[sg.Input("Skynet", key="COMPUTERNAME")],
[sg.Input(str(10000), key="STARTINGMONEY")],
[sg.Input(str(500), key="MINBET")],
]
),
],
[sg.Push(), buttons2["START"][0], sg.Push()],
]
window = sg.Window("Poker", startGameGUI, use_default_focus=False, finalize=True)
[
window["START"].bind(*each)
for each in [("<Enter>", "+IN+"), ("<Leave>", "+OUT+")]
]
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == "Quit":
break
if in_out(window, event, buttons2):
continue
if event == "START":
playerName, computerName, startingMoney, minBet = (
values["PLAYERNAME"].strip("\n").strip(" "),
values["COMPUTERNAME"].strip("\n").strip(" "),
values["STARTINGMONEY"].strip("\n").strip(" "),
values["MINBET"].strip("\n").strip(" "),
)
if startingMoney.isdigit() and minBet.isdigit():
startingMoney, minBet = int(startingMoney), int(minBet)
if startingMoney > minBet:
window.close()
return playerName, computerName, startingMoney, minBet
window.close()
return 1
def gameWindow(minBet, player, computer):
for each in buttons:
visible = False if each == "Continue" else True
buttons[each][0] = sg.Button(
each,
border_width=0,
button_color=sg.theme_background_color(),
disabled_button_color=sg.theme_background_color(),
image_data=buttons[each][2],
visible=visible,
)
font = ("Arial", 20, ["bold"])
minifont = ("Arial", 10, ["bold"])
layout = [
# COM SIDE
[
sg.Push(),
sg.Text(
computer.name,
text_color="#FF0000",
font=font,
),
sg.Text(
"$" + str(computer.money),
text_color="#FF0000",
font=minifont,
key="COMMONEY",
),
sg.Text(
"$0",
text_color="#FF0000",
font=font,
key="COMBET"
),
sg.Push()
],
[
sg.Push(),
*[
sg.Image(data=files.placeHolder, key="C_" + str(each), p=((5, 5),(20,20)))
for each in list(range(1, 3))
],
sg.Push(),
],
[
sg.Push(),
sg.Text(
"TABLE POT: ",
text_color="#FF0000",
font=font
),
sg.Text(
"$0",
key="TABLEPOT",
text_color="#FF0000",
font=font
),
sg.Push()
],
# TABLE SIDE
[
sg.Push(),
[
sg.Image(data=files.placeHolder, key="T_"+str(each), p=((5, 5),(20,20)))
for each in [1, 2, 3, 4, 5]
],
sg.Push(),
],
# PLAYER SIDE
[
sg.Push(),
*[
sg.Image(data=files.placeHolder, key="P_" + str(each), p=((5, 5),(20,20)))
for each in list(range(1, 3))
],
sg.Push(),
],
[
sg.Push(),
sg.Text(
player.name,
text_color="#FF0000",
font=font
),
sg.Text(
"$" + str(player.money),
text_color="#FF0000",
font=minifont,
key="PLAYERMONEY",
),
sg.Text(
"$0",
text_color="#FF0000",
font=font,
key="PLAYERBET"
),
sg.Push(),
],
[
sg.Push(),
sg.Slider(
range=(None, None),
default_value=None,
resolution=minBet,
orientation="horizontal",
key="BET",
),
*[buttons[each][0] for each in ["Bet", "Check", "Fold"]],
sg.Push(),
],
[sg.Push(), sg.Text("YOU GOT: "), sg.Text("", key="POINTS"), sg.Push()],
[sg.Push(), sg.Text("PLAYING...", key="OUT"), sg.Push()],
[sg.Push(), sg.pin(buttons["Continue"][0]), sg.Push()],
[sg.Push(), sg.pin(buttons["New Game"][0])],
]
window = sg.Window("Poker", layout, use_default_focus=False, finalize=True)
for button in buttons.keys():
[
window[button].bind(*each)
for each in [("<Enter>", "+IN+"), ("<Leave>", "+OUT+")]
]
return window
def readInput(window, keepLock=False):
event = None
# while event != 'Check' and event != 'Bet' and event != 'Continue' and event != 'Fold':
if keepLock == False:
unlockButtons(window)
while event not in ["Check", "Bet", "Continue"]:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == "Quit":
sys.exit(0)
if in_out(window, event, buttons):
continue
if event == "Check":
playCheck()
value = 0
elif event == "Bet":
playBet()
value = int(values["BET"])
elif event == "Fold" or event == "Continue" or event == "New Game":
if event == "Continue":
window["Continue"].Update(visible=False)
unlockButtons(window)
value = None
if event == "New Game":
window.close()
del window
return event, None
lockButtons(window)
return event, value
def giveCards(window, player):
# Updating GUI of player
display = ["P_1", "P_2"]
for element, card in list(zip(*[display, player.cards])):
t = Thread(target=playFlip, args=())
t.start()
window[element].Update(data=card.image)
t.join()
window.refresh()
# Updating GUI of COM with facedown cards
display = ["C_1", "C_2"]
[window[element].Update(data=files.cardBack) for element in display]
# Updating GUI of table
[
window["T_" + str(each)].Update(data=files.placeHolder)
for each in [1, 2, 3, 4, 5]
]
def flipCOM(window, computer):
# Updating GUI of COM flipping his cards
t = Thread(target=playFlip, args=())
t.start()
display = ["C_1", "C_2"]
[
window[element].Update(data=card.image)
for element, card in list(zip(*[display, computer.cards]))
]
window.refresh()
t.join()
lockButtons(window)
window["Continue"].Update(visible=True)
def updateFlop(window, table):
if len(table.cards) == 3:
rangeIndex = [1, 2, 3]
elif len(table.cards) == 4:
rangeIndex = [4]
elif len(table.cards) == 5:
rangeIndex = [5]
for each in rangeIndex:
t = Thread(target=playFlip, args=())
t.start()
window["T_" + str(each)].Update(data=table.cards[each - 1].image)
window.refresh()
t.join()
def updateBet(window, player):
window["BET"].Update(range=(player.minBet, player.maxBet))
window.refresh()
def updateText(window, player, computer, table):
datas = [player.bet, player.money, computer.bet, computer.money, table.pot]
datas = ["$" + str(each) if type(each) == int else str("None") for each in datas]
for i in range(len(datas) - 1):
if datas[i] == "$0":
# Bet data index
if i % 2 == 0:
datas[i] = datas[i].replace("$0", "CHECK")
# Money data index
else:
datas[i] = datas[i].replace("$0", "ALLIN")
elements = ["PLAYERBET", "PLAYERMONEY", "COMBET", "COMMONEY", "TABLEPOT"]
[
window[element].Update(str(data))
for element, data in list(zip(*[elements, datas]))
]
window.refresh()
def updateOut(window, message):
window["OUT"].Update(message)
window.refresh()
def updatePoints(window, points):
window["POINTS"].Update(points)
window.refresh()
def lockCheck(window):
disable(window, "Check", True)
window.refresh()
def unlockCheck(window):
disable(window, "Check", False)
window.refresh()
def unlockButtons(window):
elements = ["Bet", "Fold"]
[disable(window, each, False) for each in elements]
window.refresh()
def lockButtons(window):
elements = ["Bet", "Fold"]
[disable(window, each, True) for each in elements]
window.refresh()
def clear(window):
elements = ["C_" + str(i) for i in range(1, 3)]
elements += ["P_" + str(i) for i in range(1, 3)]
elements += ["T_" + str(i) for i in range(1, 6)]
[window[each].Update(data=files.placeHolder) for each in elements]
# [window["T_"+str(each)].Update(data=files.placeHolder) for each in list(range(1,6))]
def playFlip():
# folding card sound
# Path(__file__).parent / sounds
playsound(str(SOUNDS / "flip.mp3"))
def playWinHand():
playsound(str(SOUNDS / "win.mp3"))
def playLostHand():
playsound(str(SOUNDS / "lose.mp3"))
pass
def playHand(winnerIndex):
if winnerIndex == 0:
t = Thread(target=playWinHand, args=())
elif winnerIndex == 1:
t = Thread(target=playLostHand, args=())
t.start()
t.join()
def playBet():
# playsound(str(SOUNDS /"chips.mp3"))
t = Thread(target=playsound, args=(str(SOUNDS / "chips.mp3"),))
t.start()
def playCheck():
t = Thread(target=playsound, args=(str(SOUNDS / "check.mp3"),))
t.start()
def pause():
time.sleep(1.5)