This commit is contained in:
Benjamín 2021-09-10 19:36:15 +02:00
parent 24b8fa0a58
commit 95fc749c6c
12 changed files with 129 additions and 161 deletions

@ -24,12 +24,25 @@ class Multiscene(Scene):
self.subscenes_prepared = True self.subscenes_prepared = True
def light_update(self):
for subscene in self.subscenes:
subscene.light_update()
for object_ in self.l_objects:
object_.update()
object_.blit()
pygame.display.update()
def update(self): def update(self):
if self.subscenes_prepared is False: if self.subscenes_prepared is False:
self.prepare_subscenes() self.prepare_subscenes()
self.s_.fill(self.bg) self.s_.fill(self.bg)
for object_ in self.l_objects:
object_.update()
for object_ in self.r_objects: for object_ in self.r_objects:
object_.blit() object_.blit()
@ -101,20 +114,6 @@ class Multiscene(Scene):
return i_objects return i_objects
def start_bgt_objects(self):
for object_ in self.bgt_objects:
object_.start_bg_activity()
for subscene in self.subscenes:
subscene.start_bg_activity()
def stop_bgt_objects(self):
for object_ in self.bgt_objects:
object_.stop_bg_activity()
for subscene in self.subscenes:
subscene.stop_bg_activity()
@staticmethod @staticmethod
def to_ints(iterable): def to_ints(iterable):
for i in range(len(iterable)): for i in range(len(iterable)):

@ -17,7 +17,7 @@ class Scene(Screen):
self.d_objects = [] # dynamic Objects self.d_objects = [] # dynamic Objects
self.c_objects = [] # controllable Objects self.c_objects = [] # controllable Objects
self.i_objects = [] # interactive Objects self.i_objects = [] # interactive Objects
self.bgt_objects = [] # background threaded Objects self.l_objects = [] # light Objects
self.position = a([0, 0]) self.position = a([0, 0])
self.multiscene = None self.multiscene = None
@ -25,9 +25,19 @@ class Scene(Screen):
self.mouse_pos = a([0, 0]) self.mouse_pos = a([0, 0])
self.clicked = False self.clicked = False
def light_update(self):
for object_ in self.l_objects:
object_.update()
object_.blit()
pygame.display.update()
def update(self): def update(self):
self.s_.fill(self.bg) self.s_.fill(self.bg)
for object_ in self.l_objects:
object_.update()
for object_ in self.r_objects: for object_ in self.r_objects:
object_.blit() object_.blit()
@ -59,14 +69,6 @@ class Scene(Screen):
def get_i_objects(self): def get_i_objects(self):
return [[self.i_objects, self.mouse_pos]] return [[self.i_objects, self.mouse_pos]]
def start_bgt_objects(self):
for object_ in self.bgt_objects:
object_.start_bg_activity()
def stop_bgt_objects(self):
for object_ in self.bgt_objects:
object_.stop_bg_activity()
def sort_objects(self, *args): def sort_objects(self, *args):
for object_ in args: for object_ in args:
for object_tag in object_.object_type.split("_"): for object_tag in object_.object_type.split("_"):
@ -80,8 +82,8 @@ class Scene(Screen):
self.c_objects.append(object_) self.c_objects.append(object_)
elif object_tag == "i": elif object_tag == "i":
self.i_objects.append(object_) self.i_objects.append(object_)
elif object_tag == "bgt": elif object_tag == "l":
self.bgt_objects.append(object_) self.l_objects.append(object_)
elif object_tag in ["scene", "multiscene"]: elif object_tag in ["scene", "multiscene"]:
if self.object_type == "multiscene": if self.object_type == "multiscene":
self.subscenes.append(object_) self.subscenes.append(object_)

@ -1,89 +0,0 @@
import copy
import pygame
import time
import numpy as np
from numpy import array as a
from threading import Thread
class RefreshingImage:
object_type = "nr_bgt"
def __init__(self, center, scalar, path, scene, rps=24):
self.center = center
self.scalar = scalar
self.path = path
self.rps = rps # refreshes per second
self.scene = scene
self.pd_ = self.scene.pd_
self.image = pygame.image.load(self.path).convert()
self.image = pygame.transform.scale(self.image,
self.to_ints((a(self.image.get_size()) * self.scalar).tolist()))
self.size = a(self.image.get_size(), dtype=np.float64)
self.sides_ratio = self.size[0] / self.size[1]
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
self.updater = None
self.updater_alive = True
self.start_bg_activity()
def start_bg_activity(self):
self.updater_alive = True
def update(self_):
dt = 1 / self_.rps
while self_.updater_alive:
if self_.pd_ == self_.scene.pd_:
try:
self_.image = pygame.image.load(self_.path).convert()
except:
pass
else:
self_.image = pygame.transform.scale(self_.image,
self_.to_ints((a(self_.image.get_size())
* self_.scalar).tolist()))
self_.rescaled_image = pygame.transform.scale(self_.image,
self_.to_ints((self_.size * self_.pd_).tolist()))
self_.scene.s.blit(self_.rescaled_image, self_.scene.matrix @ self_.center)
pygame.display.update()
time.sleep(dt)
self.updater = Thread(target=update, args=(self,))
self.updater.start()
def stop_bg_activity(self):
self.updater_alive = False
def blit(self):
if self.pd_ != self.scene.pd_:
self.pd_ = self.scene.pd_
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
self.scene.s.blit(self.rescaled_image, self.scene.matrix @ self.center)
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):
iterable[i] = int(iterable[i])
return iterable

