def set_text(self, text): self.text = text arr = [] max_width = 0 height = 0 for i in text.split('\n'): image = self.font.render(i, True, self.color) arr.append(image) max_width = max(max_width, image.get_width()) height += image.get_height() height += self.line_spacing * (len(arr) - 1) image = get_surface(max_width, height) height = 0 for i in arr: x = 0 if self.align == TextAlign.LEFT: x = 0 elif self.align == TextAlign.CENTER: x = (max_width - i.get_width()) // 2 elif self.align == TextAlign.RIGHT: x = max_width - i.get_width() image.blit(i, (x, height)) height += i.get_height() + self.line_spacing self.width = image.get_width() self.height = image.get_height() self.set_image(image)
def update_surface(self): self.bar.set_current(self.current) image = get_surface(self.width, self.height) image.blit(self.icon, (0, (self.height - self.icon_height) // 2)) image.blit(self.bar.get_image(), (self.icon_width + self.icon_bar_offset, (self.height - self.bar.height) // 2)) self.set_image(image) self.update_position()
def __init__(self, scene, a: float, b: float, radius: float, *groups): super().__init__(scene, a - radius, b - radius, *groups, render_level=1) self.a = a self.b = b self.radius = radius self.width = int(Float(2 * self.radius)) self.height = int(Float(2 * self.radius)) image_red = get_surface(self.width, self.height) image_green = get_surface(self.width, self.height) for x in range(self.width): for y in range(self.height): if circle_and_rect_collide((Float(x + self.x), Float(y + self.y), Float(x + self.x + 1), Float(y + self.y + 1)), (self.a, self.b, self.radius)): image_red.set_at((x, y), Color.RED) image_green.set_at((x, y), Color.GREEN) dct = { 'none': ((image_red, 1),), 'detected': ((image_green, 30), (image_red, 30)) } self.animation.override_animations(dct)
def update_image(self): image = get_surface(self.width, self.height) image.fill(self.color.border) offset_x, offset_y = self.border_size image.fill(self.color.background, (offset_x, offset_y, self.width - 2 * offset_x, self.height - 2 * offset_y)) cnt = self.width - 2 * offset_x cnt = int(Float(ceil(cnt * self.current / self.max))) image.fill(self.color.full, (offset_x, offset_y, cnt, self.height - 2 * offset_y)) self.image = image
def update_surface(self): image = get_surface(self.width, self.height) for i in range(self.max): if i < self.current: img = self.full_image else: img = self.empty_image x = (Properties.WIDTH + Properties.OFFSET_X) * (i % self.column_count) y = (Properties.HEIGHT + Properties.OFFSET_Y) * (i // self.column_count) image.blit(img, (x, y)) self.set_image(image) self.update_position()
def __init__(self, width: int, height: int, color: BarIndicatorColor = None, border_size: tuple = (4, 4), max_value=100, current=0): self.width = width self.height = height if color is None: color = BarIndicatorColor((10, 10, 10), (100, 100, 100), (255, 0, 0), (0, 0, 0)) self.color = color self.max = max_value self.border_size = border_size self.current = 0 self.image = get_surface(self.width, self.height) self.set_current(current)
class GameObject(Sprite): WIDTH = 0 HEIGHT = 0 ANIMATION = get_surface(10, 10) GROUP_NAME = 'main' def __init__(self, scene: Scene, x: float, y: float, *groups: AbstractGroup, render_level=0, width=None, height=None): self.scene = scene self.x = x self.y = y self.width = width self.height = height if width is None: self.width = int(self.WIDTH) if height is None: self.height = int(self.HEIGHT) self.render_level = render_level if type(self.ANIMATION) is not Animation: self.animation = Animation(self, self.ANIMATION) else: self.animation = self.ANIMATION super().__init__(*groups) def get_render_rect(self): return pg.Rect(int(Float(self.x)), int(Float(self.y)), self.width, self.height) def get_render_image(self): return self.animation.get_current_image() def set_image(self, image): self.animation.override_animation('none', image) def get_width(self): return self.width def get_height(self): return self.height def set_x(self, value): self.x = Float(value) def set_x1(self, value): self.set_x(value) def set_middle_x(self, value): self.set_x(value - self.get_width() / 2) def set_x2(self, value): self.set_x(value - self.get_width()) def set_y(self, value): self.y = Float(value) def set_y1(self, value): self.set_y(value) def set_middle_y(self, value): self.set_y(value - self.get_height() / 2) def set_y2(self, value): self.set_y(value - self.get_height()) def get_x(self): return Float(self.x) def get_x1(self): return self.get_x() def get_middle_x(self): return Float((self.get_x1() + self.get_x2()) / 2) def get_x2(self): return Float(self.x + self.width) def get_y(self): return Float(self.y) def get_y1(self): return self.get_y() def get_middle_y(self): return Float((self.get_y1() + self.get_y2()) / 2) def get_y2(self): return Float(self.y + self.height) def print_position(self): print('Position: X =', self.get_x(), ' Y =', self.get_y()) def get_rect(self): return self.get_x1(), self.get_y1(), self.get_x2(), self.get_y2() def show(self): self.add(self.scene.to_render[self.render_level]) def hide(self): self.remove(self.scene.to_render[self.render_level]) def collide_with(self, obj): return is_intersection(self.get_rect(), obj.get_rect()) def update(self, *args, **kwargs) -> None: self.rect = self.get_render_rect() self.animation.update() self.image = self.get_render_image() super().update(*args, **kwargs) def process_logic(self, events): pass
class Air(StaticLevelObject): WIDTH = Cell.WIDTH HEIGHT = Cell.HEIGHT ANIMATION = get_surface(WIDTH, HEIGHT) GROUP_NAME = 'turret'