Python Pygame文本锚定右侧

Python Pygame文本锚定右侧,python,python-3.x,pygame,Python,Python 3.x,Pygame,我可以在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

我可以在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
,因此您可以设置
。right
Rect()
计算正确的
.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)
顺便说一句:您可以从其他对象复制
右侧

# 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