Pygame Text Anchor Right

时间:2015-12-01 05:17:34

标签: 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

我的功能:

    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()

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

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)

BTW:您可以从其他对象复制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