From 5324cfb18615bdd0d0a26a910540e1fbdf078a69 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Thu, 23 Feb 2023 00:19:20 +0100 Subject: [PATCH] use mover for moonflower --- effects/effect.py | 11 ++++++++--- effects/moonflower.py | 35 +++++++++++++---------------------- effects/presets.py | 10 +++++++--- util/transform.py | 15 ++++++++++----- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/effects/effect.py b/effects/effect.py index c40bcd7..905a1e6 100644 --- a/effects/effect.py +++ b/effects/effect.py @@ -1,6 +1,7 @@ +from typing import Optional import pygame as pg -from util.transform import Mover +from util.transform import PositionGenerator, transform_static class Effect(pg.sprite.Sprite): @@ -17,7 +18,11 @@ class Effect(pg.sprite.Sprite): class MovingEffect(Effect): def __init__( - self, image: pg.Surface, rect: pg.Rect, mover: Mover, *groups: pg.sprite.Group + self, + image: pg.Surface, + rect: pg.Rect, + mover: Optional[PositionGenerator] = None, + *groups: pg.sprite.Group ) -> None: super().__init__(image, rect, *groups) - self.mover = mover + self.mover = mover or transform_static(rect.center) diff --git a/effects/moonflower.py b/effects/moonflower.py index 9af2503..e18f624 100644 --- a/effects/moonflower.py +++ b/effects/moonflower.py @@ -1,12 +1,12 @@ -from typing import Any, Tuple +from typing import Any, Optional, Tuple import pygame as pg -from effects.effect import Effect +from effects.effect import MovingEffect from util.color import Colors -from util.transform import transform_bounce -import random import math from typing import Union, Generator +from util.transform import PositionGenerator + def calc_radii(size, f): r_c = size / (5 * f + 2) @@ -14,7 +14,7 @@ def calc_radii(size, f): return r_c, r_a -class Moonflower(Effect): +class Moonflower(MovingEffect): def __init__( self, bounds: pg.Rect, @@ -22,28 +22,20 @@ class Moonflower(Effect): Union[pg.Color, Generator[pg.Color, None, None]], Union[pg.Color, Generator[pg.Color, None, None]], ], + mover: Optional[PositionGenerator] = None, beat_color: bool = False, size: int = 100, - velocity: Tuple[int, int] = (1, 10), rot_speed: float = 5, outer: int = 5, - x_factor: Tuple[float, float] = (0.1, 1), - y_factor: Tuple[float, float] = (0.1, 1), *groups: pg.sprite.Group ) -> None: - self.min_velocity = velocity[0] - self.max_velocity = velocity[1] - self.rot_speed = rot_speed self.rotation = 0.0 - self.velocity = random.randint(self.min_velocity, self.max_velocity) - # self.ticks = random.randint(0, 360) self.colors = colors self.beat_color = beat_color self.o_count = outer self.o_f = 1 / math.sin(math.pi / self.o_count) - self.spot_radius, self.o_radius = calc_radii(size, self.o_f) self.i_radius = (self.o_radius - self.spot_radius) * 0.5 @@ -58,13 +50,14 @@ class Moonflower(Effect): size, size, ), - *groups + mover, + *groups, ) self.bounds = bounds - self.bouncer = transform_bounce( - bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor, on_beat_random_phase=True - ) - next(self.bouncer) + # self.bouncer = transform_bounce( + # bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor, on_beat_random_phase=True + # ) + next(self.mover) self.o_color = ( self.colors[0] if isinstance(self.colors[0], pg.Color) @@ -78,7 +71,7 @@ class Moonflower(Effect): self.update(is_beat=False) def update(self, *args: Any, **kwargs: Any) -> None: - self.rect.center = self.bouncer.send((self.rect.size, kwargs["is_beat"])) + self.rect.center = self.mover.send((self.rect.size, kwargs["is_beat"])) self.image.fill(Colors.Black) @@ -106,5 +99,3 @@ class Moonflower(Effect): ) self.rotation += self.rot_speed - # self.ticks += int(self.velocity / 180 * math.pi) - self.velocity = random.randint(self.min_velocity, self.max_velocity) diff --git a/effects/presets.py b/effects/presets.py index a474441..49c6381 100644 --- a/effects/presets.py +++ b/effects/presets.py @@ -17,6 +17,7 @@ from effects.rotatingpoly import RotatingPoly from effects.scanreticle import ScanReticle from effects.spiro import Spiro from effects.starfield import Starfield +from util.transform import transform_bounce class Presets: @@ -124,10 +125,13 @@ class Presets: beat_color=self.beat_reactive, size=self.bounds.height // 4, outer=5, - velocity=(1, 1), rot_speed=1.5, - x_factor=(1, 1), - y_factor=(2.2, 2.2), + mover=transform_bounce( + bounds=self.bounds, + velocity=(0.5, 1.5), + x_factor=(0.5, 1.5), + y_factor=(0.5, 1.5), + ), ) ] diff --git a/util/transform.py b/util/transform.py index 1fbf751..a8cccbb 100644 --- a/util/transform.py +++ b/util/transform.py @@ -1,12 +1,16 @@ import math import random -from typing import Callable, Generator, Optional, Tuple +from typing import Generator, Optional, Tuple import pygame as pg PositionGenerator = Generator[ Tuple[int, int], Tuple[Tuple[int, int], Optional[bool]], None ] -Mover = Callable[(...), PositionGenerator] + + +def transform_static(position: Tuple[int, int]) -> PositionGenerator: + while True: + _ = yield (position) def transform_bounce( @@ -14,7 +18,7 @@ def transform_bounce( velocity: Tuple[float, float], x_factor: Tuple[float, float], y_factor: Tuple[float, float], - on_beat_random_phase: bool = False, + on_beat_random_phase: int = 0, ) -> PositionGenerator: min_velocity = velocity[0] max_velocity = velocity[1] @@ -36,9 +40,10 @@ def transform_bounce( + bounds.centery ) - phase += current_velocity / 180 * math.pi + inc = current_velocity if on_beat_random_phase and is_beat: - phase += random.randrange(0, 360) + inc += random.randrange(0, on_beat_random_phase) + phase += inc / 180 * math.pi def transform_oscillate(