I'm trying to work on a simple Pygame program for a project that simply displays some faces and talks in a text to speech voice, but there is a while loop at the end that is necessary for the code to run but blocks another while loop that I need for the program from running. The while loop I'm trying to add uses time.sleep()
, so if I try to put it into the same block as the first one which needs to be constantly running the program crashes. I'm sure I'm probably looking over something obvious but any help would be appreciated, thanks!
Here's the code:
from random import randint
from time import sleep
import pygame
import pygame.freetype
import time
import random
run = True
pygame.init()
#faces
face = ['^-^', '^v^', '◠◡◠', "'v'", '⁀◡⁀']
talkingFace = ['^o^', '^▽^', '◠▽◠', "'▽'", '⁀ᗢ⁀']
currentFace = random.choice(face)
#background
screen = pygame.display.set_mode((800,600))
screen.fill((0,0,0))
#font and size
myFont = pygame.font.Font('unifont.ttf', 100)
#face render
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
#center and draw face
text_rect = faceDisplay.get_rect(center=(800/2, 600/2))
screen.blit(faceDisplay, text_rect)
#prevent crashes
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
pygame.display.flip()
#loop i'm trying to add
while run:
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
screen.blit(faceDisplay, text_rect)
time.sleep(randint(5, 10))