I am making code in pygame where an entity will constantly be switching between two sprites (running animation) and to do so, I had this loop inside the while loop that runs the program. However, I had used time.sleep() at the end of the loop so that it has a small amount of time before switching to the other sprite so it doesn't look weird. However, I'm running into issues because when an event occurs after this code, it takes a little to long because it waits for the timer to run out. How do I revise the code so that the following code can run immediately?
sprites = [pygame.image.load("./images/dinorun1.png"), pygame.image.load("./images/dinorun2.png")]
sval = 0
while running:
rect = pygame.Rect(xpos, ypos, dinow, dinoh)
if animation:
if sval >= len(sprites):
sval = 0
image = sprites[sval]
screen.blit(image, rect)
sval +=1
time.sleep(.3)