add color presets and helpers
This commit is contained in:
parent
95f3e6c845
commit
9202c924f7
1 changed files with 58 additions and 2 deletions
|
@ -1,9 +1,26 @@
|
|||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
import math
|
||||
import random
|
||||
from typing import Tuple
|
||||
import pygame as pg
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
def color_wheel(hue=0, increase=1):
|
||||
color = pg.Color(255, 0, 0)
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Colors:
|
||||
Black = pg.Color(0, 0, 0)
|
||||
White = pg.Color(255, 255, 255)
|
||||
Red = pg.Color(255, 0, 0)
|
||||
Green = pg.Color(0, 255, 0)
|
||||
Blue = pg.Color(0, 0, 255)
|
||||
Cyan = pg.Color(0, 255, 255)
|
||||
Yellow = pg.Color(255, 255, 0)
|
||||
Magenta = pg.Color(255, 0, 255)
|
||||
|
||||
|
||||
def color_wheel(hue=0, increase=1) -> pg.Color:
|
||||
color = Colors.Red
|
||||
h, s, l, a = color.hsla
|
||||
color.hsla = hue, s, l, a
|
||||
|
||||
|
@ -14,6 +31,45 @@ def color_wheel(hue=0, increase=1):
|
|||
color.hsla = h, s, l, a
|
||||
|
||||
|
||||
def color_randomize() -> pg.Color:
|
||||
color = Colors.Red
|
||||
h, s, l, a = color.hsla
|
||||
color.hsla = random.randint(0, 359), s, l, a
|
||||
|
||||
while True:
|
||||
yield color
|
||||
color.hsla = random.randint(0, 359), s, l, a
|
||||
|
||||
|
||||
def transform_bounce(
|
||||
bounds: pg.Rect,
|
||||
velocity: Tuple[int, int],
|
||||
x_factor: Tuple[int, int],
|
||||
y_factor: Tuple[int, int],
|
||||
):
|
||||
min_velocity = velocity[0]
|
||||
max_velocity = velocity[1]
|
||||
velocity = random.randint(min_velocity, max_velocity)
|
||||
ticks = random.randint(0, 360)
|
||||
x_factor = random.uniform(x_factor[0], x_factor[1])
|
||||
y_factor = random.uniform(y_factor[0], y_factor[1])
|
||||
|
||||
size_x, size_y = yield (bounds.centerx, bounds.centery)
|
||||
while True:
|
||||
pos_x = (
|
||||
math.cos(x_factor * ticks) * (bounds.width - 2 * size_x) // 2
|
||||
+ bounds.centerx
|
||||
)
|
||||
pos_y = (
|
||||
math.sin(y_factor * ticks) * (bounds.height - 2 * size_y) // 2
|
||||
+ bounds.centery
|
||||
)
|
||||
|
||||
ticks += velocity / 180 * math.pi
|
||||
velocity = random.randint(min_velocity, max_velocity)
|
||||
size_x, size_y = yield (pos_x, pos_y)
|
||||
|
||||
|
||||
class Effect(pg.sprite.Sprite):
|
||||
def __init__(
|
||||
self, image: pg.Surface, rect: pg.Rect, *groups: pg.sprite.Group
|
||||
|
|
Loading…
Add table
Reference in a new issue