Python display text with font & color?

后端 未结 5 1134
醉话见心
醉话见心 2020-12-05 06:16

Is there a way I can display text on a pygame window using python?

I need to display a bunch of live information that updates and would rather not make an image for

相关标签:
5条回答
  • 2020-12-05 06:44

    You can use your own custom fonts by setting the font path using pygame.font.Font

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

    example:

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

    Then render the font using fontObj.render and blit to a surface as in veiset's answer above. :)

    0 讨论(0)
  • 2020-12-05 06:45

    There are 2 possibilities. In either case PyGame has to be initialized by pygame.init.

    import pygame
    pygame.init()
    

    Use either the pygame.font module and create a pygame.font.SysFont or pygame.font.Font object. render() a pygame.Surface with the text and blit the Surface to the screen:

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

    Or use the pygame.freetype module. Create a pygame.freetype.SysFont() or pygame.freetype.Font object. render() a pygame.Surface with the text or directly render_to() the text to the screen:

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

    See also Text and font


    Minimal pygame.font example: 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()
    

    Minimal pygame.freetype example: 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()
    
    0 讨论(0)
  • 2020-12-05 06:50

    I have some code in my game that displays live score. It is in a function for quick access.

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

    and I call it using this in my while loop:

    texts(score)
    
    0 讨论(0)
  • 2020-12-05 06:55

    Yes. It is possible to draw text in 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))
    
    0 讨论(0)
  • 2020-12-05 07:00

    I wrote a wrapper, that will cache text surfaces, only re-render when dirty. googlecode/ninmonkey/nin.text/demo/

    0 讨论(0)
提交回复
热议问题