Skip to content

Commit

Permalink
feat; Refactor Code (make code more concise in some places_ (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirClashin1 authored Mar 20, 2023
1 parent 4be7122 commit bf34c02
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 137 deletions.
102 changes: 55 additions & 47 deletions GUI/GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@ def set_params(self, screen_width: int, screen_height: int, screen: pygame.Surfa

# find a font that can draw emojis
fonts = pygame.sysfont.get_fonts()
emojis = [font for font in fonts if "emoji" in font]
if emojis := [font for font in fonts if "emoji" in font]:
self.font = pygame.font.SysFont(emojis[0], 60)
self.button_font = pygame.font.SysFont(emojis[0], 40)
self.small_font = pygame.font.SysFont(emojis[0], 30)

if emojis == []:
else:
print("Didn't find font with emojis")

self.font = pygame.font.Font(None, 60)
self.button_font = pygame.font.Font(None, 40)
self.small_font = pygame.font.Font(None, 30)
else:
self.font = pygame.font.SysFont(emojis[0], 60)
self.button_font = pygame.font.SysFont(emojis[0], 40)
self.small_font = pygame.font.SysFont(emojis[0], 30)

self.button_left = Button(screen_width * .25, screen_height * .9, 200, 60, text="LEFT", font=self.button_font, bg_color=(200, 200, 200), hover_color=(220, 220, 220))
self.button_right = Button(screen_width * .75, screen_height * .9, 200, 60, text="RIGHT", font=self.button_font, bg_color=(200, 200, 200), hover_color=(220, 220, 220))
self.buttons_lst = [self.button_left, self.button_right]
Expand Down Expand Up @@ -99,12 +97,14 @@ def __seperate_text_to_rows(self, text: str, max_width: int, font_to_use: pygame
def __render_text_center(self, texts: list):
"""Render list of pygame text renders in the center of the screen"""
# Get total height of text elements rendered. Sum all text heights and add the spaces between them
total_height = sum([x.get_size()[1] for x in texts]) + self.space_between_text * (len(texts) - 1)

total_height = sum(
x.get_size()[1] for x in texts
) + self.space_between_text * (len(texts) - 1)

# Loop through every text element and render it
for _i, _text in enumerate(texts):
_text_width, _text_height = _text.get_size()

# Get the position to render it in the center of screen
center_x = self.screen_width / 2 - _text_width / 2
center_y = (self.screen_height / 2 - _text_height / 2 + 60 * _i) - total_height / 4 # make all texts centered
Expand All @@ -116,15 +116,21 @@ def ask_question(self, question: str, left_btn_txt: str, right_btn_txt: str) ->
"""Ask a question with two answers.\n
RETURNS: If pressed left_button: Return True. If pressed right_button: Return False"""
if not self.run_gui:
return self.__ask_question_no_gui(question + f" ({left_btn_txt} / {right_btn_txt}) ", left_btn_txt, right_btn_txt, color_before=Fore.GREEN, color_after=Fore.LIGHTMAGENTA_EX)

return self.__ask_question_no_gui(
f"{question} ({left_btn_txt} / {right_btn_txt}) ",
left_btn_txt,
right_btn_txt,
color_before=Fore.GREEN,
color_after=Fore.LIGHTMAGENTA_EX,
)

# Initalize texts and buttons
text_renders = self.__seperate_text_to_rows(question, self.screen_width - 50, self.font)
self.button_left.text = left_btn_txt
self.button_right.text = right_btn_txt

# Basic pygame window loop
while(True):
while True:
self.screen.blit(self.background, (0, 0))

# Update buttons
Expand All @@ -135,15 +141,17 @@ def ask_question(self, question: str, left_btn_txt: str, right_btn_txt: str) ->
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit_func()

# If left mousebutton clicked, check if clicked on a button
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]:
if self.button_left.check_click():
return True
if self.button_right.check_click():
return False

if (
event.type == pygame.MOUSEBUTTONDOWN
and pygame.mouse.get_pressed()[0]
):
if self.button_left.check_click():
return True
if self.button_right.check_click():
return False

# Render the text
self.__render_text_center(text_renders)
pygame.display.update()
Expand All @@ -157,17 +165,16 @@ def text_until_enter(self, text: str):
text_renders = self.__seperate_text_to_rows(text, self.screen_width - 50, self.font)
enter_text = self.small_font.render("Press Enter to continue", True, (0,0,0))

while(True):
while True:
self.screen.blit(self.background, (0, 0))

for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit_func()

# Return when enter pressed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
return

# Render the center text
self.__render_text_center(text_renders)
Expand All @@ -187,12 +194,12 @@ def start_screen(self):
if not self.run_gui:
self.__start_screen_no_gui()
return

text_box_w = 300
text_box_h = 100
name_text_box = TextBox((self.screen_width / 2 - text_box_w / 2,
self.screen_height * .7 - text_box_h / 2, text_box_w, text_box_h), font=self.font)

music_toggle = Toggle(self.screen_width - self.music_toggle_size - 20, 20, "assets/images/MusicOn.png", "assets/images/MusicOff.png", (self.music_toggle_size, self.music_toggle_size))

text = self.font.render("Hi! What is you name?", True, (0,0,0))
Expand All @@ -207,22 +214,23 @@ def start_screen(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit_func()

name_text_box.get_event(event)
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]:
music_toggle.check_click(pygame.mouse.get_pos())

if music_toggle.get_state() == 1:
pygame.mixer.unpause()
else:
pygame.mixer.pause()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
player_name = "".join(name_text_box.buffer)
got_name = True

if (
event.type == pygame.MOUSEBUTTONDOWN
and pygame.mouse.get_pressed()[0]
):
music_toggle.check_click(pygame.mouse.get_pos())

if music_toggle.get_state() == 1:
pygame.mixer.unpause()
else:
pygame.mixer.pause()

if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
player_name = "".join(name_text_box.buffer)
got_name = True

self.screen.blit(text, (self.screen_width / 2 - text.get_size()[0] / 2,
self.screen_height * .85))
music_toggle.draw(self.screen)
Expand Down Expand Up @@ -254,8 +262,8 @@ def __ask_question_no_gui(self, question: str, first: str, second: str, color_be
print("Invalid answer, try again.")

def __start_screen_no_gui(self):
name = input(Fore.YELLOW + "Type your name: " + Fore.LIGHTBLUE_EX)
print(Fore.LIGHTGREEN_EX + "Welcome", name, "to this adventure!")
name = input(f"{Fore.YELLOW}Type your name: {Fore.LIGHTBLUE_EX}")
print(f"{Fore.LIGHTGREEN_EX}Welcome", name, "to this adventure!")

if self.__ask_question_no_gui("Do you want to play? (yes / no) ", "yes", "no", color_before=Fore.YELLOW, color_after=Fore.LIGHTBLUE_EX):
# Yes
Expand All @@ -264,7 +272,7 @@ def __start_screen_no_gui(self):
# No
print("See you later! \U0001F600")
self.exit_func()

random.choice(chapters.my_list)()
# if self.__ask_question_no_gui("Do you want music? \U0001F3B5 (yes / no) ", "yes", "no", color_before=Fore.YELLOW, color_after=Fore.LIGHTBLUE_EX):
# # Yes
Expand Down
Loading

0 comments on commit bf34c02

Please sign in to comment.