How to Center Text in Pygame

I have some code:

# draw text
font = pygame.font.Font(None, 25)
text = font.render("You win!", True, BLACK)
screen.blit(text, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2])

How can I get the text's width and height, so I can center text like this:

screen.blit(text, [SCREEN_WIDTH / 2 - text_w / 2, SCREEN_HEIGHT / 2 - text_h / 2])

If this is not possible, what is another way ? I've found this example, but I didn't really understand it.


Solution 1:

You can always just center the text rectangle when you grab it:

# draw text
font = pygame.font.Font(None, 25)
text = font.render("You win!", True, BLACK)
text_rect = text.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
screen.blit(text, text_rect)

just another option

Solution 2:

You can get the dimensions of the rendered text image using text.get_rect(), which returns a Rect object with width and height attributes, among others (see the linked documentation for a full list). I.e. you can simply do text.get_rect().width.

Solution 3:

To simplify the use of text, I use this function (to center it the x is not useful)

import pygame

pygame.init()
WIDTH = HEIGHT = 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
font = pygame.font.SysFont("Arial", 14)


def write(text, x, y, color="Coral",):
    text = font.render(text, 1, pygame.Color(color))
    text_rect = text.get_rect(center=(WIDTH//2, y))
    return text, text_rect

text, text_rect = write("Hello", 10, 10) # this will be centered anyhow, but at 10 height
loop = 1
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
    screen.blit(text, text_rect)
    pygame.display.update()

pygame.quit()

enter image description here

Solution 4:

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).

Get the text rectangle and place the center of the text rectangle on the center of the window rectangle:

text_rect = text.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))

You can even get the center of the window from the display Surface :

text_rect = text.get_rect(center = screen.get_rect().center)

Or with the use of pygame.display.get_surface():

text_rect = text.get_rect(center = pygame.display.get_surface().get_rect().center)

A Surface can be drawn on another Surface using the blit method. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. Therefore you can pass the text rectangle directly to blit:

screen.blit(text, text_rect)

Minimal example:

import pygame
import pygame.font

pygame.init()
font = pygame.font.SysFont(None, 50)
text = font.render('Hello World', True, (255, 0, 0))

window = pygame.display.set_mode((300, 100))
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)
    window.blit(text, text.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()