pybeamshow/effects/rectangle.py

126 lines
3.5 KiB
Python
Raw Normal View History

2023-03-05 01:46:59 +01:00
from effects.effect import MovingEffect
from typing import Any, Optional
from util import XYCoord
from util.color import Colors, ColorGenerator, color_fade, copy_color
import math
import pygame as pg
from util.transform import PositionGenerator
def calc_x(R, T, D):
a = 1 + R**2
b = 2 * T * (1 + R)
c = T**2 * (1 + R**2) - D**2
return max(
(-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a),
(-b - math.sqrt(b**2 - 4 * a * c)) / (2 * a),
)
class Rectangle(MovingEffect):
def __init__(
self,
bounds: pg.Rect,
color: ColorGenerator,
size: int,
aspect: float = 1.0,
rot_speed: float = 0.5,
thickness: int = 30,
mover: Optional[PositionGenerator] = None,
*groups: pg.sprite.Group,
) -> None:
self.color_gen = color
self.color = copy_color(next(color))
self.fader = color_fade(
initial_color=self.color, end_color=next(self.color_gen), duration=10
)
self.angle = 45.0
self.rot_speed = rot_speed
self.thickness = thickness
x = calc_x(aspect, thickness, size)
2023-03-05 02:37:18 +01:00
# print(x)
# print(size)
2023-03-05 01:46:59 +01:00
self.rsize = (x, (x + 2 * thickness) * aspect - 2 * thickness)
2023-03-05 02:37:18 +01:00
# print(self.rsize)
2023-03-05 01:46:59 +01:00
self.rect_image = pg.Surface(self.rsize)
self.rect_image.fill(Colors.Black)
self.rect_image.set_colorkey(Colors.Black)
image = pg.Surface((size, size))
image.fill(Colors.Black)
image.set_colorkey(Colors.Black)
super().__init__(
image,
pg.Rect(
bounds.centerx - size // 2,
bounds.centery - size // 2,
size,
size,
),
mover,
*groups,
)
self.update(is_beat=True)
def update(self, *args: Any, **kwargs: Any) -> None:
# self.rect.center = self.mover.send((self.rect.size, kwargs["is_beat"]))
self.image.fill(Colors.Blue)
self.rect_image.fill(Colors.Green)
# rc = next(self.fader)
# pg.draw.rect(
# self.rect_image,
# rc,
# ((0, 0), self.rsize),
# 0,
# )
# pg.draw.rect(
# self.rect_image,
# Colors.Black,
# (
# self.thickness // 2,
# self.thickness // 2,
# self.rsize[0] - self.thickness,
# self.rsize[1] - self.thickness,
# ),
# 0,
# )
# if kwargs["is_beat"]:
# new_color = next(self.color_gen)
# self.fader = color_fade(
# initial_color=self.color, end_color=new_color, duration=10
# )
# self.color = copy_color(new_color)
# pg.draw.rect(
# self.rect_image,
# Colors.White,
# (
# self.thickness,
# self.thickness,
# self.rsize[0] - self.thickness * 2,
# self.rsize[1] - self.thickness * 2,
# ),
# 0,
# )
r2 = pg.transform.rotate(self.rect_image, self.angle)
2023-03-05 02:37:18 +01:00
# print(self.image.get_width()-r2.get_width())
2023-03-05 01:46:59 +01:00
self.image.blit(
r2,
(
(self.image.get_width() - r2.get_width()) // 2,
(self.image.get_height() - r2.get_height()) // 2,
),
)
# self.angle = (self.angle + self.rot_speed) % (360)