diff --git a/effects/scanreticle.py b/effects/scanreticle.py new file mode 100644 index 0000000..9ab9ff5 --- /dev/null +++ b/effects/scanreticle.py @@ -0,0 +1,84 @@ +from typing import Any, Tuple +import pygame as pg +from effects.effect import Effect, Colors, transform_bounce +import random +from typing import Union, Generator + + +class ScanReticle(Effect): + def __init__( + self, + bounds: pg.Rect, + colors: Tuple[ + Union[pg.Color, Generator[pg.Color, None, None]], + Union[pg.Color, Generator[pg.Color, None, None]], + ], + velocity=(1, 10), + x_factor=(0.1, 1), + y_factor=(0.1, 1), + *groups: pg.sprite.Group + ) -> None: + 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) + 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, *groups) + 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: + + target = self.bouncer.send((self.rect_size, self.rect_size)) + + 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, + ), + 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, + ) + + # self.ticks += int(self.velocity / 180 * math.pi) + # self.velocity = random.randint(self.min_velocity, self.max_velocity)