import asyncio import platform import pygame # Initialize Pygame pygame.init() WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pygame Menu GUI") FPS = 60 FONT = pygame.font.SysFont("Arial", 24) # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (150, 150, 150) BLUE = (0, 100, 255) class Button: def __init__(self, x, y, width, height, text, index=None): self.rect = pygame.Rect(x, y, width, height) self.text = text self.index = index # None for Exit button, else option index self.text_surface = FONT.render(text, True, BLACK) self.hovered = False def draw(self, screen): color = BLUE if self.hovered else GRAY pygame.draw.rect(screen, color, self.rect) screen.blit(self.text_surface, (self.rect.x + 10, self.rect.y + 10)) def handle_event(self, event): if event.type == pygame.MOUSEMOTION: self.hovered = self.rect.collidepoint(event.pos) elif event.type == pygame.MOUSEBUTTONDOWN and self.rect.collidepoint(event.pos): return self.index return None class MenuGUI: def __init__(self, options): self.options = options self.buttons = [] self.feedback = "" self.feedback_timer = 0 self.running = True # Create buttons for each option and Exit for i, option in enumerate(options): btn = Button(300, 100 + i * 60, 200, 40, f"{i + 1}. {option}", i + 1) self.buttons.append(btn) self.buttons.append(Button(300, 100 + len(options) * 60, 200, 40, "0. Exit", 0)) def draw(self, screen): screen.fill(WHITE) for button in self.buttons: button.draw(screen) # Display feedback if self.feedback and pygame.time.get_ticks() < self.feedback_timer: feedback_surface = FONT.render(self.feedback, True, BLACK) screen.blit(feedback_surface, (300, 50)) pygame.display.flip() def handle_choice(self, choice): if choice == 0: self.running = False self.feedback = "Exiting..." elif choice is not None: self.feedback = f"You selected: {self.options[choice - 1]}" if choice is not None: self.feedback_timer = pygame.time.get_ticks() + 2000 # Show feedback for 2 seconds def update(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False for button in self.buttons: choice = button.handle_event(event) if choice is not None: self.handle_choice(choice) def setup(): screen.fill(WHITE) def update_loop(menu): menu.update() menu.draw(screen) return menu.running async def main(): menu_options = ["Option 1", "Option 2", "Option 3"] menu = MenuGUI(menu_options) setup() while menu.running: if not update_loop(menu): break await asyncio.sleep(1.0 / FPS) pygame.quit() if platform.system() == "Emscripten": asyncio.ensure_future(main()) else: if __name__ == "__main__": asyncio.run(main())