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

View File

@@ -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

View File

@@ -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])