-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclasses.py
638 lines (511 loc) · 23 KB
/
classes.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#!/usr/bin/python3.7.4
# Setup Python ----------------------------------------------- #
import cv2
import pygame as pg
from pygame import mixer
import os
import uuid
from random import sample
# Setup classes ---------------------------------------------- #
class PgSetup(object):
# Constructor ------------------------------------------------ #
def __init__(self, WIDTH, HEIGHT):
# Creates the screen object itself - gets the length and width of the screen.
os.environ['SDL_VIDEO_CENTERED'] = '1'
pg.init()
pg.key.set_repeat(150, 350)
pg.font.init()
pg.display.set_caption('DrawGuess_Client')
pg.display.set_icon(pg.image.load('assets\\images\\icons\\programicon.png'))
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.resolution_options = ['640 x 360', '768 x 432', '960 x 540',
'1280 x 720', '1600 x 900', '1920 x 1080']
if f'{WIDTH} x {HEIGHT}' not in self.resolution_options:
self.WIDTH = 960
self.HEIGHT = 540
else:
self.WIDTH = WIDTH
self.HEIGHT = HEIGHT
self.IDEAL_WIDTH = 1920
self.IDEAL_HEIGHT = 1080
self.width_scale = self.WIDTH / self.IDEAL_WIDTH
self.height_scale = self.HEIGHT / self.IDEAL_HEIGHT
# Getters ---------------------------------------------------- #
def getScreen(self):
return self.screen
def getWidthScale(self):
return self.width_scale
def getHeightScale(self):
return self.height_scale
def getClock(self):
return self.clock
def getWidth(self):
return self.WIDTH
def getHeight(self):
return self.HEIGHT
def getResolutionOptions(self):
return self.resolution_options
# Setters ---------------------------------------------------- #
def setWidth(self, width):
self.WIDTH = width
def setHeight(self, height):
self.HEIGHT = height
def setScreen(self, width, height):
self.screen = pg.display.set_mode((width, height))
# Functions -------------------------------------------------- #
def update(self, x=None):
# Updates part of the screen (sometimes the whole) on every event that takes place in the game.
self.clock.tick(60)
if x is None:
pg.display.update()
else:
pg.display.update(x)
def flip(self, tick=60):
# Updates the entire screen on every event that takes place in the game.
pg.display.flip()
self.clock.tick(tick)
def blit(self, pic, pos):
# Display images / colors on the screen (depending on the location set by the user).
pic = pg.transform.scale(pic, (self.WIDTH, self.HEIGHT))
self.screen.blit(pic, (pos[0] * self.width_scale, pos[1] * self.height_scale))
def screen_resize(self, width, height):
# Change the screen aspect ratio (such as image / text size and location, etc.) according to the resolution.
self.WIDTH = width
self.HEIGHT = height
self.width_scale = width / self.IDEAL_WIDTH
self.height_scale = height / self.IDEAL_HEIGHT
self.screen = pg.display.set_mode((self.WIDTH, self.HEIGHT))
os.environ['SDL_VIDEO_CENTERED'] = '1'
def draw_text(self, text, color, font, size, x, y):
# Display text on the screen (depending on the color, size, font and location selected by the user).
font1 = pg.font.Font(font, int(size * (self.height_scale + self.width_scale / 3) / 2))
surface = font1.render(text, True, color)
self.screen.blit(surface, (x * self.width_scale, y * self.height_scale))
@staticmethod
def loadify(image):
# Upload an image to the system as a variable.
return pg.image.load(image).convert_alpha()
@staticmethod
def quit():
# Closes the screen.
pg.display.quit()
pg.font.quit()
class Sound(object):
# Constructor ------------------------------------------------ #
def __init__(self, volume, sound, play=True):
# Creates the sound object - receives the volume and location of the asset from the asset folder from the user.
self.play = play
self.sound = mixer.Sound(sound)
self.volume = volume
if type(self.volume) == 'int' or 'float':
self.sound.set_volume(volume)
else:
self.sound.set_volume(0.2)
# Getters ---------------------------------------------------- #
def getVolume(self):
return self.volume
def getPlay(self):
return self.play
# Setters ---------------------------------------------------- #
def setVolume(self, volume):
self.volume = volume
def setPlay(self, play):
self.play = play
# Functions -------------------------------------------------- #
def play_sound(self, event):
# Activates sound according to user action (event).
if self.play is True:
if event.type == pg.MOUSEBUTTONDOWN:
self.sound.play()
self.play = False
if event.type == pg.MOUSEBUTTONUP:
self.play = True
def play_sound_static(self):
# Activates sound regardless of user action.
if self.play is True:
self.sound.play()
self.play = False
def reset(self):
if self.play is False:
self.setPlay(True)
class Music:
# Constructor ------------------------------------------------ #
def __init__(self, volume, play=True, music='assets\\sounds\\Theme_Music.mp3'):
# Loads the music from the assets folder and defines the class as the interface music. Gets volume from the user.
mixer.music.load(music)
pg.mixer.music.play(-1)
self.play = play
self.volume = volume
if type(self.volume) == 'int' or 'float':
pg.mixer.music.set_volume(self.volume)
else:
pg.mixer.music.set_volume(0.01)
# Getters ---------------------------------------------------- #
def getVolume(self):
return self.volume
def getPlay(self):
return self.play
# Setters ---------------------------------------------------- #
def setVolume(self, volume):
if type(self.volume) == 'int' or 'float':
pg.mixer.music.set_volume(volume)
else:
pg.mixer.music.set_volume(0.01)
# Functions -------------------------------------------------- #
def play_music(self, event):
# Activate / stop the music according to the user's decision (event).
if event.type == pg.MOUSEBUTTONDOWN:
if self.play is False:
pg.mixer.music.unpause()
self.play = True
else:
pg.mixer.music.pause()
self.play = False
def display_icon(self, screen, icon_mute_on, icon_mute_off):
# Display the speaker icons (on / off) on the screen.
if self.play is False:
screen.blit(icon_mute_on, (0, 0))
else:
screen.blit(icon_mute_off, (0, 0))
class InputBox(object):
# Constructor ------------------------------------------------ #
def __init__(self, screen, x, y, w, h, font, text_size, active_color, inactive_color, max_str_len=10, border=True,
text=''):
# Creates the text box object - receives from the user the screen, location, length and width, font, text size,
# color when the box is active and inactive, maximum number of letters and whether the border will be visible (True / False).
self.screen = screen
self.w = w
self.h = h
self.x = x
self.y = y
self.color = active_color
self.inactive_color = inactive_color
self.active_color = active_color
self.border = border
self.active = False
self.text = text
self.text_size = text_size
self.font_raw = font
self.max_str_len = max_str_len
self.font = pg.font.Font(self.font_raw, int(self.text_size * self.screen.getHeightScale()))
self.txt_surface = self.font.render(self.text, True, self.color)
self.rect = pg.Rect(self.x * self.screen.getWidthScale(), self.y * self.screen.getHeightScale(), self.w,
self.h * self.screen.getHeightScale())
# Getters ---------------------------------------------------- #
def getText(self):
return self.text
def getActive(self):
return self.active
# Setters ---------------------------------------------------- #
def setFont(self, font):
self.font = pg.font.Font(font, self.text_size)
def setTextSize(self, text_size):
self.text_size = text_size
def setText(self, text):
self.text = text
def setRectY(self, y):
self.rect = pg.Rect(self.x, y, self.w, self.h)
# Functions -------------------------------------------------- #
def handle_event_send(self, event):
# Writes in the variable "text" the letters received from the user and after pressing the key "Enter" resets the variable.
self.txt_surface = self.font.render(self.text, True, self.color)
if event.type == pg.KEYDOWN:
if event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
elif len(self.text) <= self.max_str_len:
if event.unicode.encode().isalpha() or event.unicode.isdigit() or event.unicode in "!@#$%^&*()-+?_=,<>/" or event.unicode == ' ':
self.text += event.unicode
def handle_event_save(self, event):
# Writes in the variable "text" the letters received from the user and after pressing the key "Enter" makes the text box inactive.
self.resize()
if event.type == pg.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
else:
self.active = False
self.color = self.active_color if self.active else self.inactive_color
if self.active:
self.txt_surface = self.font.render(self.text, True, self.color)
if event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
self.color = self.inactive_color
self.active = False
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
elif len(self.text) <= self.max_str_len:
if event.unicode.encode().isalpha() or event.unicode.isdigit() or event.unicode in "!@#$%^&*()-+?_=,<>/" or event.unicode == ' ':
self.text += event.unicode
# Re-render the text.
else:
self.color = self.inactive_color
self.txt_surface = self.font.render(self.text, True, self.color)
def update(self, w):
# Updates the text box size according to the length of the variable.
width = max(w, self.txt_surface.get_width() + 10)
self.rect.w = width
def draw(self, screen):
# Displays the text box on the screen.
screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
if self.border:
pg.draw.rect(screen, self.color, self.rect, 4)
def run(self, event, action):
# Runs the text box function in the program itself (send / save depending on the situation)
if action == 'SAVE':
self.handle_event_save(event)
elif action == 'SEND':
self.handle_event_send(event)
self.draw(self.screen.getScreen())
self.update(self.w * self.screen.getWidthScale())
def resize(self):
# Resizes the length and width of the text box according to the screen resolution.
self.font = pg.font.Font(self.font_raw, int(self.text_size * self.screen.getHeightScale()))
self.txt_surface = self.font.render(self.text, True, self.color)
self.rect = pg.Rect(self.x * self.screen.getWidthScale(), self.y * self.screen.getHeightScale(),
self.w * self.screen.getWidthScale(), self.h * self.screen.getHeightScale())
class Pen(object):
# Default Constructor ---------------------------------------- #
def __init__(self):
# The painter's pen object.
self.COLOR = (0, 0, 0)
self.pen_thickness = 10
# Getters ---------------------------------------------------- #
def getColor(self):
return self.COLOR
def getPenThickness(self):
return self.pen_thickness
# Setters ---------------------------------------------------- #
def setColor(self, COLOR_RGB):
self.COLOR = COLOR_RGB
def setPenThickness(self, pen_thickness):
self.pen_thickness = pen_thickness
# Functions -------------------------------------------------- #
def flood_fill(self, screen, pos):
# Painter bucket filling function.
arr = pg.surfarray.array3d(screen.getScreen())
swapPoint = (pos[1], pos[0])
cv2.floodFill(arr, None, swapPoint, self.COLOR)
pg.surfarray.blit_array(screen.getScreen(), arr)
class Slider(object):
# Constructor ------------------------------------------------ #
def __init__(self, screen, x, y, w, h):
# Creates the slider object (sliding template) designed to tune the music. Receives from the user the screen,
# location and length and width of the slider.
self.screen = screen
self.x = x
self.y = y
self.w = w
self.h = h
self.volume = 50
self.sliderRect = pg.Rect(self.x * self.screen.getWidthScale(), self.y * self.screen.getHeightScale(),
self.w * self.screen.getWidthScale(), self.h * self.screen.getHeightScale())
self.circle_x = int(self.x * self.screen.getWidthScale() + self.sliderRect.w / 2)
# Getters ---------------------------------------------------- #
def getVolume(self):
return self.volume
# Setters ---------------------------------------------------- #
def setVolume(self, volume):
self.volume = volume
# Functions -------------------------------------------------- #
def draw(self, screen):
# Displays the slider on the screen.
pg.draw.rect(screen, (255, 255, 255), self.sliderRect)
pg.draw.circle(screen, (255, 240, 255), (self.circle_x, (self.sliderRect.h / 2 + self.sliderRect.y)),
self.sliderRect.h * 1.5)
def update_volume(self, x):
# Updates the volume of the music according to the position of the slider.
if x < self.sliderRect.x:
self.volume = 0
elif x > self.sliderRect.x + self.sliderRect.w:
self.volume = 100
else:
self.volume = int((x - self.sliderRect.x) / float(self.sliderRect.w) * 100)
def on_slider(self, x, y):
# Returns True answer if you hover the mouse over the slider.
if self.on_slider_hold(x, y) or self.sliderRect.x <= x <= \
self.sliderRect.x + self.sliderRect.w and self.sliderRect.y <= y <= self.sliderRect.y + self.sliderRect.h:
return True
else:
return False
def on_slider_hold(self, x, y):
# Returns True answer if the mouse cursor clicks on the slider.
if ((x - self.circle_x) * (x - self.circle_x) + (y - (self.sliderRect.y + self.sliderRect.h / 2)) * (
y - (self.sliderRect.y + self.sliderRect.h / 2))) \
<= (self.sliderRect.h * 1.5) * (self.sliderRect.h * 1.5):
return True
else:
return False
def handle_event(self, x, y):
# Operates the slider using the functions mentioned above.
if self.on_slider_hold(x, y) and pg.mouse.get_pressed()[0] \
or self.on_slider(x, y) and pg.mouse.get_pressed()[0]:
if x < self.sliderRect.x:
self.circle_x = self.sliderRect.x
elif x > self.sliderRect.x + self.sliderRect.w:
self.circle_x = self.sliderRect.x + self.sliderRect.w
else:
self.circle_x = x
self.update_volume(x)
self.draw(self.screen.getScreen())
def slider_update(self):
# Updates the size of the slider according to the screen resolution.
self.sliderRect = pg.Rect(self.x * self.screen.getWidthScale(), self.y * self.screen.getHeightScale(),
self.w * self.screen.getWidthScale(), self.h * self.screen.getHeightScale())
self.circle_x = int(self.x * self.screen.getWidthScale() + self.sliderRect.w / 2)
self.volume = 50
class Client(object):
# Constructor ------------------------------------------------ #
def __init__(self, client, addr=None):
# Creates the client object that contains all the details of a particular client.
# The object receives a client and in some cases an address.
self.client = client
self.nickname = ''
if addr is not None:
self.addr = addr
self.index = None
self.lobby = None
self.was_painter = False
self.client_id = str(uuid.uuid4())
self.client_status = 'In_Lobby_Picker'
self.score = 0
def __repr__(self):
# A function whose function is to print the client's name when printing (instead of the object details)
return self.nickname
# Getters ---------------------------------------------------- #
def getNickname(self):
return self.nickname
def getAddr(self):
return self.addr
def getClient(self):
return self.client
def getLobby(self):
return self.lobby
def getIndex(self):
return self.index
def getClientID(self):
return self.client_id
def getWasPainter(self):
return self.was_painter
def getScore(self):
return self.score
def getClientStatus(self):
return self.client_status
# Setters ---------------------------------------------------- #
def setIndex(self, index):
self.index = index
def setWasPainter(self, was_painter):
self.was_painter = was_painter
def setLobby(self, lobby):
self.lobby = lobby
def setNickname(self, nickname):
self.nickname = nickname
def setScore(self, score):
self.score = score
def setClientStatus(self, client_status):
self.client_status = client_status
# Functions -------------------------------------------------- #
def close(self):
self.client.close()
class Lobby(object):
# Constructor ------------------------------------------------- #
def __init__(self, lobby_owner, lobby_name, lobby_password=None):
# Creates the lobby object that contains all the details of a particular lobby.
# The object is given a manager name, a lobby name and in some cases a password.
self.lobby_owner = lobby_owner
self.lobby_name = lobby_name
self.lobby_password = lobby_password
self.game_status = 'INACTIVE'
self.players_list = [self.lobby_owner]
self.words = self.getRandomWord(6)
# Getters ---------------------------------------------------- #
def getLobbyOwner(self):
return self.lobby_owner
def getPlayersList(self):
return self.players_list
def getLobbySpecs(self):
return self.lobby_name, self.lobby_password
def getGameStatus(self):
return self.game_status
def getWords(self):
return self.words
# Setters ---------------------------------------------------- #
def setLobbyOwner(self, owner):
self.lobby_owner = owner
def setWords(self, words):
self.words = words
def setGameStatus(self, game_status):
self.game_status = game_status
# Functions -------------------------------------------------- #
def appendPlayersList(self, player):
# A function that expands the list of players in a particular player's lobby.
self.players_list.append(player)
def removePlayersList(self, player):
# A function that removes a particular player from the lobby.
if player in self.players_list:
self.players_list.remove(player)
@staticmethod
def getRandomWord(x=1):
# Returns a list of random words from the vocabulary (the number of words is determined by the value x)
with open('assets\\words.txt', "r") as f:
words = []
for line in f:
words.append(line[0:-1])
return sample(words, x)
class Player(object):
# Constructor ------------------------------------------------ #
def __init__(self, nickname):
# Creates a player object after connecting to the server - receives a nickname from the user.
self.IP = '127.0.0.1'
self.PORT = 5055
self.nickname = nickname
self.addr = (self.IP, self.PORT)
self.score = 0
self.fill_clicked = False
self.eraser_clicked = False
self.pen_clicked = True
self.isPainter = False
self.guessed = False
self.num_of_guessers = 0
# Getters ---------------------------------------------------- #
def getAddr(self):
return self.addr
def getNickname(self):
return self.nickname
def getIsPainter(self):
return self.isPainter
def getGuessed(self):
return self.guessed
def getNumOfGuessers(self):
print(self.num_of_guessers)
return self.num_of_guessers
def getPenClicked(self):
return self.pen_clicked
def getEraserClicked(self):
return self.eraser_clicked
def getFillClicked(self):
return self.fill_clicked
def getScore(self):
return self.score
# Setters ---------------------------------------------------- #
def setNickname(self, nickname):
self.nickname = nickname
def setIsPainter(self, IsPainter):
self.isPainter = IsPainter
def setGuessed(self, guessed):
self.guessed = guessed
def setNumOfGuessers(self, num_of_guessers):
self.num_of_guessers = num_of_guessers
def setPenClicked(self, pen_clicked):
self.eraser_clicked = False
self.fill_clicked = False
self.pen_clicked = pen_clicked
def setEraserClicked(self, eraser_clicked):
self.pen_clicked = False
self.fill_clicked = False
self.eraser_clicked = eraser_clicked
def setFillClicked(self, fill_clicked):
self.pen_clicked = False
self.eraser_clicked = False
self.fill_clicked = fill_clicked
def setScore(self, score):
self.score = score