pybeamshow/effects/bouncingspot.py

70 lines
2.1 KiB
Python
Raw Normal View History

2023-02-15 21:08:47 +01:00
from typing import Any
import pygame as pg
from effects.effect import Effect, Colors, transform_bounce
2023-02-15 21:08:47 +01:00
import random
import math
2023-02-15 22:38:02 +01:00
from typing import Union, Generator
2023-02-15 21:08:47 +01:00
2023-02-17 02:08:38 +01:00
class BouncingSpot(Effect):
2023-02-15 21:08:47 +01:00
def __init__(
self,
bounds: pg.Rect,
2023-02-15 22:38:02 +01:00
color: Union[pg.Color, Generator[pg.Color, None, None]],
2023-02-15 21:08:47 +01:00
sizes=(10, 100),
velocity=(1, 10),
x_factor=(0.1, 1),
y_factor=(0.1, 1),
*groups: pg.sprite.Group
) -> None:
self.min_size = sizes[0]
self.max_size = sizes[1]
self.min_velocity = velocity[0]
self.max_velocity = velocity[1]
self.velocity = random.randint(self.min_velocity, self.max_velocity)
self.ticks = random.randint(0, 360)
2023-02-15 22:38:02 +01:00
self.color = color
2023-02-15 21:08:47 +01:00
size = (math.sin(self.ticks) / 2 + 0.5) * (
self.max_size - self.min_size
) + self.min_size
2023-02-17 02:05:54 +01:00
image = pg.Surface((self.max_size, self.max_size))
image.fill(Colors.Black)
image.set_colorkey(Colors.Black)
2023-02-15 21:08:47 +01:00
super().__init__(
image=image,
rect=pg.Rect(
bounds.centerx - size / 2,
bounds.centery - size / 2,
size,
size,
),
*groups
)
self.bounds = bounds
self.bouncer = transform_bounce(
bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor
)
next(self.bouncer)
2023-02-15 21:08:47 +01:00
self.update()
def update(self, *args: Any, **kwargs: Any) -> None:
new_size = (math.sin(self.ticks) / 2 + 0.5) * (
self.max_size - self.min_size
) + self.min_size
new_scale = new_size - self.rect.width
self.rect.inflate_ip(new_scale, new_scale)
self.rect.center = self.bouncer.send(self.rect.size)
2023-02-15 21:08:47 +01:00
2023-02-17 02:05:54 +01:00
self.image.fill(Colors.Black)
2023-02-15 22:38:02 +01:00
pg.draw.ellipse(
self.image,
self.color if isinstance(self.color, pg.Color) else next(self.color),
((0, 0), self.rect.size),
)
2023-02-15 21:08:47 +01:00
self.ticks += self.velocity / 180 * math.pi
self.velocity = random.randint(self.min_velocity, self.max_velocity)