This commit is contained in:
Benjamín 2021-09-01 22:22:18 +02:00
parent b3c93e804a
commit 125e0e2831
5 changed files with 52 additions and 11 deletions

@ -4,8 +4,8 @@ from numpy import array as a
class Scene(Screen): class Scene(Screen):
def __init__(self, width: float, height: float, font_size: int, scene_size: a, bg=(60, 60, 60)): def __init__(self, width: float, height: float, scene_size: a, bg=(60, 60, 60)):
super().__init__(width, height, font_size, scene_size) super().__init__(width, height, scene_size)
self.bg = bg self.bg = bg
self.r_objects = [] # resizable Objects self.r_objects = [] # resizable Objects

@ -4,7 +4,7 @@ import pygame
class Screen: class Screen:
def __init__(self, width: float, height: float, font_size: int, screen_size: a): def __init__(self, width: float, height: float, screen_size: a):
pygame.init() pygame.init()
self.width = width self.width = width
@ -21,9 +21,14 @@ class Screen:
self.s_ = pygame.Surface([width, height]) self.s_ = pygame.Surface([width, height])
self.s = pygame.display.set_mode(screen_size) self.s = pygame.display.set_mode(screen_size)
self.font_size = font_size
self.center = a([width, height]) / 2 self.center = a([width, height]) / 2
def save(self, path): def save(self, path, size=None):
if size is None:
pygame.image.save(self.s, path) pygame.image.save(self.s, path)
else:
surface = pygame.Surface(size)
surface.blit(self.s, [0, 0])
pygame.image.save(surface, path)

@ -0,0 +1,32 @@
import pygame
class Text:
def __init__(self, position, text, font_size, color, scene, font_name="calibri", bold=True):
self.position = position
self.text = text
self.font_size = font_size
self.font_name = font_name
self.bold = bold
self.color = color
self.scene = scene
self.scalar = self.scene.matrix[0, 0]
self.font = pygame.font.SysFont(self.font_name, max(1, int(self.font_size * self.scalar)), self.bold)
self.rendered_text = self.font.render(self.text, False, self.color)
self.blit_position = self.scene.matrix @ self.position
self.blit_position[0] -= self.rendered_text.get_width() / 2
self.blit_position[1] -= self.rendered_text.get_height() / 2
def blit(self):
if self.scalar != self.scene.matrix[0, 0]:
self.scalar = self.scene.matrix[0, 0]
self.font = pygame.font.SysFont(self.font_name, max(1, int(self.font_size * self.scalar)), self.bold)
self.rendered_text = self.font.render(self.text, False, self.color)
self.blit_position = self.scene.matrix @ self.position
self.blit_position[0] -= self.rendered_text.get_width() / 2
self.blit_position[1] -= self.rendered_text.get_height() / 2
self.scene.s.blit(self.rendered_text, self.blit_position)

@ -6,15 +6,16 @@ import numpy as np
from UI.Objects.nr_objects.Ellipse import Ellipse from UI.Objects.nr_objects.Ellipse import Ellipse
from UI.Objects.nr_objects.Circle import Circle from UI.Objects.nr_objects.Circle import Circle
from UI.Objects.nr_objects.Line import Line from UI.Objects.nr_objects.Line import Line
from UI.Objects.nr_objects.Text import Text
from UI.Objects.r_objects.Rect import Rect from UI.Objects.r_objects.Rect import Rect
class BasicScene(Scene): class BasicScene(Scene):
def __init__(self, font_size: int, scene_size: a, bg=(60, 60, 60)): def __init__(self, scene_size: a, bg=(60, 60, 60)):
s__size = [600, 400] s__size = [600, 400]
super().__init__(s__size[0], s__size[1], font_size, scene_size, bg) super().__init__(s__size[0], s__size[1], scene_size, bg)
r0 = Rect(a([0, 0]), a([200, 200]), (160, 160, 160), self) r0 = Rect(a([0, 0]), a([200, 200]), (160, 160, 160), self)
self.r_objects.append(r0) self.r_objects.append(r0)
@ -27,3 +28,6 @@ class BasicScene(Scene):
e0 = Ellipse(a([150, 360]), 60, 20, (100, 255, 100), self) e0 = Ellipse(a([150, 360]), 60, 20, (100, 255, 100), self)
self.nr_objects.append(e0) self.nr_objects.append(e0)
t0 = Text(a([100, 50]), "hello world!", 26, (200, 200, 200), self)
self.nr_objects.append(t0)

@ -7,9 +7,9 @@ screen_size = a([200, 500])
for i in range(400): for i in range(400):
scene = BasicScene(10, screen_size) scene = BasicScene(screen_size)
scene.redraw() scene.redraw()
#scene.save(f"Render/{i}.png") # scene.save(f"Render/{i}.png", [800, 800])
if i < 150: if i < 150:
screen_size[0] += 3 screen_size[0] += 3