-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdisplay.py
More file actions
221 lines (202 loc) · 9.09 KB
/
display.py
File metadata and controls
221 lines (202 loc) · 9.09 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
""" This module containts constants and functions for display """
### Import standard libraries ###
import cv2
from copy import deepcopy
# Drawing constants
RED = (0,0,255)
GREEN = (0,255,0)
BLUE = (255,0,0)
CYAN = (255,255,0)
MAGENTA = (255,0,255)
def regions(image, surface_obj, bg=True):
# Add the semi-transparent bg boxes if requested
if bg:
overlay = deepcopy(image)
cv2.rectangle(overlay, (0, 0), (220, 80), (0, 0, 0), -1)
cv2.rectangle(overlay, (surface_obj.player_region[0], 0), (surface_obj.player_region[0] + 220, 80), (0, 0, 0), -1)
alpha = 0.3
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
# Setup offsets for value printing
x_offset = 10
y_offset = 60
shadow_offset = 2
# Draw a line down the middle of the surface
cv2.line(image, (surface_obj.dealer_region[1] + shadow_offset, 0),
(surface_obj.dealer_region[1], int(surface_obj.height)), (0, 0, 0), 2)
cv2.line(image, (surface_obj.dealer_region[1], 0),
(surface_obj.dealer_region[1], int(surface_obj.height)), (255, 255, 255), 2)
# Add titles to each region of the display
cv2.putText(image, 'Dealer', (x_offset + shadow_offset, y_offset + shadow_offset), cv2.FONT_HERSHEY_SIMPLEX,
2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, 'Dealer', (x_offset, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, 'Player',
(surface_obj.dealer_region[1] + x_offset + shadow_offset, y_offset + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, 'Player', (surface_obj.dealer_region[1] + x_offset, y_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2, cv2.LINE_AA)
return
def hand_values(image, surface_obj, cards, state, bg=True, padded=True):
dealer_tally = 0
dealer_aces = 0
player_tally = 0
player_aces = 0
# Testing area for getting value of each hand
# Loop through each found card
for i in range(len(cards)):
# Set a flag if the current card is an ace
if cards[i].best_rank_match == 'Ace':
ace_flag = 1
else:
ace_flag = 0
# Check if the card belongs to the dealer or the player (based on x centroid) and assign as appropriate
if cards[i].center[0] >= surface_obj.dealer_region[0] and cards[i].center[0] <= \
surface_obj.dealer_region[1]:
dealer_tally += cards[i].value
if ace_flag:
dealer_aces += 1
elif cards[i].center[0] >= surface_obj.player_region[0] and cards[i].center[0] <= \
surface_obj.player_region[1]:
player_tally += cards[i].value
if ace_flag:
player_aces += 1
value_1 = dealer_tally
value_2 = player_tally
# Pad the values with a leading space if optional padded argument is set
# Convert to string in either case
if padded:
val_disp_1 = str(format(value_1, '2d'))
val_disp_2 = str(format(value_2, '2d'))
else:
val_disp_1 = str(value_1)
val_disp_2 = str(value_2)
# Add the semi-transparent bg boxes if requested
if bg:
overlay = deepcopy(image)
cv2.rectangle(overlay, (surface_obj.dealer_region[1] - 120, 0), (surface_obj.dealer_region[1], 80), (0, 0, 0), -1)
cv2.rectangle(overlay, (surface_obj.player_region[1] - 120 , 0), (surface_obj.player_region[1], 80), (0, 0, 0), -1)
alpha = 0.3
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
# Setup offsets for value printing
x_offset = surface_obj.dealer_region[1] - 100
y_offset = 60
shadow_offset = 2
# Add the hand values to the display
cv2.putText(image, val_disp_1, (x_offset + shadow_offset, y_offset + shadow_offset), cv2.FONT_HERSHEY_SIMPLEX,
2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, val_disp_1, (x_offset, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, val_disp_2, (surface_obj.dealer_region[1] + x_offset + shadow_offset, y_offset + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, val_disp_2, (surface_obj.dealer_region[1] + x_offset, y_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2, cv2.LINE_AA)
blackjack = 21
if dealer_tally == blackjack and state == 0:
x_pos = int(surface_obj.dealer_region[1] / 4)
y_pos = int(image.shape[0] / 2)
overlay = deepcopy(image)
cv2.rectangle(overlay, (x_pos - 20, y_pos - 60),
(x_pos + 390, y_pos + 20), (0, 0, 0),
-1)
alpha = 0.6
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
text = 'BLACKJACK!'
cv2.putText(image, text,
(x_pos + shadow_offset, y_pos + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, text, (x_pos, y_pos),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2,
cv2.LINE_AA)
if player_tally == blackjack and state == 0:
x_pos = int(surface_obj.player_region[1] / 8 +
surface_obj.dealer_region[1])
y_pos = int(image.shape[0] / 2)
overlay = deepcopy(image)
cv2.rectangle(overlay, (x_pos - 20, y_pos - 60),
(x_pos + 390, y_pos + 20), (0, 0, 0),
-1)
alpha = 0.6
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
text = 'BLACKJACK!'
cv2.putText(image, text,
(x_pos + shadow_offset, y_pos + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, text, (x_pos, y_pos),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2,
cv2.LINE_AA)
bust = 22
if dealer_tally >= bust and state == 0:
x_pos = int(surface_obj.dealer_region[1] / 3)
y_pos = int(image.shape[0] / 2)
overlay = deepcopy(image)
cv2.rectangle(overlay, (x_pos - 20, y_pos - 60),
(x_pos + 200, y_pos + 20), (0, 0, 0),
-1)
alpha = 0.6
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
text = 'BUST!'
cv2.putText(image, text,
(x_pos + shadow_offset, y_pos + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, text, (x_pos, y_pos),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2,
cv2.LINE_AA)
if player_tally >= bust and state == 0:
x_pos = int(surface_obj.player_region[1] / 6 +
surface_obj.dealer_region[1])
y_pos = int(image.shape[0] / 2)
overlay = deepcopy(image)
cv2.rectangle(overlay, (x_pos - 20, y_pos - 60),
(x_pos + 200, y_pos + 20), (0, 0, 0),
-1)
alpha = 0.6
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
text = 'BUST!'
cv2.putText(image, text,
(x_pos + shadow_offset, y_pos + shadow_offset),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2,
cv2.LINE_AA)
cv2.putText(image, text, (x_pos, y_pos),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2,
cv2.LINE_AA)
return
def bet(image, surface_obj, chips, chip_value=5, bg=True, padded=True):
bet_amt = chip_value * len(chips)
# Pad the bet amount with a leading space if optional padded argument is
# set; convert to string in either case
if padded:
bet_disp = str('Bet: ' + format(bet_amt, '2d'))
else:
bet_disp = str('Bet: ' + str(bet_amt))
# Add the semi-transparent bg box if requested
if bg:
overlay = deepcopy(image)
cv2.rectangle(overlay, (surface_obj.player_region[1] - 290,
image.shape[0]),
(surface_obj.player_region[1],
image.shape[0] - 80), (0, 0,
0), -1)
alpha = 0.3
cv2.addWeighted(overlay, alpha, image, 1 - alpha,
0, image)
# Setup offsets for bet amount printing
x_offset = surface_obj.player_region[1] - 260
y_offset = image.shape[0] - 20
shadow_offset = 2
# Add the bet amount to the display
cv2.putText(image, bet_disp, (x_offset + shadow_offset, y_offset +
shadow_offset), cv2.FONT_HERSHEY_SIMPLEX,
2, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(image, bet_disp, (x_offset, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 2,
(255, 255, 255), 2,
cv2.LINE_AA)
return