pybeamshow/effects/scanreticle.py
2023-02-23 01:56:05 +01:00

73 lines
2 KiB
Python

from typing import Any, Optional, Tuple
import pygame as pg
from effects.effect import MovingEffect
from util.color import Colors
from typing import Union, Generator
from util.transform import PositionGenerator
class ScanReticle(MovingEffect):
def __init__(
self,
bounds: pg.Rect,
colors: Tuple[
Union[pg.Color, Generator[pg.Color, None, None]],
Union[pg.Color, Generator[pg.Color, None, None]],
],
mover: Optional[PositionGenerator] = None,
*groups: pg.sprite.Group
) -> None:
self.colors = colors
self.bounds = bounds
self.rect_size = min(self.bounds.width // 8, self.bounds.height // 8)
image = pg.Surface(self.bounds.size)
image.fill(Colors.Black)
image.set_colorkey(Colors.Black)
super().__init__(image, bounds, mover, *groups)
next(self.mover)
self.update(is_beat=False)
def update(self, *args: Any, **kwargs: Any) -> None:
target = self.mover.send(((self.rect_size, self.rect_size), kwargs["is_beat"]))
self.image.fill(Colors.Black)
line_color = (
self.colors[0]
if isinstance(self.colors[0], pg.Color)
else next(self.colors[0])
)
rect_color = (
self.colors[1]
if isinstance(self.colors[1], pg.Color)
else next(self.colors[1])
)
pg.draw.rect(
self.image,
rect_color,
(
target[0] - self.rect_size // 2,
target[1] - self.rect_size // 2,
self.rect_size,
self.rect_size,
),
0 if kwargs["is_beat"] else 10,
)
pg.draw.line(
self.image,
line_color,
(0, target[1]),
(self.bounds.width, target[1]),
10,
)
pg.draw.line(
self.image,
line_color,
(target[0], 0),
(target[0], self.bounds.height),
10,
)