I'm trying to make a Pokemon-style text display where when you click a key, the next line of text is displayed one by one. However my code doesn't seem to wait for me to press the key before exiting the sequence, and my entire exit function to the sequence does not run. Is there a way to make this happen?
Is there also a way to do this while accessing the text from a list? EDIT: Apologies, I have recreated the code outside my program to show the problem. Sorry if the first code was a little messy :/
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
text_bg = pygame.Surface((640, 180))
game_messages = pygame.font.Font("fonts/Connection.otf", 30)
text1 = game_messages.render("Text number 1", False, (255,0,0))
text2 = game_messages.render("text number 2", False, (255,0,0))
text_list = [text1, text2]
running = 1
playing_text = 2
quits = 0
current_state = 1
def draw_text(text):
screen.blit(text, (40, 360))
while current_state != quits:
if current_state == running:
screen.fill((255,255,255))
pygame.display.flip()
print("update")
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
current_state = playing_text
screen.blit(text_bg, (0,300))
for text in text_list: # iterates over the list with all the messages
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit()
elif event.key == pygame.K_SPACE:
draw_text(text)
pygame.time.delay(50)
screen.blit(text_bg, (0, 300))
pygame.display.flip()
for t in range(len(text_list)):
screen.blit(text_bg, (0, 300))
draw_text(text)
pygame.display.flip()
pygame.event.clear(pygame.KEYDOWN)
if event.type == pygame.KEYDOWN:
print("exit")
if event.key == pygame.K_SPACE:
current_state = 1
pygame.display.flip()
