-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman.py
1953 lines (1589 loc) · 81.8 KB
/
Hangman.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame, math, sys, random, time
from pygame.locals import *
#should automate finding the longesst width for all buttons based on longest text used
###########################################################
# PABM ('TEXT', posx, posy, C_or_TL='center',
# fontsize=36, fixedtoright=False, fixedtobottom=False,
# fontcolor=WHITE, proportionalbuttonshape=False, completecoverage=False, buttonwidthx=0, buttonheighty=0,
# proportionalbuttonlocationx=False, proportionalbuttonlocationy=False, useimage=False,
# useborder=(1|2|3), blitted=False, buttonstate=(1|2), pressed=False)
# True False #
###########################################################
pygame.init()
def mf(a):
return math.floor(a)
class Pygamedisplay():
def __init__(self, swidth, sheight):
self.swidth = swidth
self.sheight = sheight
def return_swidth(self):
return self.swidth
def return_sheight(self):
return self.sheight
class CentralizedVariable: #re-usable
def __init__(self, var):
self.var = var
self.var_original = var * 1
def increase_var(self, value=None):
if type(self.var) == list:
if value != None:
self.var.append(value)
else:
raise Exception("Variable is not in list. Check your input.")
elif type(self.var) != list:
if value == None:
self.var += 1
else:
raise Exception('Cannot use new value for non-list variable.')
def decrease_var(self, value=None):
if type(self.var) == list:
if value != None and value in self.var:
self.var.remove(value)
else:
#self.var = self.var[:-1]
raise Exception("Variable is not in list. Check your input.")
elif type(self.var) != list:
if value == None:
self.var -= 1
else:
raise Exception('Cannot use new value for non-list variable.')
def append_to_list(self, value=None): # may be redundant
if type(self.var) == list:
if value != None:
self.var.append(value)
elif value == None:
raise Exception("Check variable")
else:
raise Exception("Not for instance with non-list type")
def remove_from_list(self, value=None): # may be redundant
if type(self.var) == list:
if value in self.var:
self.var.remove(value)
elif value == None:
raise Exception('Check variable')
elif value not in self.var:
raise Exception('Check variable')
else:
raise Exception("Not for instance with non-list type")
def return_var(self):
return self.var
def return_var_original(self):
return self.var_original
def return_var_len(self):
return len(self.var)
class Phrase: #potentially re-usable
def __init__(self, ANYTEXT, posx, posy, fontsize):
self.ANYTEXT = ANYTEXT
self.posx = posx
self.posy = posy
self.fontsize = fontsize
def blit(self, newtext=''):
if newtext != '':
self.ANYTEXT = newtext
basicfontx = pygame.font.SysFont('calibri', self.fontsize)
#--------
try:
phrasex = basicfontx.render(self.ANYTEXT, 1, (240, 240, 240))
except pygame.error:
global inputa, storedtext1
phrasex = basicfontx.render(None, 1, (240, 240, 240))
inputa = storedtext1 = ''
#--------
phrasexrect = phrasex.get_rect()
phrasexrect.centerx = self.posx; phrasexrect.centery = self.posy
windowsurface.blit(phrasex, phrasexrect)
return phrasexrect
def blitwithbutton(self, N, S=0):
basicfontx = pygame.font.SysFont('calibri', self.fontsize)
#--------
phrasex = basicfontx.render(self.ANYTEXT, 1, (255, 255, 255))
#--------
phrasexrect = phrasex.get_rect()
#buttonrect = pygame.Rect(0, 0, mf(phrasexrect.width * 1.05), phrasexrect.height)
if S == 0:
#uttonrect = pygame.Rect(0, 0, mf((phrasexrect.width * 1.00)+(dimension1.return_swidth()*.03)), mf(dimension1.return_sheight()*.06)) #enough to fix?
buttonrect = pygame.Rect(0, 0, 150, 30)
elif S == 1:
buttonrect = pygame.Rect(0, 0, 50, 30)
elif S == 2:
buttonrect = pygame.Rect(0, 0, mf(dimension1.return_swidth()*.1), mf(dimension1.return_sheight()*.06))
if N == 1:
phrasexrect.centerx = self.posx; phrasexrect.centery = self.posy
buttonrect.centerx = self.posx; buttonrect.centery = self.posy
elif N == 2:
phrasexrect.left = self.posx + mf(buttonrect.width-phrasexrect.width-((buttonrect.width-phrasexrect.width)/2)); phrasexrect.centery = self.posy
buttonrect.left = self.posx; buttonrect.centery = self.posy
pygame.draw.rect(windowsurface, (120, 100, 12), buttonrect)
windowsurface.blit(phrasex, phrasexrect)
return buttonrect
class PABM():
list_of_buttons = []
WHITE = (255, 255, 255)
GREEN = (22, 255, 22)
CYAN = (22, 255, 255)
RED = (255, 22, 22)
GREY1 = (40, 40, 40)
GREY2 = (20, 20, 20)
BLACK = (0, 0, 0)
#num = 0
list_image = {}
list_image_rect = {}
imagetext = []
def __init__(self, TEXT, posx=100, posy=100, C_or_TL='center',
fontsize=36, fixedtoright=False, fixedtobottom=False,
fontcolor=WHITE, proportionalbuttonshape=False, completecoverage=False, buttonwidthx=0, buttonheighty=0,
proportionalbuttonlocationx=False, proportionalbuttonlocationy=False, useimage='F',
useborder=1, blitted=False, buttonstate=1, pressed=False, keyboardmode=False, specificimage=''):
PABM.list_of_buttons.append(self)
self.TEXT = TEXT; self.C_or_TL = C_or_TL
self.posx = posx; self.posy = posy
self.buttonwidthx = buttonwidthx; self.buttonheighty = buttonheighty
self.posx_original = posx; self.posy_original = posy; self.keyboardmode = keyboardmode
self.originalscreenwidth = dimension1.return_swidth(); self.originalscreenheight = dimension1.return_sheight()
self.fixedtoright = fixedtoright; self.fixedtobottom = fixedtobottom
#translate xxxxx_True to True, xxxxx_False to False
#takes in fontsize, creates phrase_rect
self.fontsize = fontsize
self.fontcolor = fontcolor
self.fontsize_original = fontsize
self.fontstyle = pygame.font.SysFont('calibri', self.fontsize)
try:
self.phrase = self.fontstyle.render(self.TEXT, 1, self.fontcolor)
except:
global inputa, storedtext1
self.phrase = self.fontstyle.render(None, 1, self.fontcolor)
inputa = storedtext1 = ''
self.phrase_rect = self.phrase.get_rect()
#-----
self.buttoncolor1 = PABM.GREY1; self.buttoncolor2 = PABM.GREY2
self.useimage = useimage; self.buttonstate = 1
self.blitted = blitted
self.pressed = pressed
#get variable keep button location proportional in regards to any game screen size/ratio change
if proportionalbuttonlocationx == True:
self.proportionalbuttonlocationx = True
self.proportionalbuttonlocationx_per = self.posx/dimension1.return_swidth() #ex: .10 or 10%
else:
self.proportionalbuttonlocationx = False
if proportionalbuttonlocationy == True:
self.proportionalbuttonlocationy = True
self.proportionalbuttonlocationy_per = self.posy/dimension1.return_sheight()
else:
self.proportionalbuttonlocationy = False
##creates button_rect from scratch, then copy phrase_rect's features (prevents being changed by phrase_rect)
self.button_rect = pygame.Rect(0,0,10,10)
self.completecoverage = completecoverage
if self.completecoverage == True:
self.button_rect.width = mf(self.phrase_rect.width + buttonwidthx)
self.button_rect.height = mf(self.phrase_rect.height + buttonheighty)
#elif self.completecoverage == False: #make it temporary size
# self.button_rect.width = mf(self.phrase_rect.width)
# self.button_rect.height = mf(self.phrase_rect.height)
elif self.completecoverage == False:
self.button_rect.width = mf(self.buttonwidthx)
self.button_rect.height = mf(self.buttonheighty)
# self.button_rect.width = mf(self.phrase_rect.width + buttonwidthx)
# self.button_rect.height = mf(self.phrase_rect.height + buttonheighty)
self.button_rect_width_original = self.button_rect.width
self.button_rect_height_original = self.button_rect.height
#get variable to keep button shape proportional in regards to any game screen size/ratio change
if proportionalbuttonshape == True:
self.proportionalbuttonshape = True
self.lockin_button_width_per = self.button_rect.width/dimension1.return_swidth() #ex: .10 or 10%
self.lockin_button_height_per = self.button_rect.height/dimension1.return_sheight()
else:
self.proportionalbuttonshape = False
#images for button state
if self.useimage == 'T':
self.imagebutton = pygame.image.load(r"C:\Users\patri\PycharmProjects\untitled\6\images_python_training\imagefu_button3.png")
self.imagebutton = pygame.transform.scale(self.imagebutton, (self.button_rect.width, self.button_rect.height))
self.imagebutton_original1 = self.imagebutton
self.imagebutton = pygame.image.load(r"C:\Users\patri\PycharmProjects\untitled\6\images_python_training\imagefu_button7.png")
self.imagebutton = pygame.transform.scale(self.imagebutton, (self.button_rect.width, self.button_rect.height))
self.imagebutton_original2 = self.imagebutton
# PABM.list_image.setdefault(len(PABM.list_image), self.imagebutton)
# self.imagebutton_rect = self.imagebutton.get_rect()
# PABM.list_image_rect.setdefault(len(PABM.list_image_rect), self.imagebutton_rect)
#border
self.useborder = useborder #1,2,3
def blit_phraseandbutton(self, NEWTEXT=''):
if NEWTEXT != '':
self.TEXT = str(NEWTEXT)
self.fontstyle = pygame.font.SysFont('calibri', self.fontsize)
try:
self.phrase = self.fontstyle.render(self.TEXT, 1, self.fontcolor)
except:
global inputa, storedtext1
self.phrase = self.fontstyle.render(None, 1, self.fontcolor)
inputa = storedtext1 = ''
self.phrase_rect = self.phrase.get_rect()
#for looping on certain buttons (in this case, loop on only buttons that have been blitted)
self.blitted = True
if self.keyboardmode == False:
self.posx = self.posx_original; self.posy = self.posy_original
else:
self.proportionalbuttonshape = False; self.proportionalbuttonlocationx = False; self.proportionalbuttonlocationy= False
#if self.completecoverage == True:
#self.button_rect.width = mf(self.phrase_rect.width + self.buttonwidthx)
# self.button_rect.height = mf(self.phrase_rect.height + self.buttonheighty)
#elif self.completecoverage == False:
#self.button_rect.width = mf(self.buttonwidthx)
#self.button_rect.height = mf(self.buttonheighty)
#updates x,y,w,h of button and phrase if game screen size changes
#updates width and height of button
if self.proportionalbuttonshape == True:
self.button_rect.width = mf(self.lockin_button_width_per * dimension1.return_swidth())
self.button_rect.height = mf(self.lockin_button_height_per * dimension1.return_sheight()) # * 1.65
elif self.proportionalbuttonshape == False:
pass #or write out the codes for width and height customization while game loop is running?
#updates center/topleft x,y of button
#try adding "if button on right side, keep button same distance toward right border"
if self.proportionalbuttonlocationx == True:
if self.C_or_TL == 'center':
self.button_rect.centerx = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.button_rect.left = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topright':
self.button_rect.right = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.proportionalbuttonlocationx == False:
if self.fixedtoright == True:
if self.C_or_TL == 'center':
#self.button_rect.centerx = (dimension1.return_swidth() - mf(self.posx)) #ex: 100
self.button_rect.centerx = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx)) #ex: 700
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.button_rect.left = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
elif self.C_or_TL == 'topright':
self.button_rect.right = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
else:
if self.C_or_TL == 'center':
self.button_rect.centerx = mf(self.posx)
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.button_rect.left = mf(self.posx)
elif self.C_or_TL == 'topright':
self.button_rect.right = mf(self.posx)
#
if self.proportionalbuttonlocationy == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.button_rect.centery = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topleft':
self.button_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topright':
self.button_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.proportionalbuttonlocationy == False:
if self.fixedtobottom == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.button_rect.centery = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topleft':
self.button_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topright':
self.button_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
else:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.button_rect.centery = mf(self.posy)
elif self.C_or_TL == 'topleft':
self.button_rect.top = mf(self.posy)
elif self.C_or_TL == 'topright':
self.button_rect.top = mf(self.posy)
#updates center/topleft x,y of phrase
#okay to use proportionalbuttonlocationxy on phrase_rect
if self.proportionalbuttonlocationx == True:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth()) + mf((self.button_rect.width-self.phrase_rect.width)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.right = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth()) - mf((self.button_rect.width-self.phrase_rect.width)/2)
elif self.proportionalbuttonlocationx == False:
if self.fixedtoright == True:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = mf(dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))) + mf((self.button_rect.width-self.phrase_rect.width)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.right = mf(dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))) - mf((self.button_rect.width-self.phrase_rect.width)/2)
else:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = mf(self.posx)
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = mf(self.posx) + mf((self.button_rect.width-self.phrase_rect.width)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.right = mf(self.posx) - mf((self.button_rect.width-self.phrase_rect.width)/2)
if self.proportionalbuttonlocationy == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight()) + mf((self.button_rect.height-self.phrase_rect.height)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight()) + mf((self.button_rect.height-self.phrase_rect.height)/2)
elif self.proportionalbuttonlocationy == False:
if self.fixedtobottom == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = mf(dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))) + mf((self.button_rect.height-self.phrase_rect.height)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.top = mf(dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))) + mf((self.button_rect.height-self.phrase_rect.height)/2)
else:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = mf(self.posy)
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = mf(self.posy) + mf((self.button_rect.height-self.phrase_rect.height)/2)
elif self.C_or_TL == 'topright':
self.phrase_rect.top = mf(self.posy) + mf((self.button_rect.height-self.phrase_rect.height)/2)
if self.useimage == 'T':
#reshape image surface if needed
#-------------------------------
if self.buttonstate == 1:
if self.useborder == 1:
self.imagebutton = self.imagebutton_original1
elif self.useborder == 2:
self.imagebutton = self.imagebutton_original1
pygame.draw.rect(windowsurface, (120,20,20), self.button_rect, 1)
elif self.useborder == 3:
pygame.draw.rect(windowsurface, (120,20,20), self.button_rect, 1)
elif self.buttonstate == 2:
if self.useborder == 1:
self.imagebutton = self.imagebutton_original2
elif self.useborder == 2:
self.imagebutton = self.imagebutton_original2
pygame.draw.rect(windowsurface, (20,120,20), self.button_rect, 1)
elif self.useborder == 3:
pygame.draw.rect(windowsurface, (20,120,20), self.button_rect, 1)
#-------------------------------
if self.useimage == 'T':
self.imagebutton = pygame.transform.scale(self.imagebutton, (self.button_rect.width, self.button_rect.height))
#reshape image rect if needed
self.imagebutton_rect = self.imagebutton.get_rect()
self.imagebutton_rect.width = self.button_rect.width
self.imagebutton_rect.height = self.button_rect.height
self.imagebutton_rect.centerx = self.button_rect.centerx
self.imagebutton_rect.centery = self.button_rect.centery
if self.useborder != 3:
windowsurface.blit(self.imagebutton, self.imagebutton_rect)
windowsurface.blit(self.phrase, self.phrase_rect)
return self.button_rect
else:
windowsurface.blit(self.phrase, self.phrase_rect)
return self.button_rect
elif self.useimage == 'F':
if self.buttonwidthx == 0 and self.buttonheighty == 0: # temporary solution
windowsurface.blit(self.phrase, self.phrase_rect)
return self.button_rect
else: #used to be without if and else, unindented
#draw and blit, returns button_rect to work with collidepoint in a game loop
#-------------------------------
if self.buttonstate == 1:
if self.useborder == 1: #none
pygame.draw.rect(windowsurface, self.buttoncolor1, self.button_rect)
elif self.useborder == 2:
pygame.draw.rect(windowsurface, self.buttoncolor1, self.button_rect)
pygame.draw.rect(windowsurface, (120,20,20), self.button_rect, 1)
elif self.useborder == 3:
pygame.draw.rect(windowsurface, (120,20,20), self.button_rect, 1)
elif self.buttonstate == 2:
if self.useborder == 1: #none
pygame.draw.rect(windowsurface, self.buttoncolor2, self.button_rect)
elif self.useborder == 2:
pygame.draw.rect(windowsurface, self.buttoncolor2, self.button_rect)
pygame.draw.rect(windowsurface, (20,120,20), self.button_rect, 1)
elif self.useborder == 3:
pygame.draw.rect(windowsurface, (20,120,20), self.button_rect, 1)
#-------------------------------
windowsurface.blit(self.phrase, self.phrase_rect)
return self.button_rect
#must create (okay without "text") once, then blit with desired text
def blit_phrase_only(self, NEWTEXT=''):
if NEWTEXT != '':
self.TEXT = str(NEWTEXT)
fontstyle = pygame.font.SysFont('calibri', self.fontsize)
try:
self.phrase = fontstyle.render(self.TEXT, 1, self.fontcolor)
except:
global inputa, storedtext1
self.phrase = fontstyle.render(None, 1, self.fontcolor)
inputa = storedtext1 = ''
self.phrase_rect = self.phrase.get_rect()
if self.proportionalbuttonlocationx == True:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topright':
self.phrase_rect.right = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.proportionalbuttonlocationx == False:
if self.fixedtoright == True:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx)) #ex: 700
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
elif self.C_or_TL == 'topright':
self.phrase_rect.right = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
else:
if self.C_or_TL == 'center':
self.phrase_rect.centerx = mf(self.posx)
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.phrase_rect.left = mf(self.posx)
elif self.C_or_TL == 'topright':
self.phrase_rect.right = mf(self.posx)
if self.proportionalbuttonlocationy == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topright':
self.phrase_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.proportionalbuttonlocationy == False:
if self.fixedtobottom == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topright':
self.phrase_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
else:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.phrase_rect.centery = mf(self.posy)
elif self.C_or_TL == 'topleft':
self.phrase_rect.top = mf(self.posy)
elif self.C_or_TL == 'topright':
self.phrase_rect.top = mf(self.posy)
windowsurface.blit(self.phrase, self.phrase_rect)
return self.phrase_rect #could this be better than self.phrasexrect (phrasexrect seems to just stick there) - wrong. actually you just keep self.blitted = False or don't add self.blitted. self.blitted=True seems to prevent the button from getting unblitted/deleted away
#draw and blit, returns button_rect to work with collidepoint in a game loop
#try using it under video resize codes
def create_list_image_and_list_image_rect(self):
self.list_image = {}
self.list_image_rect = {}
#must take in - consider adding more features:
#
# buttonshape,
# useimage='T',
# useborder=(1|2|3), blitted=False, buttonstate=(1|2), pressed=False, keyboardmode=False)
for a in PABM.imagetext: #creates a list
self.an_image = pygame.image.load(a)
if self.proportionalbuttonshape == True:
self.an_image = pygame.transform.scale(self.an_image, (mf(self.lockin_button_width_per * dimension1.return_swidth()), mf(self.lockin_button_height_per * dimension1.return_sheight())))
elif self.proportionalbuttonshape == False:
self.an_image = pygame.transform.scale(self.an_image, (self.buttonwidthx, self.buttonheighty))
self.an_image = self.an_image.convert_alpha()
self.list_image.setdefault(len(self.list_image), self.an_image)
self.an_image_rect = self.an_image.get_rect()
if self.proportionalbuttonlocationx == True:
if self.C_or_TL == 'center':
self.an_image_rect.centerx = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.an_image_rect.left = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.C_or_TL == 'topright':
self.an_image_rect.right = mf(self.proportionalbuttonlocationx_per * dimension1.return_swidth())
elif self.proportionalbuttonlocationx == False:
if self.fixedtoright == True:
if self.C_or_TL == 'center':
self.an_image_rect.centerx = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx)) #ex: 700
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.an_image_rect.left = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
elif self.C_or_TL == 'topright':
self.an_image_rect.right = dimension1.return_swidth() - (self.originalscreenwidth - mf(self.posx))
else:
if self.C_or_TL == 'center':
self.an_image_rect.centerx = mf(self.posx)
elif self.C_or_TL == 'topleft' or self.C_or_TL == 'left':
self.an_image_rect.left = mf(self.posx)
elif self.C_or_TL == 'topright':
self.an_image_rect.right = mf(self.posx)
if self.proportionalbuttonlocationy == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.an_image_rect.centery = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topleft':
self.an_image_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.C_or_TL == 'topright':
self.an_image_rect.top = mf(self.proportionalbuttonlocationy_per * dimension1.return_sheight())
elif self.proportionalbuttonlocationy == False:
if self.fixedtobottom == True:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.an_image_rect.centery = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topleft':
self.an_image_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
elif self.C_or_TL == 'topright':
self.an_image_rect.top = dimension1.return_sheight() - (self.originalscreenheight - mf(self.posy))
else:
if self.C_or_TL == 'center' or self.C_or_TL == 'left':
self.an_image_rect.centery = mf(self.posy)
elif self.C_or_TL == 'topleft':
self.an_image_rect.top = mf(self.posy)
elif self.C_or_TL == 'topright':
self.an_image_rect.top = mf(self.posy)
self.list_image_rect.setdefault(len(self.list_image_rect), self.an_image_rect)
def return_list_image(self):
return self.list_image
def return_list_image_rect(self):
return self.list_image_rect
@staticmethod
def loop_hoveringoverbuttons():
for a in PABM.list_of_buttons:
if a.blitted == True: #ONLY for buttons that are blitted
if a.blit_phraseandbutton().collidepoint(pygame.mouse.get_pos()):
a.buttonstate = 2
else:
if a.pressed == False:
a.buttonstate = 1
@staticmethod
def loop_unblitandunpressall():
for a in PABM.list_of_buttons:
a.pressed = False
a.blitted = False
@staticmethod
def loop_pressthatbutton():
for X in PABM.list_of_buttons:
if X.blitted == True:
if X.blit_phraseandbutton().collidepoint(a.pos):
X.pressed = True
X.buttonstate = 2
#print('pressed = True, buttonstate = 2')
#print(X.TEXT, "X.TEXT")
return X.TEXT
@staticmethod
def loop_unpressthatbutton():
for X in PABM.list_of_buttons:
if X.blitted == True:
if X.blit_phraseandbutton().collidepoint(a.pos):
X.pressed = False
X.buttonstate = 1
#print('pressed = False, buttonstate = 1')
qwerty = [a for a in '1234567890qwertyuiopasdfghjklzxcvbnm']
def keyboardmaker():
#keyboard maker
qwerty = [a for a in '1234567890qwertyuiopasdfghjklzxcvbnm']
placex = 0 # set to 0 and will auto-center on any game screen width size
placey = mf(dimension1.return_sheight() * .68) # starting y point from top of game screen to bottom of game screen
buttonwidth = 48#mf(dimension1.return_swidth() / 15)
buttonheight = 48#mf(dimension1.return_sheight() / 10)
placex = buttonwidth/2
gapx = 1.1
gapy = 1.1
# find row_1 width for row 1 centering - need code simplification here@@@@
row_1_buttons_width = 0
for a in qwerty[:qwerty.index('q')]:
row_1_buttons_width += buttonwidth + ((buttonwidth * gapx) - buttonwidth)
row_1_buttons_width -= ((buttonwidth * gapx) - buttonwidth)
row_1_placex = (dimension1.return_swidth() - mf(row_1_buttons_width)) / 2
#print(row_1_placex, "row_1_placex")
# find row_2 width for row 2 centering
row_2_buttons_width = 0
for a in qwerty[qwerty.index('q'):qwerty.index('a')]:
row_2_buttons_width += buttonwidth + ((buttonwidth * gapx) - buttonwidth)
row_2_buttons_width -= ((buttonwidth * gapx) - buttonwidth)
row_2_placex = (dimension1.return_swidth() - mf(row_2_buttons_width)) / 2
#print(row_2_placex, "row_2_placex")
# find row_3 width for row 3 centering
row_3_buttons_width = 0
for a in qwerty[qwerty.index('a'):qwerty.index('z')]:
row_3_buttons_width += buttonwidth + ((buttonwidth * gapx) - buttonwidth)
row_3_buttons_width -= ((buttonwidth * gapx) - buttonwidth)
row_3_placex = (dimension1.return_swidth() - mf(row_3_buttons_width)) / 2
#print(row_3_placex, "row_3_placex")
# find row_4 width for row 4 centering
row_4_buttons_width = 0
for a in qwerty[qwerty.index('z'):]:
row_4_buttons_width += buttonwidth + ((buttonwidth * gapx) - buttonwidth)
row_4_buttons_width -= ((buttonwidth * gapx) - buttonwidth)
row_4_placex = (dimension1.return_swidth() - mf(row_4_buttons_width)) / 2
#print(row_4_placex, "row_4_placex")
#keyboard maker
placex += row_1_placex
for a in qwerty:
list_button.append({a: PABM(a, placex, placey, 'center',
30, True, True,
PABM.WHITE, False, False, buttonwidth, buttonheight,
False, False, 'T',
1, False, 1, False, True)})
placex += mf(buttonwidth * gapx)
if placex > dimension1.return_swidth() - (dimension1.return_swidth()*.1) or a == '0' or a == 'p' or a == 'l':
placey += buttonheight * gapy
placex = buttonwidth/2
if a == '0':
placex += row_2_placex
elif a == 'p':
placex += row_3_placex
elif a == 'l':
placex += row_4_placex # prob
def checkletter(inputa): #not re-usable
print()
#input added/inputamulti
global missedtotal, missed, num, hidden_splitted
inputamulti = [a for a in inputa]
if ' ' in hidden_splitted:
for a in inputamulti:
if ' ' == a:
inputamulti.remove(a)
for a in inputamulti:
if a in usedletters:
inputamulti.remove(a)
inputamulti = list({a for a in inputamulti})
#print('inputamulti:',inputamulti)
print('input added: '.ljust(20, " "), sorted(inputamulti))
#correct letters/usedletters
num += len(missed)
missed = []
for n in inputamulti:
for n in [n.lower(), n.upper()]:
if n in answer_splitted0:
usedletters.append(n)
for a in answer_splitted:
while n in answer_splitted:
answer_bank.append(n)
answer_splitted.remove(n)
while answer_bank != []:
for b in answer_bank:
for bb in answer_splitted0:
if bb == b:
hidden_splitted[answer_splitted0.index(bb)] = str(bb)
answer_splitted0[answer_splitted0.index(bb)] = '*'
answer_bank.remove(b)
break
for a in usedletters:
if ' ' in a or '\n' in a:
usedletters.remove(a)
#print('usedletters: ',usedletters)
print('correct letters: '.ljust(20, " "), sorted(usedletters))
#incorrect letters/missedtotal
for n in inputamulti:
if n not in answer_splitted or n.upper() not in answer_splitted0_upper:
if n not in answer_splitted and n.upper() not in answer_splitted0_upper and n != ' ' and n != '\n':
if n not in missedtotal:
missedtotal.append(n)
missed.append(n)
missedtotal = list({a for a in missedtotal})
#print('missedtotal: ',missedtotal)
print('incorrect letters: '.ljust(20, " "), sorted(missedtotal))
def turn_all_game_loops_to_false():
global loop_main_menu, loop_setup, loop_play, loop_gameover, loop_setting
loop_main_menu = False
loop_setup = False
loop_play = False
loop_gameover = False
loop_setting = False
def save():
textfile3 = open(r"Hangman Resources\textfile_active.txt", 'w')
for a in A1.return_var():
textfile3.write(a)
textfile3.write('\n')
textfile3.close()
textfile4 = open(r"Hangman Resources\textfile_inactive.txt", 'w')
for a in A2.return_var():
textfile4.write(a)
textfile4.write('\n')
textfile4.close()
######################################################
# get a list (of answer keywords) from textfile2
textfile3 = open(r"Hangman Resources\textfile_active.txt", 'r')
listy0 = []
for a in textfile3.readlines():
if '\n' in a:
listy0.append(a[:-1])
elif '\n' not in a:
listy0.append(a)
textfile3.close()
# centralized variable init
#print(listy0)
A1 = CentralizedVariable(listy0)
######################################################
textfile4 = open(r"Hangman Resources\textfile_inactive.txt", 'r')
listy2 = []
for a in textfile4.readlines():
if '\n' in a:
listy2.append(a[:-1])
elif '\n' not in a:
listy2.append(a)
textfile4.close()
# centralized variable init (stores deleted text)
#print(listy2)
A2 = CentralizedVariable(listy2)
######################################################
maxround = CentralizedVariable(4)
maxattempt = CentralizedVariable(7)
######################################################
dimension1 = Pygamedisplay(800, 600)
windowsurface = pygame.display.set_mode((dimension1.return_swidth(), dimension1.return_sheight()),pygame.RESIZABLE)
midwidth = mf(dimension1.return_swidth())
midheight = mf(dimension1.return_sheight())
######################################################
button_title = PABM('Hangman', midwidth/2, midheight*.3, 'center',
80, False, False,
PABM.WHITE, False, False, 0, 0,
True, True, 'T',
1, False, 1, False)
button_bypatrickdlr = PABM('by Patrick DLR', midwidth/2, midheight*.4, 'center',
24, False, False,
PABM.WHITE, False, False, 0, 0,
True, True, 'T',
1, False, 1, False)
button_play = PABM('Play', midwidth/2, midheight*.6, 'center',
30, False, False,
PABM.WHITE, False, False, 250, 50,
True, True, 'T',
1, False, 1, False)
button_resume = PABM('Resume', midwidth/2, midheight*.6, 'center',
30, False, False,
PABM.WHITE, False, False, 250, 50,
True, True, 'T',
1, False, 1, False)
button_setting = PABM('Setting', midwidth/2, midheight*.7, 'center',
30, False, False,
PABM.WHITE, False, False, 250, 50,
True, True, 'T',
1, False, 1, False)
button_quit = PABM('Quit', 10, 383, 'topleft',
30, False, True,
PABM.WHITE, False, False, 100, 50,
False, False, 'F',
1, False, 1, False)
button_quit2 = PABM('Main Menu', 10, 10, 'topleft',
30, False, False,
PABM.WHITE, False, False, 160, 50,
False, False, 'F',
1, False, 1, False)
button_quit3 = PABM('Main Menu', midwidth/2, midheight*.7, 'center',
30, False, False,
PABM.WHITE, False, False, 250, 50,
True, True, 'T',
1, False, 1, False)
button_pause = PABM('Pause', midwidth-10, 383, 'topright',
30, True, True,
PABM.WHITE, False, False, 100, 50,
False, False, 'F',
1, False, 1, False)
button_back = PABM('Back', 10, 10, 'topleft',
30, False, False,
PABM.WHITE, False, False, 100, 50,
False, False, 'F',
1, False, 1, False)
####
button_save = PABM('Save', 10, 70, 'topleft',
30, False, False,
PABM.WHITE, False, False, 100, 50,
False, False, 'F',
1, False, 1, False)
button_saved = PABM('Saved!', 135, 70, 'topleft',
30, False, False,
PABM.GREEN, False, False, 100, 50,
False, False, 'F',
3, False, 1, False)
phrase_roundspergame = PABM('Rounds per game', midwidth*.15, midheight*.4, 'left',
36, False, False,
PABM.WHITE, False, True, 0, 0,
True, True, 'F',
1, False, 1, False)
phrase_roundspergame_X = PABM(str(maxround.return_var()), midwidth*.6, midheight*.4, 'center',
36, False, False,
PABM.WHITE, False, False, 0, 0,
True, True, 'F',
1, False, 1, False)
phrase_attemptsperround = PABM('Attempts per round', midwidth*.15, midheight*.5, 'left',
36, False, False,
PABM.WHITE, False, True, 0, 0,
True, True, 'F',
1, False, 1, False)
phrase_attemptsperround_X = PABM(str(maxattempt.return_var()), midwidth*.6, midheight*.5, 'center',
36, False, False,
PABM.WHITE, False, False, 0, 0,
True, True, 'F',
1, False, 1, False)
##issue
phrase_listofwords = PABM('List of words', midwidth*.15, midheight*.6, 'left',
36, False, False,
PABM.WHITE, False, True, 0, 0,
True, True, 'F',
1, False, 1, False)
phrase_listofwords_X = PABM(str(len(A1.return_var())), midwidth*.6, midheight*.6, 'center',
36, False, False,
PABM.WHITE, False, False, 0, 0,
True, True, 'F',
1, False, 1, False)
button_up1 = PABM('↑', midwidth*.7, midheight*.4, 'center',
36, False, False,
PABM.WHITE, False, False, 48, 48,
True, True, 'T',
1, False, 1, False)
button_down1 = PABM('↓', midwidth*.8, midheight*.4, 'center',
36, False, False,
PABM.WHITE, False, False, 48, 48,
True, True, 'T',
1, False, 1, False)
button_up2 = PABM('↑', midwidth*.7, midheight*.5, 'center',
36, False, False,
PABM.WHITE, False, False, 48, 48,
True, True, 'T',
1, False, 1, False)
button_down2 = PABM('↓', midwidth*.8, midheight*.5, 'center',
36, False, False,
PABM.WHITE, False, False, 48, 48,
True, True, 'T',
1, False, 1, False)
####
button_expand = PABM('Expand', midwidth*.75, midheight*.6, 'center',
36, False, False,