From e5a0dcaf619369ee9c86159b2f4890b6531a13b6 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Sat, 18 Feb 2023 22:51:52 +0100 Subject: [PATCH] Fix color copying for color_*() --- effects/effect.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/effects/effect.py b/effects/effect.py index dbbd9b8..2da7c0c 100644 --- a/effects/effect.py +++ b/effects/effect.py @@ -2,11 +2,15 @@ from dataclasses import dataclass from enum import Enum import math import random -from typing import Tuple +from typing import Generator, Tuple import pygame as pg from abc import abstractmethod +def copy_color(source: pg.Color) -> pg.Color: + return pg.Color(source.r, source.g, source.b, source.a) + + @dataclass(frozen=True, slots=True) class Colors: Black = pg.Color(0, 0, 0) @@ -20,19 +24,18 @@ class Colors: def color_wheel(hue=0, increase=1) -> pg.Color: - color = Colors.Red + color = copy_color(Colors.Red) h, s, l, a = color.hsla - color.hsla = hue, s, l, a + h = hue while True: - yield color - h, s, l, a = color.hsla - h = (h + increase) % 360 color.hsla = h, s, l, a + yield color + h = (h + increase) % 360 def color_randomize() -> pg.Color: - color = Colors.Red + color = copy_color(Colors.Red) h, s, l, a = color.hsla color.hsla = random.randint(0, 359), s, l, a @@ -57,12 +60,10 @@ def transform_bounce( 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 + math.cos(x_factor * ticks) * (bounds.width - size_x) // 2 + bounds.centerx ) pos_y = ( - math.sin(y_factor * ticks) * (bounds.height - 2 * size_y) // 2 - + bounds.centery + math.sin(y_factor * ticks) * (bounds.height - size_y) // 2 + bounds.centery ) ticks += velocity / 180 * math.pi