Reputation: 153
I have a problem with pygame where I'm trying to show a game message but it just resets the background after the user clicks enter, thus resulting in the game message being shown only for a split second.
I gather user input in a while loop (because I only need it to run three times) and use pygame.event.poll() to detect incoming inputs such as keyboard input etc. So the way I made it is that when the user presses enter it'll evaluate the data entered and show a game message if the input is correct or not. However, the game message will just be removed in a split second since its in a while loop. Is there any way I can make it so that the game message shows up until I click enter again?
Here is the shortened version of the code:
while bad_input < 3:
screen.blit(Variables.bgImage, [0, 0])
...
...
e = pygame.event.poll()
...
if e.type == KEYDOWN:
# Add a character
textBox.AddChar(pygame.key.name(e.key))
...
# If the user presses return,
elif e.key == K_RETURN:
# And if the text size was more than 0
if len(textBox.text) > 0:
user_input = textBox.text.lower()
if result[0]:
showGameLog("Correct!")
else:
showGameLog("Hm, not quite.")
bad_input += 1
textBox.text = ""
# Then update the textbox
textBox.Update()
pygame.display.update()
and here is the code for the showGameLog function:
def showGameLog(txt):
text = font.render(txt , True, Color.black)
screen.blit(logText, [25, 570])
Here's a video on how the problem looks like: problemvid
Upvotes: 1
Views: 175
Reputation: 210996
Add a state showText
:
showText = False
Set the state when RETURN is pressed, but reset the state if RETURN and showText == True
:
elif e.key == K_RETURN:
if showText:
showText = False
elif len(textBox.text) > 0: # And if the text size was more than 0
showText = True
# [...]
Show the game log, dependent on the state of showText
, in the main application loop:
if showText:
if result[0]:
showGameLog("Correct!")
else:
showGameLog("Hm, not quite.")
Likely you want to skip any input as long the game log is shown:
if e.type == KEYDOWN:
if not showText:
# Add a character
textBox.AddChar(pygame.key.name(e.key))
Example code:
showText = False
while bad_input < 3:
screen.blit(Variables.bgImage, [0, 0])
# [...]
e = pygame.event.poll()
# [...]
elif e.type == KEYDOWN:
if showText:
if e.key == K_RETURN:
showText = False
else:
# Add a character
textBox.AddChar(pygame.key.name(e.key))
# If the user press space, add a empty text and update the textbox.
if e.key == K_SPACE:
textBox.text += " "
textBox.Update()
# If the user presses backspace, delete a character
elif e.key == K_BACKSPACE:
textBox.text = textBox.text[:-1]
textBox.Update()
# If the user presses return,
elif e.key == K_RETURN:
# And if the text size was more than 0
if len(textBox.text) > 0:
showText = True
# [...]
user_input = textBox.text.lower()
if not result[0]:
bad_input += 1
textBox.text = ""
# Then update the textbox
textBox.Update()
if showText:
if result[0]:
showGameLog("Correct!")
else:
showGameLog("Hm, not quite.")
pygame.display.update()
Upvotes: 1