sep-pm-platformer/project/ui_elements/TextLabel.py

36 lines
1.3 KiB
Python

import pygame
from pygame.examples.textinput import Screen
from pygame.font import Font
from sprite.PositionScale import PositionScale
class TextLabel:
# pygame.font.Font('data/font/arcade.TTF')
def __init__(self, text: str, x_position: float, y_position: float, font_size: float, font: Font,
alignment: str = "left"):
self.text = text
self.x_position = x_position
self.y_position = y_position
self.alignment = alignment
self.current_width = 0
self.current_height = 0
self.font_size = font_size
self.font = font
self.position_scale = PositionScale()
def draw(self, screen: Screen, screen_transform: PositionScale):
rendered_font = self.font.render(str(self.text), True, (255, 255, 255))
self.current_width = rendered_font.get_width()
self.current_height = rendered_font.get_height()
if self.alignment == "right":
screen.blit(rendered_font, (self.x_position - self.current_width / 2, self.y_position))
elif self.alignment == "right":
screen.blit(rendered_font, (self.x_position, self.y_position))
elif self.alignment == "center":
screen.blit(rendered_font, (self.x_position - self.current_width, self.y_position))
def set_text(self, new_text: str):
self.text = new_text