import pygame, sys

pygame.init()
clock = pygame.time.Clock()

WIDTH = 800
HEIGHT = 800
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)

surface = pygame.display.set_mode((WIDTH, HEIGHT))

base_font = pygame.font.Font(None, 32)
user_text = "a"

input_rect = pygame.Rect(200, 200, 140, 32)

active = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False

        if event.type == pygame.KEYDOWN:
            if active == True:
                if event.key == pygame.K_BACKSPACE:
                    user_text = user_text[:-1]
                else:
                    user_text += event.unicode
        

    surface.fill(BLACK)


    pygame.draw.rect(surface, BLUE, input_rect, 2)

    text_surface = base_font.render(user_text, True, WHITE)
    surface.blit(text_surface, (input_rect.x + 5, input_rect.y + 5))

    input_rect.w = max(100, text_surface.get_width() + 10)

    pygame.display.flip()
    clock.tick(60)