import pygame # Importing the pygame library for game development import sys # Importing the sys module to access system-specific parameters and functions # Initialize Pygame pygame.init() # Create a display surface with dimensions 400x300 pixels display = pygame.display.set_mode((400, 300)) # Define a class for text input fields, inheriting from pygame.sprite.Sprite class TextInput(pygame.sprite.Sprite): def __init__(self, x, y, width=100, height=50, color=(0, 0, 0), bgcolor=(0, 255, 0), selectedColor=(0, 0, 255)): super().__init__() # Initialize text input properties self.text_value = "" # Initial text value self.isSelected = False # Indicates whether the text input is currently selected self.color = color # Text color self.bgcolor = bgcolor # Background color self.selectedColor = selectedColor # Color when selected # Create a font object for rendering text self.font = pygame.font.SysFont("Verdana", 20) # Render the initial text value self.text = self.font.render(self.text_value, True, self.color) # Define the background rectangle for the text input field self.bg = pygame.Rect(x, y, width, height) # Method to handle mouse click events on the text input field def clicked(self, mousePos): # Check if the mouse click occurred within the text input field if self.bg.collidepoint(mousePos): # Toggle the selection status self.isSelected = not(self.isSelected) return True # Return True to indicate that the click event was handled return False # Return False if the click event was not handled # Method to update the text input field (placeholder, not implemented) def update(self, mousePos): pass # Method to update the text value of the text input field def update_text(self, new_text): # Render the new text value temp = self.font.render(new_text, True, self.color) # Check if the width of the rendered text exceeds the width of the background rectangle if temp.get_rect().width >= (self.bg.width - 20): return # Return if the text exceeds the width of the background rectangle self.text_value = new_text # Update the text value self.text = temp # Update the rendered text # Method to render the text input field on the display surface def render(self, display): # Calculate the position for rendering the text self.pos = self.text.get_rect(center=(self.bg.x + self.bg.width / 2, self.bg.y + self.bg.height / 2)) # Draw the background rectangle if self.isSelected: pygame.draw.rect(display, self.selectedColor, self.bg) else: pygame.draw.rect(display, self.bgcolor, self.bg) # Render the text on the display surface display.blit(self.text, self.pos) # Define a custom group class for managing text input fields class CustomGroup(pygame.sprite.Group): def __init__(self): super().__init__() self.current = None # Initialize the current text input field to None def current(self): return self.current # Return the current text input field # Create a CustomGroup instance to manage text input fields TextInputGroup = CustomGroup() # Add text input fields to the group with specified positions and widths TextInputGroup.add(TextInput(x=100, y=100, width=200)) TextInputGroup.add(TextInput(x=100, y=200, width=200)) # Define the ibeam cursor for text input mode ibeam = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_IBEAM) # Main game loop while True: # Get the current mouse position mouse_pos = pygame.mouse.get_pos() # Handle events for event in pygame.event.get(): # Check if the user wants to quit the game if event.type == pygame.QUIT: pygame.quit() # Quit Pygame sys.exit() # Exit the program # Handle mouse button click events if event.type == pygame.MOUSEBUTTONDOWN: # Iterate over text input fields for textinput in TextInputGroup: # Check if a text input field was clicked if textinput.clicked(mouse_pos): if TextInputGroup.current: TextInputGroup.current.isSelected = False textinput.isSelected = True TextInputGroup.current = textinput break # Handle keyboard events if event.type == pygame.KEYDOWN: # Handle backspace key press if event.key == pygame.K_BACKSPACE: # Update the text value of the current text input field by removing the last character TextInputGroup.current.update_text(TextInputGroup.current.text_value[:-1]) # Handle return key press if event.key == pygame.K_RETURN: if TextInputGroup.current: print(TextInputGroup.current.text_value) # Handle text input events if event.type == pygame.TEXTINPUT: # Update the text value of the current text input field by adding the entered text TextInputGroup.current.update_text(TextInputGroup.current.text_value + event.text) # Update and render text input fields for textinput in TextInputGroup: textinput.update(mouse_pos) textinput.render(display) # Change cursor to ibeam if it's over a text input field if TextInputGroup.current and TextInputGroup.current.bg.collidepoint(mouse_pos): pygame.mouse.set_cursor(ibeam) else: pygame.mouse.set_cursor(pygame.cursors.Cursor()) # Update the display pygame.display.update()