Failing to get a solution to my problem from other people, I got tired of waiting and wrote this (rather rushed) Text class to deal with the text problems I've been having in Pygame. I wanted the text to update itself, so that it would be unnecessary to keep track of its original position. After playing a lot with it, I found this solution:
class Text(object):
def __init__(self, value, size, color,
left_orientation=False,
font=None,
x=0, y=0,
top=None, bottom=None, left=None, right=None,
centerx=None, centery=None):
self._size = size
self._color = color
self._value = value
self._font = pygame.font.Font(font, self._size)
self.width, self.height = self._font.size(self._value)
self.left_orientation = left_orientation
self.image = self._create_surface()
self.rect = self.image.get_rect()
if x: self.rect.x = x
if y: self.rect.y = y
if top: self.rect.top = top
if bottom: self.rect.bottom = bottom
if left: self.rect.left = left
if right: self.rect.right = right
if centerx: self.rect.centerx = centerx
if centery: self.rect.centery = centery
def _create_surface(self):
return self._font.render(self._value, True, self._color)
def set_value(self, new_value):
if new_value != self._value:
self._value = new_value
self.image = self._create_surface()
new_rect = self.image.get_rect(x = self.rect.x, y = self.rect.y)
if self.left_orientation:
width_diff = new_rect.width - self.rect.width
new_rect.x = self.rect.x - width_diff
self.rect = new_rect
def set_position(self, x_or_x_and_y, y=None):
if y != None:
self.rect.x = x_or_x_and_y
self.rect.y = y
else:
self.rect.x = x_or_x_and_y[0]
self.rect.y = x_or_x_and_y[1]
So, if a text is supposed to increase to the left, all that I have to do is initialize a Text object with the left_orientation
parameter set to True
and, whatever the rect is, it will update itself to remain at it's original position.
Is this a good solution? If not, what would be a better one?