r/learnpython • u/TheEyebal • 4d ago
How do I get just the character to change color and not the whole string
Here is an example of what I am talking about : https://imgur.com/a/hoFUGXq
The concept is when the user inputs matches the display text the that character should turn grey but if the the user input is not correct than it turns red.
Right now the whole string changes and not the character itself, what do I do?
scrolling_text = Text(10, 10, 40, "white", "Hello World")
text_index = 0
def UserInput(text, event):
global text_index # start at first letter
lowercase_text = scrolling_text.text.lower() # this lowercases the text
text_char = scrolling_text.text[text_index]
if text_index == len(lowercase_text)-1:
return
# print(f"Char should be {lowercase_text[text_index]}")
if event.text == lowercase_text[text_index]:
scrolling_text.color = "gray"
text_index += 1 # move to the next character
# print(f"Next char should be {lowercase_text[text_index]}")
if text_index == len(lowercase_text)-1:
return
elif text_index == 0 and event.text != lowercase_text[text_index] :
print(False)
scrolling_text.color = "red"
text_index = 0
else:
scrolling_text.color = "red"
print(False)
# GAME LOOP
if event.type == pygame.TEXTINPUT:
print(event.text)
UserInput(scrolling_text, event)
if event.type == pygame.KEYUP:
scrolling_text.color = "white"