def save_load_page():
bg = pygame.image.load(resource_path("./Game_Assets/Backgrounds/menu_background.png"))
bg = pygame.transform.scale(bg,(900,600))
ogre_set = pygame.image.load(resource_path("./Game_Assets/Character_Name/Ogre_Character_Overview.png"))
ogre_set = pygame.transform.scale(ogre_set,(844,420))
name_input = ''
change_click = False
click = False
while True:
mx, my = pygame.mouse.get_pos()
screen.blit(bg,(0,0))
screen.blit(ogre_set,(22,140))
text_surface = base_font.render(name_input,True,(255,255,255))
screen.blit(text_surface,(60, 445))
# Logic goes here
change_click = False
click = False
# Event Collections
for event in pygame.event.get():
# Function to quit game
'''if event.type == QUIT:
event.quit()
sys.exit()'''
if event.type == KEYDOWN:
if event.key == K_RETURN:
pass
elif event.key == K_KP_ENTER:
ogre1_name = name_input
break
elif event.unicode == K_alpha:
name_input += event.Ascii
if len(name_input) <= 21:
pass
while name == '' or len(name) > 20 or name in names_list:
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
change_click = True
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
clock.tick(120)
How can I apply a limit on input(not more than 20 letters) and the input value can be just alphabet?
Asked
Active
Viewed 100 times
0
-
Is the issue solved? – Rabbid76 Nov 17 '20 at 20:15
-
Yeah Thank you and sorry for the late reply @Rabbid76 – Bella Lee Feb 18 '21 at 01:39
1 Answers
0
You have to evaluate if the length of the text is less than 20, before you add the new character. isalpha()
can be used to test whether a character is a letter (see How can I check if character in a string is a letter?):
if event.type == KEYDOWN:
if event.key == K_RETURN:
# [...]
elif event.key == K_KP_ENTER:
# [...]
elif len(name_input) < 20 and event.unicode.isalpha():
name_input += event.unicode

Rabbid76
- 202,892
- 27
- 131
- 174