Add bouncing spot based on transform helper
This commit is contained in:
parent
fec4f0334a
commit
5a28453d2e
1 changed files with 8 additions and 12 deletions
|
@ -1,12 +1,12 @@
|
||||||
from typing import Any
|
from typing import Any
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
from effects.effect import Effect, Colors
|
from effects.effect import Effect, Colors, transform_bounce
|
||||||
import random
|
import random
|
||||||
import math
|
import math
|
||||||
from typing import Union, Generator
|
from typing import Union, Generator
|
||||||
|
|
||||||
|
|
||||||
class BouncingSpot(Effect):
|
class BouncingSpot2(Effect):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
bounds: pg.Rect,
|
bounds: pg.Rect,
|
||||||
|
@ -24,8 +24,6 @@ class BouncingSpot(Effect):
|
||||||
|
|
||||||
self.velocity = random.randint(self.min_velocity, self.max_velocity)
|
self.velocity = random.randint(self.min_velocity, self.max_velocity)
|
||||||
self.ticks = random.randint(0, 360)
|
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
|
self.color = color
|
||||||
size = (math.sin(self.ticks) / 2 + 0.5) * (
|
size = (math.sin(self.ticks) / 2 + 0.5) * (
|
||||||
self.max_size - self.min_size
|
self.max_size - self.min_size
|
||||||
|
@ -44,6 +42,10 @@ class BouncingSpot(Effect):
|
||||||
*groups
|
*groups
|
||||||
)
|
)
|
||||||
self.bounds = bounds
|
self.bounds = bounds
|
||||||
|
self.bouncer = transform_bounce(
|
||||||
|
bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor
|
||||||
|
)
|
||||||
|
next(self.bouncer)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def update(self, *args: Any, **kwargs: Any) -> None:
|
def update(self, *args: Any, **kwargs: Any) -> None:
|
||||||
|
@ -53,14 +55,8 @@ class BouncingSpot(Effect):
|
||||||
|
|
||||||
new_scale = new_size - self.rect.width
|
new_scale = new_size - self.rect.width
|
||||||
self.rect.inflate_ip(new_scale, new_scale)
|
self.rect.inflate_ip(new_scale, new_scale)
|
||||||
self.rect.centerx = (
|
|
||||||
0.4 * math.cos(self.x_factor * self.ticks) * self.bounds.width
|
self.rect.center = self.bouncer.send(self.rect.size)
|
||||||
+ self.bounds.centerx
|
|
||||||
)
|
|
||||||
self.rect.centery = (
|
|
||||||
0.4 * math.sin(self.y_factor * self.ticks) * self.bounds.height
|
|
||||||
+ self.bounds.centery
|
|
||||||
)
|
|
||||||
|
|
||||||
self.image.fill(Colors.Black)
|
self.image.fill(Colors.Black)
|
||||||
pg.draw.ellipse(
|
pg.draw.ellipse(
|
||||||
|
|
Loading…
Reference in a new issue