I'm working on a project and I want to implement user's text input. It was working, but was too hard to read and was not very practical, so I reworked it. But it stopped working. I looked up the internet and found this video and this stack overflow article, but couldn't resolve the problem. What is wrong?
Whole class + run method (I'm not having an error)
Update: I had one line missing. Now it's withour an error
import pygame, sys
pygame.init()
class textBox:
def __init__(self, font, surface, x, y, width, height):
self.width = width
self.height = height
self.inactive = 'blue'
self.active ='red'
self.font = font
self.userText = ''
self.displaySurface = surface
self.rect = pygame.Rect(x, y, self.width, self.height)
self.color = self.inactive
self.active = False
def inputCheck(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
pygame.time.wait(100)
else:
self.active = False
pygame.time.wait(100)
self.color = self.active if self.active else self.inactive
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_BACKSPACE:
self.userText = self.userText[:-1]
if event.key == pygame.K_RETURN:
return self.userText
else:
self.userText += event.unicode
def update(self):
if len(self.userText) >= 21:
self.userText = self.userText[:-1]
print('you cannot type anymore')
self.rect.w = max(230, self.textSurf.get_width() + 10)
def renderText(self):
pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
self.textSurf = self.font.render(self.userText, True, (0, 0, 0))
self.displaySurface.blit(self.textSurf, (self.rect.x + 5, self.rect.y + 5))
bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
box.inputCheck(event)
box.renderText()
pygame.display.flip()
clock.tick(60)
Update: The code bellow is the old working code
import pygame, sys
pygame.init()
class textBox:
def __init__(self, font, surface, x, y, width, height):
self.width = width
self.height = height
self.inactive = 'blue'
self.active ='red'
self.font = font
self.userText = ''
self.displaySurface = surface
self.rect = pygame.Rect(x, y, self.width, self.height)
self.color = self.inactive
self.active = False
def inputCheck(self):
self.userText += event.unicode
def renderText(self):
pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
textSurf = self.font.render(self.userText, True, (0, 0, 0))
self.displaySurface.blit(textSurf, (self.rect.x + 5, self.rect.y + 5))
if len(self.userText) >= 21:
self.userText = self.userText[:-1]
print('you cannot type anymore')
self.rect.w = max(230, textSurf.get_width() + 10)
bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if box.rect.collidepoint(event.pos):
box.active = not box.active
pygame.time.wait(100)
else:
box.active = False
pygame.time.wait(100)
box.color = box.active if box.active else box.inactive
if event.type == pygame.KEYDOWN:
if box.active:
if event.key == pygame.K_BACKSPACE:
box.userText = box.userText[:-1]
if event.key == pygame.K_RETURN:
pass
else:
box.inputCheck()
screen.fill((255, 255, 255))
box.renderText()
pygame.display.flip()
clock.tick(60)