# Importing necessary modules from time import sleep # Importing the sleep function from the time module import pygame # Importing the pygame module for game development import pygame_menu # Importing the pygame_menu module for creating menus in Pygame from pygame_menu import themes # Importing themes submodule from pygame_menu for menu themes # Initializing Pygame pygame.init() # Creating a display surface with dimensions 600x400 pixels surface = pygame.display.set_mode((600, 400)) # Function to handle setting the game difficulty def set_difficulty(value, difficulty): print(value) # Printing the selected value print(difficulty) # Printing the selected difficulty # Function to start the game def start_the_game(): mainmenu._open(loading) # Opening the loading menu when the game starts pygame.time.set_timer(update_loading, 30) # Setting a timer to update the loading progress # Function to open the level selection menu def level_menu(): mainmenu._open(level) # Opening the level selection menu # Creating the main menu with the title 'Welcome', using the SOLARIZED theme mainmenu = pygame_menu.Menu('Welcome', 600, 400, theme=themes.THEME_SOLARIZED) # Adding a text input field labeled 'Name:' with a default value 'username' and maximum character limit of 20 mainmenu.add.text_input('Name: ', default='username', maxchar=20) # Adding a button labeled 'Play', which triggers the start_the_game() function when clicked mainmenu.add.button('Play', start_the_game) # Adding a button labeled 'Levels', which triggers the level_menu() function when clicked mainmenu.add.button('Levels', level_menu) # Adding a button labeled 'Quit', which exits the program when clicked mainmenu.add.button('Quit', pygame_menu.events.EXIT) # Creating the level selection menu with the title 'Select a Difficulty', using the BLUE theme level = pygame_menu.Menu('Select a Difficulty', 600, 400, theme=themes.THEME_BLUE) # Adding a selector labeled 'Difficulty :' with options ('Hard', 1) and ('Easy', 2) # The set_difficulty function will be called when the selection changes level.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty) # Creating the loading menu with the title 'Loading the Game', using the DARK theme loading = pygame_menu.Menu('Loading the Game', 600, 400, theme=themes.THEME_DARK) # Adding a progress bar labeled 'Progress' with default value 0 and width 200 loading.add.progress_bar("Progress", progressbar_id="1", default=0, width=200) # Creating a LeftArrowSelection widget for menu navigation arrow = pygame_menu.widgets.LeftArrowSelection(arrow_size=(10, 15)) # Defining a custom event for updating the loading progress update_loading = pygame.USEREVENT + 0 # Main game loop while True: events = pygame.event.get() # Getting all Pygame events for event in events: if event.type == update_loading: # Checking for custom update_loading event progress_bar = loading.get_widget("1") # Getting the progress bar widget progress_bar.set_value(progress_bar.get_value() + 1) # Incrementing the progress bar value if progress_bar.get_value() == 100: # Checking if loading progress is complete pygame.time.set_timer(update_loading, 0) # Disabling the update_loading timer if event.type == pygame.QUIT: # Checking if the window close button is clicked pygame.quit() # Quitting Pygame if mainmenu.is_enabled(): # Checking if the main menu is active mainmenu.update(events) # Updating the main menu mainmenu.draw(surface) # Drawing the main menu on the display surface if mainmenu.get_current().get_selected_widget(): # Checking if a widget is selected in the main menu arrow.draw(surface, mainmenu.get_current().get_selected_widget()) # Drawing the arrow selection pygame.display.update() # Updating the display to show changes