Pygame 右文本锚点

Pygame Text Anchor Right

我可以在 pygame 的文本中放置右锚点吗?像这样:

|"Text justify left."|  #(default in pygame)
|"Text justify right"|

#and when delete the word "Text", text stay in the right position:

|" justify left."| #anchor in left (default)
    |" justify right"| #anchor in right

我的功能:

    def messageScreen(self, message = "Default message", color = (0, 0, 0), pos = [50, 50]):
        screenText = self.font.render(message, True, color)
        self.screen.blit(screenText, pos)

    self.messageScreen("my text", (200, 100, 0))
    pygame.display.update()

有办法吗?

blit() 可以使用 pygame.Rect() 作为对象位置。 pygame.Rect() 有属性 .right 所以你可以设置 .rightRect() 计算正确 .x

text = self.font.render(message, True, color)
text_rect = text.get_rect()
text_rect.right = 150 # align to right to 150px

self.screen.blit(text, text_rect)

顺便说一句:您可以从其他对象复制 right

# first text draw normally

text_1 = self.font.render(message_1, True, color)
text_1_rect = text_1.get_rect()

# second text

text_2 = self.font.render(message_2, True, color)
text_2_rect = text_2.get_rect()

text_2_rect.right = text_1_rect.right # align to right side of first text