2

I have some code that will blit text to the screen, but I can only align it to the, but I want it on the left. Here is what i have right now:

smallText = pygame.font.Font('freesansbold.ttf', 25)
textSurf, textRect = text_objects(coins, smallText, black)
textRect.center = ((displayWidth-50), (38))
gameDisplay.blit(textSurf, textRect)

This will align the center of the text to the coordinates given, but I want to be able to select the coordinates of the leftmost point, or top left corner. I tried textRect.left but it didn't work.

Before anyone says 'Google it', I already have, that's why I have come here.

Krystian O
  • 35
  • 1
  • 6

1 Answers1

0

Try one of the other rect attributes like midleft or topleft. I'm not quite sure which one you need because the question is a little unclear.

import pygame as pg

pg.init()

screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
FONT = pg.font.Font(None, 42)

text_surface = FONT.render('text', True, pg.Color('dodgerblue1'))
text_rect = text_surface.get_rect()
text_rect.midleft = (screen.get_width()-50, 38)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    screen.fill((30, 30, 30))
    screen.blit(text_surface, text_rect)
    pg.display.flip()
    clock.tick(30)

pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48