import abc from typing import Optional import pygame from pygame import Surface from sprite.PositionScale import PositionScale class UiElement: def __init__(self): self.position_scale = PositionScale() self.visible = True @abc.abstractmethod def tick(self, dt: float): return @abc.abstractmethod def render_sprite_image(self) -> Optional[Surface]: return None def draw(self, screen: pygame.Surface, screen_transform: PositionScale): if not self.visible: return image = self.render_sprite_image() if image is not None: # target_scale = (screen_transform.scale[0] * self.position_scale.scale[0], # screen_transform.scale[1] * self.position_scale.scale[1]) # target_position = ((self.position_scale.position[0] + screen_transform.position[0]) * target_scale[0], # (self.position_scale.position[1] + screen_transform.position[1]) * target_scale[1]) # target_size = (int(target_scale[0] * image.get_width()), # int(target_scale[1] * image.get_height())) # target_image = self.get_scaled_image(image, target_size) # screen.blit(target_image, target_position) screen_scale = screen_transform.scale screen_position = screen_transform.position object_scale = self.position_scale.scale object_position = self.position_scale.position target_position = ((object_position[0] + screen_position[0]) * screen_scale[0], (object_position[1] + screen_position[1]) * screen_scale[1]) target_size = (int(screen_scale[0] * object_scale[0] * image.get_width()), int(screen_scale[1] * object_scale[1] * image.get_height())) target_image = self.get_scaled_image(image, target_size) screen.blit(target_image, target_position) def get_scaled_image(self, image, resize): return pygame.transform.scale(image, resize)