【问题标题】:How to draw text in front of the background?如何在背景前面绘制文字?
【发布时间】:2021-06-25 14:34:45
【问题描述】:

我正在 pygame 中制作程序。程序首先根据事件循环中的事件键入一些文本。这样做的问题是文本将在背景后面。

打字的部分:

        elif event.type == write_text_0_event:
            if index_0 <= len(msg0) - 1:
                current_text_0 += msg0[index_0]

                text = font.render(current_text_0, True, (255, 255, 255))
                screen.blit(text, (100, 100))

                index_0 += 1

绘制背景的部分:

while running:
    # [...] (the event loop)
    screen.blit(city_bg, (0, 0))

我尝试在事件循环前面插入screen.blit() 行,但文本仍然在背景后面。

如何让文字出现在背景前面而不是出现在背景后面?

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    如果要永久绘制文本,则需要在应用程序循环中进行绘制。在事件循环中绘制文本时,只会在事件发生的帧中绘制。
    在循环之前定义变量text (text = None)。在事件循环中设置变量。并在应用循环中绘制text if text != None:

    text = None
    
    # application loop 
    while running:
        # [...]
    
        # event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
     
            elif event.type == write_text_0_event:
                if index_0 <= len(msg0) - 1:
                    current_text_0 += msg0[index_0]
                    text = font.render(current_text_0, True, (255, 255, 255))
                    index_0 += 1
    
        screen.blit(city_bg, (0, 0))
        if text != None:
            screen.blit(text, (100, 100))
    
        # [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多