pybeamshow/effects/bouncingspot.py

76 lines
2.2 KiB
Python
Raw Normal View History

2023-02-23 01:56:05 +01:00
from effects.effect import MovingEffect
from typing import Any, Optional
2023-02-24 18:21:21 +01:00
from util.color import Colors, ColorGenerator
2023-02-22 22:55:11 +01:00
import math
2023-02-15 21:08:47 +01:00
import pygame as pg
import random
2023-02-23 01:56:05 +01:00
from util.transform import PositionGenerator
2023-02-15 21:08:47 +01:00
2023-02-23 01:56:05 +01:00
class BouncingSpot(MovingEffect):
2023-02-15 21:08:47 +01:00
def __init__(
self,
bounds: pg.Rect,
2023-02-24 18:21:21 +01:00
color: ColorGenerator,
2023-02-15 21:08:47 +01:00
sizes=(10, 100),
velocity=(1, 10),
2023-02-23 01:56:05 +01:00
mover: Optional[PositionGenerator] = None,
2023-02-24 18:21:21 +01:00
on_beat_color: bool = False,
2023-02-15 21:08:47 +01:00
*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-24 18:21:21 +01:00
self.spot_color = next(color)
self.on_beat_color = on_beat_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__(
2023-02-18 23:17:03 +01:00
image,
pg.Rect(
2023-02-15 21:08:47 +01:00
bounds.centerx - size / 2,
bounds.centery - size / 2,
size,
size,
),
2023-02-23 01:56:05 +01:00
mover,
2023-02-15 21:08:47 +01:00
*groups
)
self.bounds = bounds
2023-02-22 22:55:11 +01:00
self.update(is_beat=False)
2023-02-15 21:08:47 +01:00
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
2023-02-23 01:56:05 +01:00
if kwargs["is_beat"]:
new_scale *= 1.5
2023-02-15 21:08:47 +01:00
self.rect.inflate_ip(new_scale, new_scale)
2023-02-23 01:56:05 +01:00
self.rect.center = self.mover.send((self.rect.size, kwargs["is_beat"]))
2023-02-15 21:08:47 +01:00
2023-02-24 18:21:21 +01:00
if (not self.on_beat_color) or kwargs["is_beat"]:
self.spot_color = next(self.color)
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,
2023-02-24 18:21:21 +01:00
self.spot_color,
2023-02-15 22:38:02 +01:00
((0, 0), self.rect.size),
)
2023-02-15 21:08:47 +01:00
2023-02-18 23:17:03 +01:00
self.ticks += int(self.velocity / 180 * math.pi)
2023-02-15 21:08:47 +01:00
self.velocity = random.randint(self.min_velocity, self.max_velocity)