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() # Creating a display surface with dimensions 400x300 pixels display = pygame.display.set_mode((400, 300)) background = pygame.Surface((400, 300)) # Creating a surface for the background # Creating a font object using the Verdana font with a size of 20 pixels font = pygame.font.SysFont("Verdana", 20) # Initializing text_value as an empty string text_value = "" # Rendering the text to be displayed, initially empty text = font.render(text_value, True, (255, 255, 255)) # Rendering the text in white color # Main game loop while True: # Getting the current position of the mouse mouse_pos = pygame.mouse.get_pos() # Handling events for event in pygame.event.get(): # Checking if the window close button is clicked if event.type == pygame.QUIT: pygame.quit() # Quitting Pygame sys.exit() # Exiting the program # Handling key press events if event.type == pygame.KEYDOWN: # Checking if the backspace key is pressed if event.key == pygame.K_BACKSPACE: text_value = text_value[:-1] # Removing the last character from text_value text = font.render(text_value, True, (255, 255, 255)) # Rendering the updated text # Checking if the return key is pressed if event.key == pygame.K_RETURN: print(text_value) # Printing the current text_value to the console # Handling text input events if event.type == pygame.TEXTINPUT: text_value += event.text # Appending the entered text to text_value text = font.render(text_value, True, (255, 255, 255)) # Rendering the updated text # Blitting the background onto the display surface display.blit(background, (0, 0)) # Blitting the rendered text onto the display surface at position (100, 150) display.blit(text, (100, 150)) # Updating the display pygame.display.update()