简体   繁体   中英

python and pygame font centering is working but I don't know why

My code is working perfectly although I'm completely in the dark as to why.

my code is:

def textobjects(message, font,color):
    Textsurface = font.render(message, True, color)
    return Textsurface, Textsurface.get_rect()

def Display_text(message,color):
    Heading = pygame.font.SysFont("Arial",70)
    TextShown, Textrect = textobjects(message, Heading,color)
    Textrect.center = ((displaywidth/2),0)
    gameWindow.blit(TextShown,Textrect)
    pygame.display.update()
    time.sleep(2)
    game_loop()


def border():
    Display_text("You hit the border",wine)

and I don't know why I was able to do:

Textrect.center = ((displaywidth/2),0)

without having to define ".center". Is .center a pygame/python predefined module?

.center is a part of a rect object that you can both get and set

You can also do

Rect.left
Rect.top
Rect.right
Rect.bottom
Rect.topleft
Rect.bottomleft
Rect.topright
Rect.bottomright
Rect.centerx
Rect.centery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM