How to display text in pygame? [duplicate] How to display text in pygame? [duplicate] python python

How to display text in pygame? [duplicate]


You can create a surface with text on it. For this take a look at this short example:

pygame.font.init() # you have to call this at the start,                    # if you want to use this module.myfont = pygame.font.SysFont('Comic Sans MS', 30)

This creates a new object on which you can call the render method.

textsurface = myfont.render('Some Text', False, (0, 0, 0))

This creates a new surface with text already drawn onto it.At the end you can just blit the text surface onto your main screen.

screen.blit(textsurface,(0,0))

Bear in mind, that everytime the text changes, you have to recreate the surface again, to see the new text.


There's also the pygame.freetype module which is more modern, works with more fonts and offers additional functionality.

Create a font object with pygame.freetype.SysFont() or pygame.freetype.Font if the font is inside of your game directory.

You can render the text either with the render method similarly to the old pygame.font.Font.render or directly onto the target surface with render_to.

import pygameimport pygame.freetype  # Import the freetype module.pygame.init()screen = pygame.display.set_mode((800, 600))GAME_FONT = pygame.freetype.Font("your_font.ttf", 24)running =  Truewhile running:    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False    screen.fill((255,255,255))    # You can use `render` and then blit the text surface ...    text_surface, rect = GAME_FONT.render("Hello World!", (0, 0, 0))    screen.blit(text_surface, (40, 250))    # or just `render_to` the target surface.    GAME_FONT.render_to(screen, (40, 350), "Hello World!", (0, 0, 0))    pygame.display.flip()pygame.quit()


When displaying I sometimes make a new file called Funk. This will have the font, size etc. This is the code for the class:

import pygamedef text_to_screen(screen, text, x, y, size = 50,            color = (200, 000, 000), font_type = 'data/fonts/orecrusherexpand.ttf'):    try:        text = str(text)        font = pygame.font.Font(font_type, size)        text = font.render(text, True, color)        screen.blit(text, (x, y))    except Exception, e:        print 'Font Error, saw it coming'        raise e

Then when that has been imported when I want to display text taht updates E.G score I do:

Funk.text_to_screen(screen, 'Text {0}'.format(score), xpos, ypos)

If it is just normal text that isn't being updated:

Funk.text_to_screen(screen, 'Text', xpos, ypos)

You may notice {0} on the first example. That is because when .format(whatever) is used that is what will be updated. If you have something like Score then target score you'd do {0} for score then {1} for target score then .format(score, targetscore)