Search code examples
pythonpygame

How to display text with font and color using pygame?


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 each character I need.

Can I blit text to the screen?


Solution

  • 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))