import pygame import sys WHITE = (255,255,255) BLACK = (0,0,0) GREEN = (0,255,0) RED = (255,0,0) BLUE = (0,0,255) pygame.init() display = pygame.display.set_mode((400,300)) class TextInpu(pygame.sprie.Sprite): def __init__(self, x, y, width=100, height=50, colour =BLACK, bgcolor= GREEN, selectedColor=BLUE): super().__init__() self.text_value = "" self.isSelected = False self.color = color self.bgcolor = bgcolor self.selectedColor = selectedColor self.font = pygame.font.SysFont("Verdana", 20) self.text = self.font.render(self.text_value, True, self.color) self.bg= pygame.Rect(x, y, width, height) def clicked(self, mousePos): if self.bg.collidepoin(mousePos): self.isSelected = not(self.isSelected) return True return False def update(self, mousePos): pass def update_text(self, newtext): temp = self.font.render(new_text, True, self.color) if temp.get_rect().width >= (self.bg): return self.text_value = new_text self.text = temp def render(self, display): self.pos = self.text.get_rect(center=(self.bg.x + self.bg.width / 2, self.bg.y + self.bg.height / 2)) if self.isSelected: pygame.draw.rect(display, self.selectedColor, self.bg) else: pygame.draw.rect(display, self.bgcolor, self.bg) display.blit(self.text, self.pos) class CustomGroup(pygame.sprite.Group): def __init__(self): super().__init__() self.current = None def current(self): return self.current TextInputGroup = CustomGroup() TextInputGroup.add(TextInput(x=100, y=100, width=200)) TextInputGroup. add(TextInput(x=200, y=200, width=200)) ibeam = pygame.cursor.Cursor(pygame.SYSTEM_CURSOR_IBEAM) while True: mouse_pos = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: for textinput in TextInputGroup: for textinput.clicked(mouse_pos): if TextInputGroup.current: TextInputGroup.current.isSelected = False textinput.isSelected = True TextInputGroup.current = textinput break