use color_fader

This commit is contained in:
Patrick 2023-02-15 22:38:02 +01:00
parent 192e9d22eb
commit 448999c449
2 changed files with 26 additions and 32 deletions

View file

@ -6,25 +6,31 @@ import sys
import time import time
from effects.bouncingspot import BouncingSpot from effects.bouncingspot import BouncingSpot
from effects.effect import color_fader
# pg.init() # 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 = pg.Surface(win.get_size())
background.fill(pg.Color(0, 0, 0)) # 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 = [ effects = [
BouncingSpot( # BouncingSpot(
bounds=win.get_rect(), # bounds=win.get_rect(),
colored=False, # # color=pg.Color(255,255,255),
sizes=(300, 300), # color=color_fader(),
velocity=(1, 1), # sizes=(300, 300),
x_factor=(1, 1), # velocity=(1, 1),
y_factor=(2.2, 2.2), # x_factor=(1, 1),
), # y_factor=(2.2, 2.2),
# BouncingSpot(bounds=win.get_rect()) # ),
BouncingSpot(bounds=win.get_rect(), color=color_fader()),
BouncingSpot(bounds=win.get_rect(), color=color_fader(hue=180))
] ]

View file

@ -3,27 +3,20 @@ import pygame as pg
from effects.effect import Effect from effects.effect import Effect
import random import random
import math import math
from typing import Union, Generator
class BouncingSpot(Effect): 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__( def __init__(
self, self,
bounds: pg.Rect, bounds: pg.Rect,
colored=True, color: Union[pg.Color, Generator[pg.Color, None, None]],
sizes=(10, 100), sizes=(10, 100),
velocity=(1, 10), velocity=(1, 10),
x_factor=(0.1, 1), x_factor=(0.1, 1),
y_factor=(0.1, 1), y_factor=(0.1, 1),
*groups: pg.sprite.Group *groups: pg.sprite.Group
) -> None: ) -> None:
self.colored = colored
self.min_size = sizes[0] self.min_size = sizes[0]
self.max_size = sizes[1] self.max_size = sizes[1]
self.min_velocity = velocity[0] self.min_velocity = velocity[0]
@ -33,12 +26,7 @@ class BouncingSpot(Effect):
self.ticks = random.randint(0, 360) self.ticks = random.randint(0, 360)
self.x_factor = random.uniform(x_factor[0], x_factor[1]) self.x_factor = random.uniform(x_factor[0], x_factor[1])
self.y_factor = random.uniform(y_factor[0], y_factor[1]) self.y_factor = random.uniform(y_factor[0], y_factor[1])
self.color = pg.Color( self.color = color
255,
0 if colored else 255,
0 if colored else 255,
255,
)
size = (math.sin(self.ticks) / 2 + 0.5) * ( size = (math.sin(self.ticks) / 2 + 0.5) * (
self.max_size - self.min_size self.max_size - self.min_size
) + self.min_size ) + self.min_size
@ -73,11 +61,11 @@ class BouncingSpot(Effect):
) )
self.image.fill(pg.Color(255, 255, 0, 0)) 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.ticks += self.velocity / 180 * math.pi
self.velocity = random.randint(self.min_velocity, self.max_velocity) 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