@ -0,0 +1,67 @@
import math
import random
import numpy as np
from numpy import array
from pygame.draw import circle
class LuminousCircleEffect:
object_type = "nr_l"
def __init__(self, position, r, color, scene, circles_n=20, point_speed=0.02, movement_stability=5):
self.position = position
self.r = r
self.color = color
self.scene = scene
self.circles_n = circles_n
self.point_speed = point_speed
self.point_speed_inverse = 1 / self.point_speed
self.movement_stability = movement_stability
self.point_position = array([0., 0.])
self.point_velocity = array([0., 0.])
def update(self):
self.point_position += self.point_velocity
a = math.sqrt(sum(self.point_position ** 2))
if a > 1:
self.point_position /= a
a = 1
b = 1 - a
c = (0.5 * a) + b
d = math.sqrt((c ** 2) - ((a * 0.5) ** 2))
if a == 0:
rotation_point = complex(1, 0)
else:
normalized_point_position = self.point_position / a
rotation_point = complex(*normalized_point_position)
delta = random.uniform(0, math.tau)
m = max(random.uniform(0, 1), random.uniform(0, 1))
point_position = complex(math.cos(delta) * m * c, math.sin(delta) * m * d) * rotation_point
point_position = array([point_position.real, point_position.imag])
self.point_velocity *= self.movement_stability * self.point_speed_inverse
self.point_velocity += point_position - self.point_position
self.point_velocity /= math.sqrt(sum(self.point_velocity ** 2))
self.point_velocity *= self.point_speed
def blit(self):
rescaled_r = self.r * self.scene.pd_
transformed_position = self.scene.matrix @ self.position
x = np.linspace(0, self.point_position[0], self.circles_n) * rescaled_r
y = np.linspace(0, self.point_position[1], self.circles_n) * rescaled_r
r = np.linspace(rescaled_r, 0, self.circles_n)
c = [np.linspace(value, 255, self.circles_n) for value in self.color]
for i in range(self.circles_n - 1):
circle(self.scene.s, (c[0][i], c[1][i], c[2][i]),
transformed_position + array([x[i], y[i]]), r[i])

@ -0,0 +1,16 @@
from UI.Objects.Scene import Scene
from numpy import array as a
from UI.Objects.nr_objects.light_objects.LuminousCircleEffect import LuminousCircleEffect
class LuminousCircleEffectScene(Scene):
def __init__(self, screen_size, bg=(60, 60, 60)):
s__size = [600, 400]
super().__init__(*s__size, screen_size, bg)
ce0 = LuminousCircleEffect(a([300, 200]), 100, (80, 120, 80), self, circles_n=600)
self.sort_objects(ce0)

@ -1,16 +0,0 @@
from UI.Objects.Scene import Scene
from numpy import array as a
from UI.Objects.nr_objects.background_threaded_objects.RefreshingImage import RefreshingImage
class VideoScene(Scene):
def __init__(self, screen_size, bg=(60, 60, 60)):
s__size = [1200, 800]
super().__init__(*s__size, screen_size, bg)
ri0 = RefreshingImage(a([100, 100]), 1.2, "video_capture.png", self)
self.sort_objects(ri0)

17
UI/light_update_sample.py Normal file

@ -0,0 +1,17 @@
from numpy import array as a
import time
from UI.Scenes.LuminousCircleEffectScene import LuminousCircleEffectScene
screen_size = a([1000, 800])
scene = LuminousCircleEffectScene(screen_size)
scene.update()
for i in range(400):
scene.update()
scene.save(f"Render/{i}.png", screen_size)
time.sleep(0.05)

@ -5,18 +5,17 @@ from UI.Scenes.BasicMultiscene import BasicMultiscene
from UI.Scenes.MultisceneInMultiscene import MultisceneInMultiscene from UI.Scenes.MultisceneInMultiscene import MultisceneInMultiscene
from UI.Scenes.RopeScene import RopeScene from UI.Scenes.RopeScene import RopeScene
from UI.Scenes.RopeMultiscene import RopeMultiscene from UI.Scenes.RopeMultiscene import RopeMultiscene
from UI.Scenes.VideoScene import VideoScene from UI.Scenes.LuminousCircleEffectScene import LuminousCircleEffectScene
from numpy import array as a from numpy import array as a
import time import time
screen_size = a([200, 500]) screen_size = a([200, 500])
scene = VideoScene(screen_size) scene = LuminousCircleEffectScene(screen_size)
mouse_pos = [0, 0] mouse_pos = [0, 0]
clicked = False clicked = False
for i in range(400): for i in range(400):
scene.resize_screen(screen_size) scene.resize_screen(screen_size)
scene.update() scene.update()
@ -32,6 +31,4 @@ for i in range(400):
pass pass
screen_size -= 4 screen_size -= 4
time.sleep(0.01) time.sleep(0.1)
scene.stop_bgt_objects()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 350 KiB

@ -1,19 +0,0 @@
from UI.Scenes.VideoScene import VideoScene
from numpy import array as a
import time
screen_size = a([1200, 800])
scene = VideoScene(screen_size)
mouse_pos = [0, 0]
clicked = False
scene.update()
try:
time.sleep(20)
except KeyboardInterrupt:
pass
scene.stop_bgt_objects()

@ -81,12 +81,6 @@ class Hands_AI:
else: else:
self.x, self.y = None, None self.x, self.y = None, None
cv2.imshow("Image", img) cv2.imshow("Image", img)
cv2.imwrite("UI/video_capture_.png", img)
try:
os.remove("UI/video_capture.png")
os.rename("UI/video_capture_.png", "UI/video_capture.png")
except:
pass
cv2.waitKey(1) cv2.waitKey(1)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 357 KiB