微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使精灵出现在 Pygame 中的文本之前?

如何解决如何使精灵出现在 Pygame 中的文本之前?

我正在尝试在 Pygame 中制作一个类似于游戏的小型 RPG,并且我有一个可以缓慢打印文本的功能(就像在很多 RPG 中一样)和一个创建精灵的功能。当我调用它们时,它们都可以工作,但是直到文本打印完成后精灵才会出现。

我尝试移动 sprite 函数,但到目前为止,根据我放置它的位置,它要么不会改变任何东西,要么根本不显示 sprite。我也试过在 text 函数中放置一个精灵绘图函数,但也没有用。

我想知道是否有办法让精灵出现在文本之前。下面是慢速打印文本和创建精灵的函数

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)):
        surface.fill(s_color)
        text += string[i]
        text1 = font.render(text,True,WHITE,BLACK)
        pygame.draw.rect(surface,[text_x,text_y,400,50],1)
        textRect = text1.get_rect()
        textRect.center = (x,y)
        surface.blit(text1,textRect)
        pygame.display.update()
        pygame.time.wait(100)

下面是我实际调用这些函数代码

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:
      carryOn=False
    elif event.type==pygame.KEYDOWN:
      if event.key==pygame.K_x: #Pressing the x Key will quit the game
        carryOn=False
  pygame.display.flip()
  clock.tick(60)

解决方法

你必须打电话

char_sprite(display_surface,place_holder,(text_x -100,text_y - 25))

之前

display_text_animation(display_surface,BLACK,"hello world",(text_x + 65,text_y + 25))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。