From 2960dfad299b3de69248db6d7c178d93784be27c Mon Sep 17 00:00:00 2001 From: Statute8234 <76547158+Statute8234@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:00:30 -0400 Subject: [PATCH] Update window_resize.py --- pygame_menu/examples/window_resize.py | 120 +++++++++++++++++--------- 1 file changed, 80 insertions(+), 40 deletions(-) diff --git a/pygame_menu/examples/window_resize.py b/pygame_menu/examples/window_resize.py index 84ebaf0c..670b0044 100644 --- a/pygame_menu/examples/window_resize.py +++ b/pygame_menu/examples/window_resize.py @@ -7,55 +7,95 @@ """ import pygame +import random, sys, math, time import pygame_menu +from pygame_menu import themes pygame.init() +current_time = time.time() +random.seed(current_time) +# Seting up the display +screenWidth, screenHeight = 700, 700 +screen = pygame.display.set_mode((screenWidth, screenHeight), pygame.RESIZABLE) +pygame.display.set_caption("Race Game") +clock = pygame.time.Clock() -surface = pygame.display.set_mode((600, 400), pygame.RESIZABLE) -pygame.display.set_caption("Example resizable window") +# color +def random_color(): + return (random.randint(0,255),random.randint(0,255),random.randint(0,255)) +colors = { + "RED": (255, 0, 0), + "GREEN": (0, 255, 0), + "BLUE": (0, 0, 255), + "WHITE": (255, 255, 255), + "BLACK": (0, 0, 0), + "GRAY": (128, 128, 128), + "DARK_GRAY": (169, 169, 169), +} -menu = pygame_menu.Menu( - height=100, - theme=pygame_menu.themes.THEME_BLUE, - title='Welcome', - width=100 -) +# Function checked if the window is resized. +def on_resize() -> None: + window_size = screen.get_size() + new_w, new_h = window_size[0], window_size[1] + menu.main_menu.resize(new_w, new_h) + menu.player_screen.resize(new_w, new_h) + menu.settings_screen.resize(new_w, new_h) +# main menu +class MainMenu: + def __init__(self, screen, width, height): + self.screen = screen + self.width = width + self.height = height + self.create_main_menu() + + def create_main_menu(self): + self.main_menu = pygame_menu.Menu('Welcome', self.width, self.height, theme=pygame_menu.themes.THEME_DARK) + self.player_screen = pygame_menu.Menu('Player Screen', self.width, self.height, theme=pygame_menu.themes.THEME_DEFAULT) + self.settings_screen = pygame_menu.Menu('Settings', self.width, self.height, theme=pygame_menu.themes.THEME_DARK) + self.main_menu.add.button('Play', self.game) + self.main_menu.add.button('Settings', self.settings) + self.main_menu.add.button('Quit', pygame_menu.events.EXIT) + + def quitMenu(self): + self.main_menu.disable() -def on_resize() -> None: - """ - Function checked if the window is resized. - """ - window_size = surface.get_size() - new_w, new_h = 0.75 * window_size[0], 0.7 * window_size[1] - menu.resize(new_w, new_h) - print(f'New menu size: {menu.get_size()}') - - -menu.add.label('Resize the window!') -user_name = menu.add.text_input('Name: ', default='John Doe', maxchar=10) -menu.add.selector('Difficulty: ', [('Hard', 1), ('Easy', 2)]) -menu.add.button('Quit', pygame_menu.events.EXIT) -menu.enable() -on_resize() # Set initial size - -if __name__ == '__main__': - while True: + def game(self): + self.player_screen.add.text_input(title="Country Name: ") + self.player_screen.add.button('Play', self.quitMenu) + self.main_menu._open(self.player_screen) + + def settings(self): + self.music_volume = self.settings_screen.add.range_slider('Music Volume', default=50, range_values=(0, 100), increment=1) + self.settings_screen.add.range_slider('Sound Effects', default=50, range_values=(0, 100), increment=1) + self.settings_screen.add.range_slider('Frame Rate', default=60, range_values=(30, 120), increment=1) + self.settings_screen.add.range_slider('Brightness', default=100, range_values=(0, 100), increment=1) + self.settings_screen.add.button('Return to Main Menu', pygame_menu.events.BACK) + self.main_menu._open(self.settings_screen) +menu = MainMenu(screen, screenWidth, screenHeight) +on_resize() + +# main +def main(): + global screen + running = True + while running: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: - pygame.quit() - break - if event.type == pygame.VIDEORESIZE: - # Update the surface - surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE) - # Call the menu event + running = False + sys.exit() + elif event.type == pygame.VIDEORESIZE: + screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE) on_resize() - - # Draw the menu - surface.fill((25, 0, 50)) - - menu.update(events) - menu.draw(surface) - + screen.fill(colors["WHITE"]) + menu.main_menu.update(events) + menu.main_menu.draw(screen) + # This is to update the scene + clock.tick(64) pygame.display.flip() + pygame.display.update() + +# loop +if __name__ == "__main__": + main()