Add presets

This commit is contained in:
Patrick Moessler 2023-02-17 02:08:21 +01:00
parent 5a28453d2e
commit 3b8449f00c
2 changed files with 100 additions and 22 deletions

View file

@ -1,5 +1,5 @@
from argparse import ArgumentParser
from typing import Iterable, Tuple
from typing import Iterable, List, Tuple
from pygame.locals import *
import math
import pygame as pg
@ -8,7 +8,9 @@ import sys
import time
from effects.bouncingspot import BouncingSpot
from effects.effect import Effect, color_wheel
from effects.doublespot import DoubleSpot
from effects.effect import Effect, color_randomize, color_wheel, Colors
from effects.presets import Presets
def print_displays() -> None:
@ -41,7 +43,7 @@ def initialize(
background.fill(pg.Color(0, 0, 0, 5))
else:
background = pg.Surface(win.get_size())
background.fill(pg.Color(0, 0, 0))
background.fill(Colors.Black)
return win, background
@ -76,7 +78,7 @@ def render_loop_3d(
clock: pg.time.Clock,
) -> None:
stage = pg.Surface(size=window.get_size())
stage.fill(Color(0, 0, 0))
stage.fill(Colors.Black)
full_size = stage.get_size()
scaled_sizes = [
@ -98,10 +100,10 @@ def render_loop_3d(
e.update()
e.draw(stage)
window.fill(Color(0, 0, 0))
window.fill(Colors.Black)
stage.set_colorkey(Color(0, 0, 0))
for i in range(0, 101, 5):
stage.set_colorkey(Colors.Black)
for i in range(0, 101, 4):
stage.set_alpha(192 - 192 * ((i / 100) ** 0.5))
window.blit(
pg.transform.scale(stage, scaled_sizes[int(100 * (i / 100) ** 2)]),
@ -129,6 +131,15 @@ def main() -> None:
argparser.add_argument(
"--trails", action="store_true", help="Fade patterns out (trail mode)"
)
argparser.add_argument(
"--randomize",
metavar="N",
type=int,
nargs="?",
default=0,
const=5,
help="Select random effect presets after <N> seconds",
)
argparser.add_argument("--fps", action="store_true", help="Show FPS in console")
argparser.add_argument(
"-d", "--display", type=int, default=0, help="ID of the display to use"
@ -150,18 +161,20 @@ def main() -> None:
display_id=args.display, windowed=args.window, trails=args.trails
)
presets = Presets(bounds=window.get_rect())
if args.randomize:
effects = presets.randomize()
else:
effects = [
BouncingSpot(
bounds=window.get_rect(),
color=pg.Color(255, 255, 255),
color=Colors.White,
# color=color_wheel(),
sizes=(window.get_height() / 8, window.get_height() / 8),
velocity=(1, 1),
x_factor=(1, 1),
y_factor=(2.2, 2.2),
),
BouncingSpot(bounds=window.get_rect(), color=color_wheel()),
BouncingSpot(bounds=window.get_rect(), color=color_wheel(hue=180)),
]
clock = pg.time.Clock()
@ -171,10 +184,18 @@ def main() -> None:
else:
loop = render_loop_normal(window, background, effects, clock)
framecounter = 0
while True:
next(loop)
pg.display.flip()
clock.tick(60)
framecounter += 1
if args.randomize:
if (framecounter % (args.randomize * 60)) == 0:
effects.clear()
effects.extend(presets.randomize())
if args.fps:
print(clock.get_fps())

57
effects/presets.py Normal file
View file

@ -0,0 +1,57 @@
from random import choice
from typing import List
from effects.effect import Effect, color_randomize, color_wheel, Colors
from effects.bouncingspot import BouncingSpot
from effects.doublespot import DoubleSpot
import pygame as pg
class Presets:
def __init__(self, bounds: pg.Rect) -> None:
self.bounds = bounds
def randomize(self) -> List[Effect]:
return getattr(
self,
choice(
[
func
for func in dir(self)
if callable(getattr(self, func)) and not func.startswith("__")
]
),
)()
def DoubleSpotRandomColor(self) -> List[Effect]:
return [
DoubleSpot(
bounds=self.bounds,
color=color_wheel(increase=30),
radius=100,
fade_out=True,
fade_in=True,
hold=60,
)
]
def DoubleBouncingSpotsColorWheel(self) -> List[Effect]:
return [
BouncingSpot(bounds=self.bounds, color=color_wheel()),
BouncingSpot(bounds=self.bounds, color=color_wheel(hue=180)),
]
def BouncingSpotWhite(self) -> List[Effect]:
return [
BouncingSpot(
bounds=self.bounds,
color=Colors.White,
# color=color_wheel(),
sizes=(self.bounds.height / 8, self.bounds.height / 8),
velocity=(1, 1),
x_factor=(1, 1),
y_factor=(2.2, 2.2),
),
]
#