From 448999c449d5e17e25da6b8a97b34b29145beb0e Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 15 Feb 2023 22:38:02 +0100 Subject: [PATCH] use color_fader --- beamshow.py | 30 ++++++++++++++++++------------ effects/bouncingspot.py | 28 ++++++++-------------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/beamshow.py b/beamshow.py index c189dc4..9a04b2e 100644 --- a/beamshow.py +++ b/beamshow.py @@ -6,25 +6,31 @@ import sys import time from effects.bouncingspot import BouncingSpot +from effects.effect import color_fader # pg.init() -win = pg.display.set_mode(size=(1600, 1200), flags=pg.RESIZABLE) +win = pg.display.set_mode(size=(800, 600), flags=pg.RESIZABLE) -background = pg.Surface(win.get_size()) -background.fill(pg.Color(0, 0, 0)) +# background = pg.Surface(win.get_size()) +# background.fill(pg.Color(0, 0, 0)) + +background = pg.Surface(win.get_size(), flags=pg.SRCALPHA) +background.fill(pg.Color(0, 0, 0, 10)) effects = [ - BouncingSpot( - bounds=win.get_rect(), - colored=False, - sizes=(300, 300), - velocity=(1, 1), - x_factor=(1, 1), - y_factor=(2.2, 2.2), - ), - # BouncingSpot(bounds=win.get_rect()) + # BouncingSpot( + # bounds=win.get_rect(), + # # color=pg.Color(255,255,255), + # color=color_fader(), + # sizes=(300, 300), + # velocity=(1, 1), + # x_factor=(1, 1), + # y_factor=(2.2, 2.2), + # ), + BouncingSpot(bounds=win.get_rect(), color=color_fader()), + BouncingSpot(bounds=win.get_rect(), color=color_fader(hue=180)) ] diff --git a/effects/bouncingspot.py b/effects/bouncingspot.py index 4762390..622245a 100644 --- a/effects/bouncingspot.py +++ b/effects/bouncingspot.py @@ -3,27 +3,20 @@ import pygame as pg from effects.effect import Effect import random import math +from typing import Union, Generator class BouncingSpot(Effect): - # MIN_SIZE = 10 - # max_size = 100 - # min_velocity = 1 - # max_velocity = 10 - # min_xy_factor = 0.1 - # max_xy_factor = 1 - def __init__( self, bounds: pg.Rect, - colored=True, + color: Union[pg.Color, Generator[pg.Color, None, None]], sizes=(10, 100), velocity=(1, 10), x_factor=(0.1, 1), y_factor=(0.1, 1), *groups: pg.sprite.Group ) -> None: - self.colored = colored self.min_size = sizes[0] self.max_size = sizes[1] self.min_velocity = velocity[0] @@ -33,12 +26,7 @@ class BouncingSpot(Effect): 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 = pg.Color( - 255, - 0 if colored else 255, - 0 if colored else 255, - 255, - ) + self.color = color size = (math.sin(self.ticks) / 2 + 0.5) * ( self.max_size - self.min_size ) + self.min_size @@ -73,11 +61,11 @@ class BouncingSpot(Effect): ) self.image.fill(pg.Color(255, 255, 0, 0)) - pg.draw.ellipse(self.image, self.color, ((0, 0), self.rect.size)) + pg.draw.ellipse( + self.image, + self.color if isinstance(self.color, pg.Color) else next(self.color), + ((0, 0), self.rect.size), + ) self.ticks += self.velocity / 180 * math.pi self.velocity = random.randint(self.min_velocity, self.max_velocity) - if self.colored: - h, s, l, a = self.color.hsla - h = (h + 1) % 256 - self.color.hsla = h, s, l, a