From 5a28453d2e09f87e865816f77d63437b1286cf9b Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Fri, 17 Feb 2023 02:07:06 +0100 Subject: [PATCH] Add bouncing spot based on transform helper --- effects/bouncingspot.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/effects/bouncingspot.py b/effects/bouncingspot.py index 6bbd479..ba6ab19 100644 --- a/effects/bouncingspot.py +++ b/effects/bouncingspot.py @@ -1,12 +1,12 @@ from typing import Any import pygame as pg -from effects.effect import Effect, Colors +from effects.effect import Effect, Colors, transform_bounce import random import math from typing import Union, Generator -class BouncingSpot(Effect): +class BouncingSpot2(Effect): def __init__( self, bounds: pg.Rect, @@ -24,8 +24,6 @@ class BouncingSpot(Effect): self.velocity = random.randint(self.min_velocity, self.max_velocity) self.ticks = random.randint(0, 360) - self.x_factor = random.uniform(x_factor[0], x_factor[1]) - self.y_factor = random.uniform(y_factor[0], y_factor[1]) self.color = color size = (math.sin(self.ticks) / 2 + 0.5) * ( self.max_size - self.min_size @@ -44,6 +42,10 @@ class BouncingSpot(Effect): *groups ) self.bounds = bounds + self.bouncer = transform_bounce( + bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor + ) + next(self.bouncer) self.update() def update(self, *args: Any, **kwargs: Any) -> None: @@ -53,14 +55,8 @@ class BouncingSpot(Effect): new_scale = new_size - self.rect.width self.rect.inflate_ip(new_scale, new_scale) - self.rect.centerx = ( - 0.4 * math.cos(self.x_factor * self.ticks) * self.bounds.width - + self.bounds.centerx - ) - self.rect.centery = ( - 0.4 * math.sin(self.y_factor * self.ticks) * self.bounds.height - + self.bounds.centery - ) + + self.rect.center = self.bouncer.send(self.rect.size) self.image.fill(Colors.Black) pg.draw.ellipse(