Add oscillating transform

This commit is contained in:
Patrick Moessler 2023-02-19 16:02:28 +01:00
parent 337b9a2385
commit 0e567b36fd

View file

@ -71,6 +71,41 @@ def transform_bounce(
size_x, size_y = yield (pos_x, pos_y)
def transform_oscillate(
bounds: pg.Rect,
period: int,
initial_pos: Tuple[int, int] = (-1, -1),
) -> Generator[Tuple[int, int], Tuple[int, int], None]:
pos_x = float(initial_pos[0] if initial_pos[0] > 0 else bounds.left)
pos_y = float(initial_pos[1] if initial_pos[1] > 0 else bounds.top)
direction = "+"
size_x, size_y = yield (bounds.left, bounds.top)
while True:
range_x = bounds.width - size_x
range_y = bounds.height - size_y
inc_x = range_x / period
inc_y = range_y / period
if direction == "+":
pos_x = pg.math.clamp(pos_x + inc_x, 0, range_x)
pos_y = pg.math.clamp(pos_y + inc_y, 0, range_y)
else:
pos_x = pg.math.clamp(pos_x - inc_x, 0, range_x)
pos_y = pg.math.clamp(pos_y - inc_y, 0, range_y)
if (inc_x and (pos_x > range_x - inc_x)) or (
inc_y and (pos_y > range_y - inc_y)
):
direction = "-"
elif (inc_x and (pos_x < inc_x)) or (inc_y and (pos_y < inc_y)):
direction = "+"
size_x, size_y = yield (int(pos_x), int(pos_y))
class Effect(pg.sprite.Sprite):
def __init__(
self, image: pg.Surface, rect: pg.Rect, *groups: pg.sprite.Group