-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreens.py
361 lines (305 loc) · 20.6 KB
/
screens.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
import pygame
import ui
import game
import time
from storage import save_data, load_data, add_custom_words, del_custom_words, get_custom_words
from wordanalyzer import WordDifficulty
word_difficulty = WordDifficulty()
class Screens:
def __init__(self, screen):
self.screen = screen
self.center_x = ui.SCREEN_WIDTH // 2
self.center_y = ui.SCREEN_HEIGHT // 2
self.bottom_y = ui.SCREEN_HEIGHT
self.right_x = ui.SCREEN_WIDTH
self.running = True
self.new_game = True
self.mode_select = False
self.start_game = False
self.quit_screen = False
self.replay_menu = False
self.ensurance = False
self.display_rules = False
self.themes = False
self.custom_words = False
self.player_won = False
self.confirmed_words = False
self.button_font = pygame.font.Font(None, 30)
self.input_font = pygame.font.Font(None, 30)
self.guess_font = pygame.font.Font(None, 50)
self.lives_font = pygame.font.Font(None, 50)
self.title_font = pygame.font.Font(None, 120)
self.chat_font = pygame.font.Font(None, 25)
self.stats_width = self.right_x / 4
self.stats_height = self.bottom_y / 3
self.rules_width = self.right_x / 4
self.rules_height = self.bottom_y / 2
self.custom_words_list = []
added_words = get_custom_words()
if added_words:
self.confirmed_words = True
for word in added_words:
self.custom_words_list.append(word)
self.words_entered_dict = {}
self.custom_word_input_text = ""
self.file_path = "words/words.txt"
self.input_box_rect = pygame.Rect((self.center_x - 300), (self.bottom_y * (5/6)), 100, 30)
self.title_rect = pygame.Rect((self.center_x) - (600 // 2), (self.bottom_y / 50), 600, 130)
self.game_title_rect = pygame.Rect((self.center_x) - (400 // 2), (self.bottom_y / 50), 400, 130)
self.menu_rect = pygame.Rect(20, 20, 100, 100)
self.quit_but_rect = pygame.Rect(self.right_x - 120, 20, 100, 100)
self.text_box_rect = pygame.Rect(self.center_x - (420 // 2), self.bottom_y - 50, 400, 50)
self.lives_box_rect = pygame.Rect(self.right_x - 180, 100, 170, 70)
self.incorrect_box_rect = pygame.Rect(self.right_x - 280, self.bottom_y - 50, 170, 40)
self.rules_button_rect = pygame.Rect(self.center_x - (150 // 2), self.center_y + 40, 150, 50)
self.button_rect = pygame.Rect(self.center_x - (150 // 2), self.center_y - 20, 150, 50)
self.theme_button_rect = pygame.Rect(self.center_x - (150 // 2), self.center_y + 100, 150, 50)
self.custom_words_button_rect = pygame.Rect(self.center_x - (150 // 2), self.center_y + 160, 150, 50)
self.welc_title_rect = pygame.Rect((self.center_x) - (1000 // 2), (self.bottom_y / 50), 1000, 130)
self.stats_box_rect = pygame.Rect(100, (self.bottom_y / 2) - (self.stats_height / 4), self.stats_width, self.stats_height)
self.stats_reset_button_rect = pygame.Rect(100 + (self.right_x / 13), self.bottom_y - (self.stats_height), 130, 30)
self.quit_rect = pygame.Rect((self.center_x) - (600 // 2), (self.bottom_y / 100), 600, 130)
self.button_rect_yes = pygame.Rect(self.center_x - 120, self.center_y - (50 // 2), 100, 50)
self.button_rect_no = pygame.Rect(self.center_x + 20, self.center_y - (50 // 2), 100, 50)
self.dir_box_rect = pygame.Rect(self.right_x - (self.rules_width*1.25), (self.bottom_y / 2) - (self.stats_height / 4), self.rules_width, self.rules_height)
self.howto_box_rect = pygame.Rect(self.right_x / 2 - (self.rules_width/2), (self.bottom_y / 2) - (self.stats_height / 4), self.rules_width, self.rules_height)
self.rules_box_rect = pygame.Rect(self.right_x / 16, (self.bottom_y / 2) - (self.stats_height / 4), self.rules_width, self.rules_height)
self.rect_easy = pygame.Rect((self.center_x - (150*2)), (self.bottom_y * (7/12)), 150, 50)
self.rect_med = pygame.Rect((self.center_x) - (150//2), (self.bottom_y * (7/12)), 150, 50)
self.rect_hard = pygame.Rect((self.center_x + 150), (self.bottom_y * (7/12)), 150, 50)
self.top_rect = pygame.Rect((self.center_x) - (350 // 2), (self.bottom_y // 4), 350, 50)
self.replay_rect = pygame.Rect(self.center_x - (500 // 2), self.center_y - 200, 500, 50)
self.button_rect_replay = pygame.Rect(self.center_x - 120, self.center_y - 100, 100, 50)
self.button_rect_not = pygame.Rect(self.center_x + 20, self.center_y - 100, 100, 50)
self.sure_rect = pygame.Rect((self.center_x) - (650 // 2), (self.bottom_y / 100), 650, 130)
self.button_rect_cont = pygame.Rect(self.center_x - 120, self.center_y - (50 // 2), 100, 50)
self.button_rect_dont = pygame.Rect(self.center_x + 20, self.center_y - (50 // 2), 100, 50)
self.words_entered_box_rect = pygame.Rect(100, (self.bottom_y / 2) - (self.stats_height / 4), self.stats_width, self.stats_height)
self.words_entered_reset_button_rect = pygame.Rect(100 + (self.right_x / 13), self.bottom_y - (self.stats_height) + 20, 130, 30)
self.words_confirm_rect = pygame.Rect(self.center_x - (150 // 2), self.center_y + 100, 150, 50)
def homeScreen(self, user_name, name_input_text):
# Draw Start Screen Title
ui.draw_text_box(self.screen, self.welc_title_rect, "Welcome to Hangman!", self.title_font)
# Draw Quit Button
button_hover_quit = self.quit_but_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.quit_but_rect, "Quit", self.lives_font, button_hover_quit)
# Draw Start Button
button_hover = self.button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.button_rect, "Start Game!", self.button_font, button_hover)
# Draw Rules Button
button_hover_rules = self.rules_button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.rules_button_rect, "Rules", self.button_font, button_hover_rules)
# Draw Theme Button
button_hover_theme = self.theme_button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.theme_button_rect, "Theme Select", self.button_font, button_hover_theme)
# Draw Custom Words Button
button_hover_custom_words = self.custom_words_button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.custom_words_button_rect, "Custom Words", self.button_font, button_hover_custom_words)
# Draw User Name Entry Box
name_box_width = self.right_x / 4
name_box_height = self.bottom_y / 3
name_input_box_rect = pygame.Rect(self.right_x - (name_box_width + 100) + (self.right_x / 13), (self.bottom_y*2/3 + 20) - (name_box_height / 4), 150, 30)
name_box_rect = pygame.Rect(self.right_x - (name_box_width + 100), (self.bottom_y / 2) - (name_box_height / 4), name_box_width, name_box_height)
name_box_list = ["", "Current User Name is: ", f"{user_name}", "", "Enter a User Name Below"]
ui.draw_multiline_text_box(self.screen, name_box_rect, "User Name", name_box_list, self.lives_font, self.chat_font)
ui.draw_input_box(self.screen, name_input_box_rect, name_input_text, self.input_font)
# Draw Player Stats Box
player_data = load_data()
high_score = 0
high_streak = 0
if player_data:
for entry in player_data:
if entry["user"] == user_name:
high_score = entry["score"]
high_streak = entry["word-streak"]
stats_list = ["", f"High Score: {high_score}", "", f"Most Words: {high_streak}"]
ui.draw_multiline_text_box(self.screen, self.stats_box_rect, "Player Stats", stats_list, self.lives_font, self.lives_font)
# Draw Stats Reset Button
button_hover_stats_reset = self.stats_reset_button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.stats_reset_button_rect, "Reset", self.button_font, button_hover_stats_reset)
def quitScreen(self, event, user_name, word_streak):
ui.draw_text_box(self.screen, self.quit_rect, "Are you sure you want to quit?", self.lives_font)
button_hover_yes = self.button_rect_yes.collidepoint(pygame.mouse.get_pos())
button_hover_no = self.button_rect_no.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.button_rect_yes, "YES", self.lives_font, button_hover_yes)
ui.draw_button(self.screen, self.button_rect_no, "NO", self.lives_font, button_hover_no)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.button_rect_yes.collidepoint(event.pos):
save_data(user_name, game.score, word_streak)
self.running = False
return
elif self.button_rect_no.collidepoint(event.pos):
self.quit_screen = False
return
def rulesScreen(self):
# Draw Start Screen Title
ui.draw_text_box(self.screen, self.welc_title_rect, "Welcome to Hangman!", self.title_font)
# Draw menu button
button_hover_menu = self.menu_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.menu_rect, "Menu", self.lives_font, button_hover_menu)
# Rules
rules_list = ["You must guess the word correctly", "in order to earn a score",
"Your score will increment for each", "word you guess correctly",
"If you fail to correctly guess the", "word then your score resets",
"If you also exit the game your score", "will be reset when you return",
"However, the game will save your", "highest score to your player stats",
"The game will also save the most", "amount of words you guess correctly",
"consecutively to your player stats too",
"You may add custom words to the game", "but you are limited to 8 added words"
]
ui.draw_multiline_text_box(self.screen, self.rules_box_rect, "Hangman Rules", rules_list, self.lives_font, self.chat_font)
# How to play
howto_list = ["Once in the main menu hit the start-", "game button",
"You will then choose your difficulty-", "Easy, Medium or Hard",
"You will then be prompted into the game",
"Here you must enter a single letter-", "at a time",
"You can see your letter currently-", "in the gray input box",
"If you wish to change it hit del-", "before you hit enter to not lose a life",
"In the bottom right you see the list-", "of incorrect guessed letters",
"Once you complete the word or fail-", "the game will end and give you a prompt"
]
ui.draw_multiline_text_box(self.screen, self.howto_box_rect, "How to Play", howto_list, self.lives_font, self.chat_font)
# Directions
dir_list = ["Easy", "You have 10 lives and 60 seconds", "to guess the word correctly",
"Your final score is your", "remaining guesses with no multiplier",
"Medium", "You have 7 lives and 45 seconds", "to guess the word correctly",
"Your final score is your", "remaining guesses times 2x",
"Hard", "You have 7 lives and 30 seconds", "to guess the word correctly",
"Your final score is your", "remaining guesses times 3x"
]
ui.draw_multiline_text_box(self.screen, self.dir_box_rect, "Game Directions", dir_list, self.lives_font, self.chat_font)
def themesScreen(self, event, images):
# Draw Screen Title
ui.draw_text_box(self.screen, self.welc_title_rect, "Theme Selection", self.title_font)
# Draw menu button
button_hover_menu = self.menu_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.menu_rect, "Menu", self.lives_font, button_hover_menu)
button_rects = {}
button_hovers = {}
h = ui.SCREEN_HEIGHT
# Sets the rect hit box for all themes
for i in range(8):
if i < 4:
button_rect_theme = pygame.Rect(self.center_x - 300, h/5 + ((i+1)*100), 200, 50)
else:
button_rect_theme = pygame.Rect(self.center_x + 100, h/5 + ((i-3)*100), 200, 50)
button_hover_theme = button_rect_theme.collidepoint(pygame.mouse.get_pos())
button_rects[i] = button_rect_theme
button_hovers[i] = button_hover_theme
# Draws a button for each theme
for i in range(8):
ui.draw_button(self.screen, button_rects[i], f"Theme {i+1}", self.lives_font, button_hovers[i])
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for key, rect in button_rects.items():
if rect.collidepoint(event.pos):
images.changeTheme(key)
def customWordsScreen(self, event):
# Draw Screen Title
ui.draw_text_box(self.screen, self.welc_title_rect, "Add Custom Words", self.title_font)
# Draw menu button
button_hover_menu = self.menu_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.menu_rect, "Menu", self.lives_font, button_hover_menu)
# Draw confirm button
button_hover_words_confirm = self.words_confirm_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.words_confirm_rect, "Confirm Words", self.button_font, button_hover_words_confirm)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
self.custom_word_input_text = self.custom_word_input_text[:-1]
elif event.key == pygame.K_RETURN:
# Process user input
if self.custom_word_input_text:
self.custom_word_input_text = self.custom_word_input_text.rstrip('\r') # Strip out carriage return character
self.custom_words_list.append(self.custom_word_input_text.lower())
self.custom_word_input_text = ""
else:
self.custom_word_input_text += event.unicode
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.words_entered_reset_button_rect.collidepoint(event.pos):
if self.confirmed_words:
del_custom_words(self.file_path, self.words_entered_dict)
self.confirmed_words = False
self.words_entered_dict = {}
self.custom_words_list = []
elif self.words_entered_dict and self.words_confirm_rect.collidepoint(event.pos):
self.confirmed_words = True
add_custom_words(self.file_path, self.words_entered_dict)
time.sleep(0.1)
self.custom_words = False
elif not self.words_entered_dict and self.words_confirm_rect.collidepoint(event.pos):
time.sleep(0.1)
self.custom_words = False
# Draw Custom Words Entry Box
custom_word_box_width = self.right_x / 4
custom_word_box_height = self.bottom_y / 3
custom_word_input_box_rect = pygame.Rect(self.right_x - (custom_word_box_width + 100) + (self.right_x / 17), (self.bottom_y*2/3 + 60) - (custom_word_box_height / 4), 200, 30)
custom_word_box_rect = pygame.Rect(self.right_x - (custom_word_box_width + 100), (self.bottom_y / 2) - (custom_word_box_height / 4), custom_word_box_width, custom_word_box_height)
custom_word_box_list = ["Only enter 1 word at a time (8 Max)", "Please enter your custom words below", "Once you have ensured the spelling is", "correct, hit enter and they will", "appear on the left", "Hit the reset button to remove them"]
ui.draw_multiline_text_box(self.screen, custom_word_box_rect, "Enter a Custom Word", custom_word_box_list, self.lives_font, self.chat_font)
ui.draw_input_box(self.screen, custom_word_input_box_rect, self.custom_word_input_text, self.input_font)
# Draw Custom Words Entered Box
if self.custom_words_list and len(self.custom_words_list) <= 8:
self.words_entered_dict = {}
for i in range(len(self.custom_words_list)):
diff = word_difficulty.evaluate_word_difficulty(self.custom_words_list[i])
self.words_entered_dict[self.custom_words_list[i]] = diff
ui.draw_multiline_text_box(self.screen, self.words_entered_box_rect, "Current Words Entered", self.words_entered_dict, self.lives_font, self.chat_font)
# Draw Stats Reset Button
button_hover_words_entered_reset = self.words_entered_reset_button_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.words_entered_reset_button_rect, "Reset", self.button_font, button_hover_words_entered_reset)
def replayScreen(self, event):
ui.draw_text_box(self.screen, self.replay_rect, "Do you wish to play again?", self.lives_font)
button_hover_replay = self.button_rect_replay.collidepoint(pygame.mouse.get_pos())
button_hover_not = self.button_rect_not.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.button_rect_replay, "YES", self.lives_font, button_hover_replay)
ui.draw_button(self.screen, self.button_rect_not, "NO", self.lives_font, button_hover_not)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.button_rect_not.collidepoint(event.pos): # No goto main menu
self.start_game = False
self.new_game = True
self.replay_menu = False
game.score = 0
elif self.button_rect_replay.collidepoint(event.pos): # Yes goto mode select
self.start_game = False
self.new_game = True
self.mode_select = True
self.replay_menu = False
def modeScreen(self):
# Draw title
ui.draw_text_box(self.screen, self.title_rect, "H A N G M A N", self.title_font)
ui.draw_text_box(self.screen, self.top_rect, "Choose a Difficulty", self.lives_font)
button_hover_easy = self.rect_easy.collidepoint(pygame.mouse.get_pos())
button_hover_med = self.rect_med.collidepoint(pygame.mouse.get_pos())
button_hover_hard = self.rect_hard.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.rect_easy, "Easy", self.button_font, button_hover_easy)
ui.draw_button(self.screen, self.rect_med, "Medium", self.button_font, button_hover_med)
ui.draw_button(self.screen, self.rect_hard, "Hard", self.button_font, button_hover_hard)
def gameScreen(self, lives, input_text, display, mode, current_theme):
# Call hangman function
game.draw_hangman(self.screen, lives, mode, current_theme)
# Draw Chat
ui.draw_chat(self.screen, ui.text_list, self.chat_font)
# Draw title
if not self.ensurance:
ui.draw_text_box(self.screen, self.game_title_rect, mode, self.title_font)
# Draw the text box for the word to guess
ui.draw_text_box(self.screen, self.text_box_rect, display, self.guess_font)
# Draw lives box
lives_string = f"Lives: {lives}"
ui.draw_text_box(self.screen, self.lives_box_rect, lives_string, self.lives_font)
# Draw input box
ui.draw_input_box(self.screen, self.input_box_rect, input_text, self.input_font)
# Draw menu button
button_hover_menu = self.menu_rect.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.menu_rect, "Menu", self.lives_font, button_hover_menu)
# Draw Incorrect Guess Letters
ui.draw_list_text(self.screen, self.incorrect_box_rect, game.incorrect_guesses, self.chat_font)
def ensuranceScreen(self):
ui.draw_text_box(self.screen, self.sure_rect, "Your game will not save. Are you sure?", self.lives_font)
button_hover_cont = self.button_rect_cont.collidepoint(pygame.mouse.get_pos())
button_hover_dont = self.button_rect_dont.collidepoint(pygame.mouse.get_pos())
ui.draw_button(self.screen, self.button_rect_cont, "YES", self.lives_font, button_hover_cont)
ui.draw_button(self.screen, self.button_rect_dont, "NO", self.lives_font, button_hover_dont)