【问题标题】:pygame - How to display text with font & color?pygame - 如何用字体和颜色显示文本?
【发布时间】:2012-04-22 02:06:32
【问题描述】:

有没有一种方法可以使用 python 在 pygame 窗口上显示文本?

我需要显示一堆更新的实时信息,并且不想为我需要的每个角色制作图像。

我可以将文本传送到屏幕上吗?

【问题讨论】:

标签: python pygame


【解决方案1】:

是的。可以在pygame中绘制文字:

# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 15)

# render text
label = myfont.render("Some text!", 1, (255,255,0))
screen.blit(label, (100, 100))

【讨论】:

  • 好的,如果电脑上没有安装字体,我可以安装字体吗?
  • 我不确定,但看起来你可以。根据font.SysFont 的文档,它从系统字体(pygame.org/docs/ref/font.html#pygame.font.SysFont)加载字体。
  • @maxhud:是的。使用pygame.font.Font("path/to/font.ttf",size)
【解决方案2】:

您可以通过使用pygame.font.Font设置字体路径来使用自己的自定义字体

pygame.font.Font(filename, size): return Font

示例:

pygame.font.init()
font_path = "./fonts/newfont.ttf"
font_size = 32
fontObj = pygame.font.Font(font_path, font_size)

然后使用 fontObj.render 渲染字体并像上面 veiset 的回答一样将 blit 到一个表面。 :)

【讨论】:

    【解决方案3】:

    我的游戏中有一些显示实时比分的代码。它在一个快速访问的功能中。

    def texts(score):
       font=pygame.font.Font(None,30)
       scoretext=font.render("Score:"+str(score), 1,(255,255,255))
       screen.blit(scoretext, (500, 457))
    

    我在我的 while 循环中使用它来调用它:

    texts(score)
    

    【讨论】:

    • 这样每次都会创建一个Font对象,浪费处理周期。
    • 我建议对您使用的字体大小进行缓存。只需定义一个字典并这样做:如果 self.fonts.get(size) 为 None: self.fonts[size] = pygame.font.SysFont(None, size)
    【解决方案4】:

    我写了一个包装器,它会缓存文本表面,只有在脏时才会重新渲染。 googlecode/ninmonkey/nin.text/demo/

    【讨论】:

    • 这个问题的答案如何?
    【解决方案5】:

    有两种可能性。无论哪种情况,PyGame 都必须由 pygame.init 初始化。

    import pygame
    pygame.init()
    

    使用pygame.font 模块并创建pygame.font.SysFontpygame.font.Font 对象。 render() pygame.Surfaceblit Surface 到屏幕:

    my_font = pygame.font.SysFont(None, 50)
    text_surface = myfont.render("Hello world!", True, (255, 0, 0))
    screen.blit(text_surface, (10, 10))
    

    或者使用pygame.freetype 模块。创建 pygame.freetype.SysFont()pygame.freetype.Font 对象。 render()一个pygame.Surface带文字或者直接render_to()文字到屏幕:

    my_ft_font = pygame.freetype.SysFont('Times New Roman', 50)
    my_ft_font.render_to(screen, (10, 10), "Hello world!", (255, 0, 0))
    

    另见Text and font


    最小pygame.font 示例: repl.it/@Rabbid76/PyGame-Text

    import pygame
    
    pygame.init()
    window = pygame.display.set_mode((500, 150))
    clock = pygame.time.Clock()
    
    font = pygame.font.SysFont(None, 100)
    text = font.render('Hello World', True, (255, 0, 0))
    
    background = pygame.Surface(window.get_size())
    ts, w, h, c1, c2 = 50, *window.get_size(), (128, 128, 128), (64, 64, 64)
    tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
    for rect, color in tiles:
        pygame.draw.rect(background, color, rect)
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        window.blit(background, (0, 0))
        window.blit(text, text.get_rect(center = window.get_rect().center))
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    最小的pygame.freetype 示例: repl.it/@Rabbid76/PyGame-FreeTypeText

    import pygame
    import pygame.freetype
    
    pygame.init()
    window = pygame.display.set_mode((500, 150))
    clock = pygame.time.Clock()
    
    ft_font = pygame.freetype.SysFont('Times New Roman', 80)
    
    background = pygame.Surface(window.get_size())
    ts, w, h, c1, c2 = 50, *window.get_size(), (128, 128, 128), (64, 64, 64)
    tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
    for rect, color in tiles:
        pygame.draw.rect(background, color, rect)
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        window.blit(background, (0, 0))
        text_rect = ft_font.get_rect('Hello World')
        text_rect.center = window.get_rect().center
        ft_font.render_to(window, text_rect.topleft, 'Hello World', (255, 0, 0))
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多