-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
391 lines (332 loc) · 12 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
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
import pygame
from network import Network
# Variables
undercover = "Undercover"
civilian = "Civilian"
mr_white = "Mr. White"
sep = ";" # separator in the txt file
def retrieve_words(files_name):
return [tuple(word.strip().split(sep)) for word in open(files_name, "r").readlines()]
# Pygame
screen_w = 1200 # img size: 1200
screen_h = 650 # img size: 900
pygame.init()
screen = pygame.display.set_mode((screen_w, screen_h))
pygame.display.set_caption("Undercover")
# pygame.display.set_icon(pygame.image.load('icon.png'))
# Buttons
class Button:
def __init__(self, color, x, y, w, h, txt=""):
self.color = color
self.x = x
self.y = y
self.w = w
self.h = h
self.txt = txt
def draw(self, screen):
"""Call this method to draw the button on the screen"""
pygame.draw.rect(screen, self.color, (self.x, self.y, self.w, self.h), 0)
if self.txt != '':
font = pygame.font.SysFont('Charlemagne Std', 20)
txt = font.render(self.txt, True, (0, 0, 0))
screen.blit(txt, (self.x + (self.w // 2 - txt.get_width() // 2), self.y + (self.h // 2 - txt.get_height() // 2)))
def click(self, pos):
"""Pos is the mouse position or a tuple of (x, y) coordinates"""
if pos[0] > self.x and pos[0] < (self.x + self.w):
if pos[1] > self.y and pos[1] < (self.y + self.h):
return True
return False
btn_hovered_c = (255, 255, 255)
btn_inactive_c = (210, 210, 210)
# Buttons creation
margin = 125
space = (screen_w - margin * 2 - 150 * 5) / 4
bg_btn_w = 150
btn_h = 30
red_btn = Button(btn_inactive_c, margin, 15, bg_btn_w, btn_h, 'Rouge')
yellow_btn = Button(btn_inactive_c, margin + bg_btn_w + space, 15, bg_btn_w, btn_h, 'Jaune')
deep_blue_btn = Button(btn_inactive_c, margin + bg_btn_w * 2 + space * 2, 15, bg_btn_w, btn_h, 'Bleu foncé')
blue_btn = Button(btn_inactive_c, margin + bg_btn_w * 3 + space * 3, 15, bg_btn_w, btn_h, 'Bleu')
green_btn = Button(btn_inactive_c, margin + bg_btn_w * 4 + space * 4, 15, bg_btn_w, btn_h, 'Vert')
bg_btns = [red_btn, yellow_btn, deep_blue_btn, blue_btn, green_btn]
create_room_btn = Button(btn_inactive_c, screen_w / 2 - bg_btn_w - 100, screen_h // 2 - btn_h, bg_btn_w, btn_h, 'Lancer une partie')
join_room_btn = Button(btn_inactive_c, screen_w / 2 + 100, screen_h // 2 - btn_h, bg_btn_w, btn_h, 'Rejoindre une partie')
begin_btn = Button(btn_inactive_c, screen_w / 2 - 150 / 2, 435, 150, 30, 'Commencer')
ready_btn = Button(btn_inactive_c, screen_w / 2 - 150 / 2, 300, 150, 30, 'Prêts')
next_btn = Button(btn_inactive_c, screen_w / 2 - 150 / 2 - 25, screen_h - 90, 200, 30, 'Jouer avec les prochains mots')
all_btns = [red_btn, yellow_btn, deep_blue_btn, blue_btn, green_btn, create_room_btn, join_room_btn, begin_btn, ready_btn, next_btn]
# Backgrounds
red_com = pygame.image.load('bg1.gif')
yellow_com = pygame.image.load('bg2.gif')
deep_blue = pygame.image.load('bg3.gif')
blue_com = pygame.image.load('bg4.gif')
green_com = pygame.image.load('bg5.gif')
background = deep_blue
# Function to change the background
def change_bg(btn):
global background
if btn == red_btn:
background = red_com
elif btn == yellow_btn:
background = yellow_com
elif btn == deep_blue_btn:
background = deep_blue
elif btn == blue_btn:
background = blue_com
elif btn == green_btn:
background = green_com
# Input box & text classes & functions
black = (0, 0, 0)
white = (255, 255, 255)
FONT = pygame.font.Font(None, 32)
txtbox_active_c = white
txtbox_inactive_c = (150, 150, 150)
input_content = None
# Input boxes
class InputBox:
def __init__(self, x, y, w, h, txt=''):
self.rect = pygame.Rect(x, y, w, h)
self.txt_color = black
self.color = txtbox_inactive_c
self.txt = txt
self.txt_surface = FONT.render(txt, True, self.txt_color)
self.active = False
def handle_event(self, event):
global step, input_content
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect
if self.rect.collidepoint(event.pos):
# Toggle the active variable
self.active = not self.active
else:
self.active = False
# Change the color of the input box
self.color = txtbox_active_c if self.active else txtbox_inactive_c
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
input_content = self.txt
step += 1
elif event.key == pygame.K_BACKSPACE:
self.txt = self.txt[:-1]
else:
self.txt += event.unicode
# Re-render the txt
self.txt_surface = FONT.render(self.txt, True, self.txt_color)
def draw(self, screen):
# Blit the rect
pygame.draw.rect(screen, self.color, self.rect, 0)
# Blit the txt
screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
def return_input(self):
return self.txt
def ask_sth_centered(txt=''):
global player_name, step
# txt
txt_w, txt_h = FONT.size(txt)
x = int((screen_w // 2) - (txt_w // 2))
y = int((screen_h // 2) - (txt_h - 10))
rect = pygame.Rect(x, y, txt_w, txt_h)
txt_surface = FONT.render(txt, True, white)
screen.blit(txt_surface, rect)
clock = pygame.time.Clock()
# box
box_x = screen_w // 2 - 150 // 2 - 25
box_y = screen_h // 2 - 50
box_w = 200
box_h = 30
input_box = InputBox(box_x, box_y + 60, box_w, box_h)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
running = False
pos = pygame.mouse.get_pos()
input_box.handle_event(event)
if step == 2 or step == 4 or step == 6 or step == 8: # if step % 2 == 0?
done = True
input_box.draw(screen)
pygame.display.flip()
clock.tick(30)
# Texts
def display_txt(x, y, w, h, txt):
rect = pygame.Rect(x, y, w, h)
txt_surface = FONT.render(txt, True, white)
screen.blit(txt_surface, rect)
def display_txt_x_center(y, h, txt):
txt_w, txt_h = FONT.size(txt)
x = int((screen_w // 2) - (txt_w // 2))
rect = pygame.Rect(x, y, txt_w, h)
txt_surface = FONT.render(txt, True, white)
screen.blit(txt_surface, rect)
def display_players(players_names):
i = 0
y = 180
while i <= (len(players_names) - 1):
display_txt(50, y, 185, 30, "{}".format(players_names[i]))
i += 1
y += 45
def draw_bg_btns():
for btn in bg_btns:
btn.draw(screen)
def display_role(role, player_name, words, words_nb):
if role == civilian:
display_txt_x_center(230, 30, "{}, votre mot est {}.".format(player_name, words[words_nb - 1][0]))
elif role == undercover:
display_txt_x_center(230, 30, "{}, votre mot est {}.".format(player_name, words[words_nb - 1][1]))
elif role == mr_white:
display_txt_x_center(230, 30, "{}, vous êtes {}.".format(player_name, mr_white))
else:
print("Erreur 'roles_distribution'")
def display_list(lst, x, y):
"""display a list at the 'indice+1. item' format"""
i = 0
display_txt(x, y, 185, 30, "Ordre de jeu :")
y += 45
while i <= (len(lst) - 1):
display_txt(x, y, 185, 30, "{}. {}".format(i + 1, lst[i]))
i += 1
y += 45
step = 1
launch_ready = False
def main():
global step, input_content, words, launch_ready
run = True
clock = pygame.time.Clock()
n = Network()
player_id = n.get_id()
print('You are player', player_id)
player_name = n.send("0 "+input_content)
print("You are", player_name)
player_launcher = None
join_room_existing = True
players_names = []
words_nb = 0
while run:
clock.tick(60)
screen.blit(background, (0, 0))
if step == 2: # retrieve words
print("2, c'est la step", step)
words = retrieve_words('undercover_words.txt')
step += 1
elif step == 3: # choose to create a room (player_launcher) or join one
print("3, c'est la step", step)
display_txt_x_center(250, 30, "Bonjour {} !".format(player_name))
create_room_btn.draw(screen)
join_room_btn.draw(screen)
elif step == 4: # for tests purposes
print("4, c'est la step", step)
print("player launcher:", player_launcher)
step += 1
elif step == 5:
print("5, c'est la step", step)
if player_launcher:
ask_sth_centered("Entrez le nom que vous voulez donner à la partie :")
else:
ask_sth_centered("Entrez le nom de la partie que vous voulez rejoindre :")
print("join room existing? ", join_room_existing)
if not join_room_existing:
display_txt_x_center(155, 30, "Le nom de partie que vous avez choisi n'existe pas.")
elif step == 6: # send the room_name (create the info for launcher, checks if it exists for !launcher)
print("6, c'est la step", step)
if player_launcher:
room_name = n.send("4 "+input_content)
step += 1
else:
room_name = input_content
join_room_existing = n.send("5 " + input_content)
print("room existing? ", join_room_existing)
if join_room_existing:
step += 5
else:
step -= 1
elif step == 7: # for player_launcher only : choose the couple of words to begin from
print("c'est la step", step)
ask_sth_centered("Choisissez un nombre de couple de mots entre 1 et {}".format(len(words)))
elif step == 8: # for player_launcher only: send the words couple number
print("c'est la step", step)
words_nb = n.send("6 "+input_content)
step += 1
elif step == 9: # for player_launcher only
print("c'est la step", step)
draw_bg_btns()
display_txt_x_center(105, 30, "Couple de mots : {}".format(words_nb))
display_txt_x_center(155, 30, "Nom de la partie : {}".format(room_name))
ready_btn.draw(screen)
players_names = n.send("1 " + player_name)
if isinstance(players_names, list):
display_players(players_names)
elif step == 10: # not used for the moment
pass
elif step == 11: # for !player_launcher only
print("c'est la step", step)
draw_bg_btns()
display_txt_x_center(200, 30, "En attente du lancement du jeu")
players_names = n.send("1 " + player_name)
if isinstance(players_names, list):
display_players(players_names)
launch_ready = n.send("7 ")
if launch_ready:
step += 1
elif step == 12: # for everybody, launcher or not: get the player role and game order, reset launch_ready to False
player_info = n.send("9 ")
pygame.time.delay(100) # necessary or it seems that the server global launch_ready becomes False before some of the clients have retrieved the True value and got to this step
launch_ready = n.send("10")
print("Test : launch_ready est pour le joueur", player_id, " ", launch_ready)
step += 1
elif step == 13: # display the roles and play order, player_launcher has a next_button
draw_bg_btns()
display_players(players_names)
display_role(player_info[0], player_name, words, player_info[1])
display_list(player_info[2], screen_w / 2 - 150 / 2, 275)
if player_launcher:
next_btn.draw(screen)
else:
launch_ready = n.send("7 ")
if launch_ready:
step = 12
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in bg_btns:
if btn.click(pos):
change_bg(btn)
if create_room_btn.click(pos):
player_launcher = n.send("2 ")
step += 1
if join_room_btn.click(pos):
player_launcher = n.send("3 ")
step += 1
if ready_btn.click(pos): # only player_id = 0 see this button and can interact with it
launch_ready = n.send("8 ")
step = 12
if next_btn.click(pos): # only player_id = 0 see this button and can interact with it
print("next")
words_nb = n.send("11")
launch_ready = n.send("8 ")
step = 12
if event.type == pygame.MOUSEMOTION:
for btn in all_btns:
if btn.click(pos):
btn.color = btn_hovered_c
else:
btn.color = btn_inactive_c
pygame.display.update()
def beginning_screen():
global step
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
screen.blit(background, (0, 0))
ask_sth_centered("Entrez votre nom")
pygame.display.update()
if step == 2:
run = False
main()
while True:
beginning_screen()