-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
277 lines (201 loc) · 9.3 KB
/
game.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
import pygame
import constants
from settings import Settings
from color_constants import ColorConst
from input_func import InputFunc
from fonts import get_font_paths, get_font_paths_dict
import tts
from translator import Translator
from strings_on_screen import StringImage
from text_icon_gen import Gen
class Game:
#TODO: Make specical characters requiring multiple presses work
#TODO: Fix distorted tts voices for chars in some languages.
def __init__(self) -> None:
self.SCREEN_WIDTH = 0
self.SCREEN_HEIGHT = 0
self.user_name = ""
self.full_screen = 0
self.SCREEN_WIDTH_WINDOWED = 0
self.SCREEN_HEIGHT_WINDOWED = 0
self.lang = ""
self.slow_speech = 0
self.repetitions = 1
self.instructions = 1
self.screen = None
self.progress = constants.PROGRESS_START
self.FPS = 5
self.background_color = ColorConst.BEIGE
self.mode_name = "Welcome"
self.img_folder = "img/"
# Clock
self.clock = pygame.time.Clock()
# Translator
self.trans = Translator()
# Read Settings
self.read_settings()
if not self.user_name:
self.user_name = "User"
self.settings = Settings.r_setup()
self.menu_texts = Settings.r_menu_text()
self.lang_code = self.trans.get_code(self.settings["language"])
self.extra_chars = Settings.r_extra_chars()
# Caption
self.caption = Settings.r_instructions()[self.lang]["Caption"]
pygame.display.set_caption(self.caption)
# TTS
self.tts = tts.TTS(self.lang, self.slow_speech, caller_instance=self)
# Screen
infoObject = pygame.display.Info()
self.MAX_SCREEN_WIDTH = infoObject.current_w
self.MAX_SCREEN_HEIGHT = infoObject.current_h
self.adjust_window_size()
# Font
self.font_paths = get_font_paths()
self.font_paths_dict = get_font_paths_dict(self.font_paths)
self.font_path = self.font_paths_dict[self.font_name_in_use]
self.gen = Gen()
# Create the Exit button in the upper left corner
# Save png to file
back_img_file = f"{self.img_folder}back_button.png"
self.gen.create_exit_button(back_img_file)
self.exit_button = pygame.image.load(back_img_file) #pygame.Rect(0, 0, 50, 25)
self.string_image = StringImage(self.font_path, "", self.screen, 1800, 0, 0, self.background_color, self.text_color)
self.size = pygame.RESIZABLE
def read_settings(self):
settings = Settings.r_setup()
self.user_name = settings["user_name"]
self.slow_speech = settings["slow_speech"]
self.lang = self.trans.get_code(settings["language"])
self.repetitions = settings["repetitions"]
self.instructions = settings["instructions"]
self.letter_case = settings["letter_case"]
self.excluded_chars = settings["excluded_characters"]
self.font_name_in_use = settings["font"]
self.full_screen = settings["full_screen"]
self.SCREEN_WIDTH_WINDOWED = settings["window_width"]
self.SCREEN_HEIGHT_WINDOWED = settings["window_height"]
self.text_color = settings["text_color"]
self.background_color = settings["background_color"]
def adjust_window_size(self):
if int(self.full_screen):
self.set_display_size_mode(constants.DISPLAY_MODE_FULLSCREEN)
else:
self.set_display_size_mode(constants.DISPLAY_MODE_WINDOWED)
def set_display_size_mode(self, display_mode):
resizeable = (display_mode == constants.DISPLAY_MODE_WINDOWED)
self.size = pygame.RESIZABLE if resizeable else pygame.FULLSCREEN
if resizeable:
self.SCREEN_WIDTH = self.SCREEN_WIDTH_WINDOWED
self.SCREEN_HEIGHT = self.SCREEN_HEIGHT_WINDOWED
else:
self.SCREEN_WIDTH = self.MAX_SCREEN_WIDTH
self.SCREEN_HEIGHT = self.MAX_SCREEN_HEIGHT
if self.screen:
self.screen = pygame.transform.scale(self.screen, (self.SCREEN_WIDTH, self.SCREEN_HEIGHT))
self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT), self.size)
pygame.display.update()
def draw(self):
self.string_image.update()
self.screen.blit(self.exit_button, (25, 25))
pygame.display.flip() # update the whole screen (pygame.display.update() for certain parts only)
def handle_correct_key_pressed(self, key_pressed):
pass
def handle_key_pressed(self, key_pressed):
pass
def init_extended(self):
pass
# Changed April 30
def draw_error_no_internet(self):
self.string_image = StringImage(self.font_path, "ERROR - No Internet Connection!", self.screen, bg_color=ColorConst.RED4, text_color=ColorConst.BLACK)
#self.string_image.background_color = ColorConst.RED4
self.string_image.update()
pygame.display.flip()
# Changed April 30
def draw_mode_welcome(self):
string_image = StringImage(self.font_path, self.mode_name, self.screen, bg_color=self.background_color, text_color=self.text_color)
#string_image.background_color = self.background_color
#string_image.text_color = self.text_color
string_image.update()
pygame.display.flip()
def handle_key_input(self, events):
"""
Handle keyboard input events.
Parameters:
events (list): A list of pygame events.
Returns:
None
"""
if InputFunc.is_exit_event(events) or InputFunc.is_escape_event(events):
self.progress = constants.PROGRESS_END
return
if not self.tts.first_done:
return
key_pressed = InputFunc.get_key_pressed_if_any(events)
if not key_pressed:
return
self.handle_key_pressed(key_pressed)
def handle_mouse_input(self, events):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if self.exit_button.get_rect().collidepoint(mouse):
self.progress = constants.PROGRESS_END
def handle_window_size_changed(self, events):
#ToDo:The standard approach to handling window movement and resizing in Pygame will pause
#ToDo:the event loop while the window is being moved or resized. One way to mitigate this
#ToDo:and keep the event loop running while the window is being moved or resized is to use
#ToDo:a separate thread to handle the window events.
for event in events:
if event.type == pygame.VIDEORESIZE:
# ToDO: save to settings
#self.screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
self.window_min_size_enforcement()
self.string_image.update_text_size()
self.draw()
def window_min_size_enforcement(self):
'''
Ensures that the size of the Pygame display window meets a minimum size requirement of 400 pixels
in both the horizontal and vertical dimensions.
If the window size is less than 400 pixels in either dimension, the method sets the window size
to 400 pixels in that dimension.
'''
x, y = pygame.display.get_window_size()
x = max(x, 400)
y = max(y, 400)
Settings.write_window_size_settings(x, y)
self.screen = pygame.display.set_mode((x, y), pygame.RESIZABLE)
'''
Potential issue:
The end_after_tts() method is checking if self.tts.get_busy() is False before setting self.progress to constants.PROGRESS_END.
However, there is no guarantee that the TTS will finish playing within the same loop iteration, so this check may be incorrect.
'''
def end_after_tts(self):
if not self.tts.get_busy():
self.progress = constants.PROGRESS_END
def get_events(self):
return pygame.event.get()
def extern_run_no_internet(self):
self.clock.tick(self.FPS)
events = self.get_events()
self.handle_key_input(events)
self.handle_mouse_input(events)
self.handle_window_size_changed(events)
self.draw_error_no_internet()
self.draw()
def run(self):
while self.progress:
if self.progress == constants.PROGRESS_WIN:
self.end_after_tts()
self.clock.tick(self.FPS)
events = self.get_events()
self.handle_key_input(events)
self.handle_mouse_input(events)
self.handle_window_size_changed(events)
self.tts.check_queue()
if not self.tts.get_busy():
self.draw()
def game_loop(self):
###print(self.mode_name)
self.init_extended()
self.run()