-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
354 lines (323 loc) · 12.7 KB
/
Client.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
import pygame
import sys
import Button
import RoundButton
import TextField
import socket
pygame.init()
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 600
GREEN = (0, 100, 0)
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Black Jack")
# Dealer Image
dealer_image = pygame.image.load("images/dealer.png")
dealer_image = pygame.transform.scale(dealer_image, (50, 50))
#Reversed card image
reversed_card_image = pygame.image.load("cards/back.png")
reversed_card_image = pygame.transform.scale(reversed_card_image, (80, 100))
# Card information
dealer_cards = []
player1_cards = []
player2_cards = []
player3_cards = []
# Player image
player_image = pygame.image.load("images/player.png")
player_image = pygame.transform.scale(player_image, (50, 50))
# All buttons and fields
start_button = Button.Button(400, 300, 200, 100, "START")
play_again_button = Button.Button(20, 20, 100, 50, "play again")
hit_button = Button.Button(20, 20, 100, 50, "HIT")
stay_button = Button.Button(20, 80, 100, 50, "STAY")
double_button = Button.Button(20, 140, 100, 50, "DOUBLE")
bet_50_button = RoundButton.RoundButton(40,230,25,(204,204,0),"50$")
bet_100_button = RoundButton.RoundButton(40,290,25,(0,0,204),"100$")
bet_all_in_button = RoundButton.RoundButton(40,350,25,(204,0,0),"All")
balance_field = TextField.TextField(20,500,150,70,"Balance")
server_is_full_message = TextField.TextField(400,200,200,70,"Server is full :(")
dealer_chat = TextField.TextField(600,50,300,50,"Waiting for players")
player_name = TextField.TextField(250,560,50,40,"You")
#All phases of the client
def menu():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if start_button.handle_event(event):
connecting_phase()
window.fill(GREEN)
start_button.draw(window)
pygame.display.flip()
def connecting_phase():
HOST = '127.0.0.1'
PORT = 12345
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
client_socket.settimeout(2)
data = client_socket.recv(1024).decode()
print(data)
if data == "server is full":
server_is_full_window()
elif data == "2":
player_name.rect.x = 500
elif data == "3":
player_name.rect.x = 750
client_data = client_socket.recv(1024).decode()
balance_field.change_text("Balance:\n" + client_data)
bet_phase(client_socket)
def server_is_full_window():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if start_button.handle_event(event):
connecting_phase()
window.fill(GREEN)
server_is_full_message.draw(window)
start_button.draw(window)
pygame.display.flip()
def bet_phase(client_socket):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client_socket.close()
pygame.quit()
sys.exit()
if bet_50_button.handle_event(event):
client_socket.sendall("bet50 button clicked".encode())
data = client_socket.recv(1024).decode()
if data == "incorrect bet":
dealer_chat.change_text("You can't place that bet")
else:
dealer_chat.change_text("Bet placed")
balance_field.change_text("Balance:\n" + data)
waiting_for_all_bets_phase(client_socket)
if bet_100_button.handle_event(event):
client_socket.sendall("bet100 button clicked".encode())
data = client_socket.recv(1024).decode()
if data == "incorrect bet":
dealer_chat.change_text("You can't place that bet")
else:
dealer_chat.change_text("Bet placed")
balance_field.change_text("Balance:\n" + data)
waiting_for_all_bets_phase(client_socket)
if bet_all_in_button.handle_event(event):
client_socket.sendall("betall button clicked".encode())
data = client_socket.recv(1024).decode()
if data == "incorrect bet":
dealer_chat.change_text("You can't place that bet")
else:
dealer_chat.change_text("Bet placed")
balance_field.change_text("Balance:\n" + data)
waiting_for_all_bets_phase(client_socket)
window.fill(GREEN)
window.blit(dealer_image, (500,80))
for number in range (1,4):
window.blit(player_image, (200*number*1.25,500))
bet_50_button.draw(window)
bet_100_button.draw(window)
bet_all_in_button.draw(window)
balance_field.draw(window)
dealer_chat.change_text("Place your bet")
dealer_chat.draw(window)
player_name.draw(window)
pygame.display.flip()
def waiting_for_all_bets_phase(client_socket):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client_socket.close()
pygame.quit()
sys.exit()
window.fill(GREEN)
window.blit(dealer_image, (500,80))
for number in range (1,4):
window.blit(player_image, (200*number*1.25,500))
balance_field.draw(window)
dealer_chat.change_text("Please wait for all bets")
dealer_chat.draw(window)
player_name.draw(window)
pygame.display.flip()
try:
data = client_socket.recv(1024).decode()
except:
continue
game_phase(client_socket, data)
def game_phase(client_socket, data):
read_data(data)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client_socket.close()
pygame.quit()
sys.exit()
if hit_button.handle_event(event):
client_socket.sendall("hit button clicked".encode())
card_data = client_socket.recv(1024).decode()
continue_data = client_socket.recv(1024).decode()
read_data(card_data)
if continue_data == "no":
waiting_for_results_phase(client_socket)
if stay_button.handle_event(event):
client_socket.sendall("stay button clicked".encode())
card_data = client_socket.recv(1024).decode()
read_data(card_data)
waiting_for_results_phase(client_socket)
if double_button.handle_event(event):
client_socket.sendall("double button clicked".encode())
card_data = client_socket.recv(1024).decode()
if card_data != "you are brokie":
read_data(card_data)
waiting_for_results_phase(client_socket)
window.fill(GREEN)
window.blit(dealer_image, (500,80))
for number in range (1,4):
window.blit(player_image, (200*number*1.25,500))
place_cards()
window.blit(reversed_card_image,(400,70))
balance_field.draw(window)
dealer_chat.change_text("You can play now")
dealer_chat.draw(window)
player_name.draw(window)
hit_button.draw(window)
stay_button.draw(window)
double_button.draw(window)
pygame.display.flip()
def waiting_for_results_phase(client_socket):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client_socket.close()
pygame.quit()
sys.exit()
window.fill(GREEN)
window.blit(dealer_image, (500,80))
place_cards()
window.blit(reversed_card_image,(400,70))
for number in range (1,4):
window.blit(player_image, (200*number*1.25,500))
balance_field.draw(window)
dealer_chat.change_text("Please wait for all players")
dealer_chat.draw(window)
player_name.draw(window)
pygame.display.flip()
try:
data = client_socket.recv(1024).decode()
except:
continue
result_phase(client_socket, data)
def result_phase(client_socket, data):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client_socket.close()
pygame.quit()
sys.exit()
if play_again_button.handle_event(event):
bet_phase(client_socket)
window.fill(GREEN)
window.blit(dealer_image, (500,80))
read_data(data.split("|")[0])
place_cards()
for number in range (1,4):
window.blit(player_image, (200*number*1.25,500))
balance_field.change_text("Balance:\n" + data.split("|")[1])
balance_field.draw(window)
dealer_chat.change_text("Better luck next time XD")
dealer_chat.draw(window)
player_name.draw(window)
if data.split("|")[1] != "0":
play_again_button.draw(window)
pygame.display.flip()
#Additional functions
def place_cards():
cords = [400,70]
for card in dealer_cards:
window.blit(card, cords)
cords[0] -= 50
cords[1] += 50
cords = [300,400]
for card in player1_cards:
window.blit(card, cords)
cords[0] += 50
cords[1] -= 50
cords = [500,400]
for card in player2_cards:
window.blit(card, cords)
cords[0] += 50
cords[1] -= 50
cords = [700,400]
for card in player3_cards:
window.blit(card, cords)
cords[0] += 50
cords[1] -= 50
def read_data(data):
card_size = (80,100)
dealer_cards.clear()
player1_cards.clear()
player2_cards.clear()
player3_cards.clear()
splitted_data = data.split(",")
for card in splitted_data[0].split(" "):
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, card_size)
dealer_cards.append(card_image)
for card in splitted_data[1].split(" "):
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, card_size)
player1_cards.append(card_image)
if len(splitted_data) > 2:
for card in splitted_data[2].split(" "):
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, card_size)
player2_cards.append(card_image)
if len(splitted_data) > 3:
for card in splitted_data[3].split(" "):
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, card_size)
player3_cards.append(card_image)
def make_cards_images(data):
new_data = data.split(",")
cards = new_data[0].split(" ")
for card in cards:
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, (25, 50))
dealer_cards.append(card_image)
cards = new_data[1].split(" ")
for card in cards:
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, (25, 50))
player1_cards.append(card_image)
if len(new_data) > 2:
cards = new_data[2].split(" ")
for card in cards:
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, (25, 50))
player2_cards.append(card_image)
if len(new_data) > 3:
cards = new_data[3].split(" ")
for card in cards:
if card == "":
continue
card_image = pygame.image.load("cards/" + card + ".png")
card_image = pygame.transform.scale(card_image, (25, 50))
player3_cards.append(card_image)
#Main
if __name__ == "__main__":
menu()