Ask coding questions

← Back to all posts
How to make a sprite appear before text in Pygame?
Madeline69 (0)

I'm trying to make a small RPG like game in Pygame and I have a function that prints text slowly (like in a lot of RPGs) and a function that creates a sprite. When I call them they both work however the sprite doesn't appear until after the text is done printing.

I have tried moving the sprite function around but so far, depending on where I put it, it either doesn't change anything or the sprite just doesn't show up at all. I have also tried putting a sprite drawing function inside of the text function but that didn't work either.

I was wondering if there is a way to make it so the sprite appears before the text. Below is the functions for printing the text slow and for creating the sprite.

 place_holder = pygame.image.load('place_holder_frog.png')

 def char_sprite(surface,img,tuple):
   x,y = tuple
   surface.blit(img, (x,y))

 def display_text_animation(surface, s_color, string, tuple):
   x,y = tuple
   text = ''
   for i in range(len(string)):
      text += string[i]
      text1 = font.render(text, True, WHITE, BLACK)
      pygame.draw.rect(surface, WHITE, [text_x, text_y,400, 50],1)
      textRect = text1.get_rect() = (x, y)
      surface.blit(text1, textRect)

Below is the code where I actually call those functions:

 if screen_num == 0:
   display_text_animation(display_surface, BLACK, "hello world",(text_x + 65, text_y + 25))
   char_sprite(display_surface,place_holder, (text_x -100,text_y - 25))

 while carryOn:
   for event in pygame.event.get():
     if event.type==pygame.QUIT:
     elif event.type==pygame.KEYDOWN:
       if event.key==pygame.K_x: #Pressing the x Key will quit the game

Thanks in advanced!! :)

InvisibleOne (2678)

I think you need to call the char_spride() function before your display_text_animation() function.

Madeline69 (0)

@InvisibleOne Thank you for the suggestion! I have tried this before however, when I do that the sprite just doesn't appear at all.