-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
152 lines (113 loc) · 4.95 KB
/
Copy pathGUI.py
File metadata and controls
152 lines (113 loc) · 4.95 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
import tkinter as tk
from cgitb import reset
import requests
from PIL import Image, ImageTk
from io import BytesIO
import GameLogic as gl
def create_gui():
global root, player_hand_frame, dealer_hand_frame,dealer_points_label,dealer_cards,player_points_label,player_cards,hit_button,stand_button,start_button
root = tk.Tk()
root.title("Card Game")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_position = (screen_width - 1200) // 2
y_position = (screen_height - 600) // 2
root.geometry(f"1200x600+{x_position}+{y_position}")
root.resizable(width=False, height=False)
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
frame.grid_rowconfigure(0, weight=1 ,minsize=200)
frame.grid_rowconfigure(1, weight=0, minsize=50)
frame.grid_rowconfigure(2, weight=0 , minsize=100)
frame.grid_rowconfigure(3, weight=0, minsize=50)
frame.grid_rowconfigure(4, weight=1, minsize=200)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
dealer_hand_frame = tk.Frame(frame)
dealer_hand_frame.grid(row=0, column=0, columnspan=3, sticky="nsew")
dealer_hand_frame.grid_rowconfigure(0, weight=1)
dealer_hand_frame.grid_columnconfigure(0, weight=0)
dealer_hand_frame.grid_propagate(False)
dealer_cards = []
dealer_points_label = tk.Label(frame, text="Dealer Points: 0", font=("Arial", 12))
dealer_points_label.grid(row=1, column=0, columnspan=3, pady=5, sticky="nsew")
hit_button = tk.Button(frame, text="Hit", font=("Arial", 12), command=gl.hit)
hit_button.grid(row=2,column=0,pady=10, padx=10,sticky="nsew")
start_button = tk.Button(frame, text="Start Game", font=("Arial", 12), command=starting_game)
start_button.grid(row=2, column=1, pady=10, padx=10, sticky="nsew")
stand_button = tk.Button(frame, text="Stand", font=("Arial", 12), command=gl.stand)
stand_button.grid(row=2, column=2, pady=10, padx=10, sticky="nsew")
hit_button.grid_remove()
stand_button.grid_remove()
player_points_label = tk.Label(frame, text="Player Points: 0", font=("Arial", 12))
player_points_label.grid(row=3, column=0, columnspan=3, pady=5, sticky="nsew")
player_points_label.grid_remove()
dealer_points_label.grid_remove()
player_hand_frame = tk.Frame(frame)
player_hand_frame.grid(row=4, column=0, columnspan=3, sticky="nsew")
player_hand_frame.grid_propagate(False)
player_cards = []
root.mainloop()
def pop_up_window(msg):
popup = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_position = (screen_width - 100) // 2
y_position = (screen_height - 50) // 2
popup.geometry(f"150x70+{x_position}+{y_position}")
popup.resizable(width=False, height=False)
label = tk.Label(popup, text=msg, font=("Arial", 16))
label.pack(pady=20)
popup.mainloop()
def load_card_image(url,size=(80, 100)):
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img_resized = img.resize(size, Image.Resampling.LANCZOS)
img_tk = ImageTk.PhotoImage(img_resized)
return img_tk
def add_dealer_card(url = "https://deckofcardsapi.com/static/img/back.png"):
card_image = load_card_image(url)
card_label = tk.Label(dealer_hand_frame, image=card_image)
card_label.image = card_image
card_label.grid(row=0, sticky="nsew", column=len(dealer_cards), padx=5)
dealer_cards.append(card_label)
def add_player_card(url = "https://deckofcardsapi.com/static/img/back.png"):
card_image = load_card_image(url)
card_label = tk.Label(player_hand_frame, image=card_image)
card_label.image = card_image
card_label.grid(row=0, column=len(player_cards), padx=5)
player_cards.append(card_label)
def update_player_point(points):
player_points_label.config(text=f"Player Points: {points}")
def update_dealer_point(points):
dealer_points_label.config(text=f"Dealer Points: {points}")
def wait(miliseconds):
def do_nothing():
pass
root.after(miliseconds,do_nothing());
def remove_card():
if dealer_cards:
card_to_remove = dealer_cards.pop()
card_to_remove.destroy()
def starting_game():
start_button.grid_remove()
player_points_label.grid()
dealer_points_label.grid()
reset_playground()
gl.start_game()
def load_interface():
hit_button.grid()
stand_button.grid()
def reset_playground():
for widget in dealer_hand_frame.winfo_children():
widget.destroy()
for widget in player_hand_frame.winfo_children():
widget.destroy()
def game_over(msg):
ending_game_buttons()
pop_up_window(msg)
def ending_game_buttons():
hit_button.grid_remove()
stand_button.grid_remove()
start_button.grid()