How to Center Text in Pygame

How to Center Text in Pygame

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.

How do you center text being displayed with pygame?

You should first get the text rectangle after rendering it, then center the rectangle relative to the screen width. Afterwhich you can pass it into blit.

centerTitle = title.get_rect(center=(WIDTH/2,35))
window.blit(title, centerTitle)

Pygame - center text in moving rect

Use pygame.Surface.get_rect to get the text rectangle. pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0). Set the center of the text rectangle by the center of the square. Use the text reectnagle to blit the text. The second argument of blit is either a tuple (x, y) 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:

class sqr:
# [...]

def numberTextFunc(self, X, Y):
numberText = self.numberFont.render(f"{self.number}", True, (87, 63, 63))
rect = pygame.Rect(X, Y, 120, 120)
textRect = numberText.get_rect(center = rect.center)
screen.blit(numberText, textRect)

You can simplify your code. X and Y are attributes of the object. Hence there is no need to pass the coordinates to the methods:

class sqr:
def __init__(self):
self.colours = [(255,0,0), (0,255,0), (0,0,255)]
self.Xpositions = [0,125,245,365,485]
self.OnScreen = False
self.X = random.choice(self.Xpositions)
self.Y = 0
self.colour = random.choice(self.colours)
self.number = random.choice([20,40,80])
self.numberFont = pygame.font.Font("TitilliumWeb-Black.ttf", 48)

def drawSquare(self, colour):
pygame.draw.rect(screen, colour, (self.X, self.Y, 120, 120))

def numberTextFunc(self):
numberText = self.numberFont.render(f"{self.number}", True, (87, 63, 63))
rect = pygame.Rect(self.X, self.Y, 120, 120)
textRect = numberText.get_rect(center = rect.center)
screen.blit(numberText, textRect)
gameRunning = True
while gameRunning:

background = screen.fill((0,100,140))

#draw lines for grid
for i in range(5):
pygame.draw.rect(screen, (0,0,0), (line[i], 0 , 5, 800))

for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False

#square
square.drawSquare(square.colour)
square.numberTextFunc()
square.OnScreen = True

square.Y += 1
if square.Y >= 680:
square.Y = 680

pygame.display.update()

Pygame: Centering text system font text

Use pygame.freetype.Font.get_rect to get a pygame.Rect object with the size of the text. Note, freetype.Font and freetype.SysFont have the same interface:

text = "Hello World"
text_size = 50
text_rect = font.get_rect(text, size = text_size)
text_rect.center = surface.get_rect().center

font.render_to(surface, text_rect, text, color, size = text_size)

Minimal examaple:

Sample Image

import pygame
import pygame.freetype

pygame.init()
window = pygame.display.set_mode((400, 200))

def drawTextCentered(surface, text, text_size, color):
text_rect = font.get_rect(text, size = 50)
text_rect.center = surface.get_rect().center
font.render_to(surface, text_rect, text, color, size = 50)

font = pygame.freetype.SysFont("comicsansms", 0)

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

window.fill(0)
drawTextCentered(window, "Hello World", 50, (255, 0, 0))
pygame.display.flip()

pygame.quit()
exit()

How do I only center text on x-axis, but be able to move the y-axis in pygame?

The pygame.Rect object has the following attributes:

x, y 
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

So you could set the centerx to the screen's centerx to make the rectangle centered without affecting the y value.

screen.blit(text, text.get_rect(centerx=screen_rect.centerx))

For completion, if you want it centered at the top, you could write

screen.blit(text, text.get_rect(midtop=screen_rect.midtop)

and centered at the bottom

screen.blit(text, text.get_rect(midbottom=screen_rect.midbottom)


Related Topics



Leave a reply



Submit