File size: 4,800 Bytes
a7d7463 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | """Particle Burst Effect - Celebration animations"""
import sys
import time
import random
import threading
from typing import Optional, Tuple
from dataclasses import dataclass
from .config import DEFAULT_CONFIG, AnimationConfig
@dataclass
class Particle:
"""Individual particle for burst effect"""
x: float
y: float
vx: float
vy: float
char: str
color: str
lifetime: int
class ParticleBurst:
"""Create particle burst celebration effect"""
PARTICLE_CHARS = ["*", "✦", "◆", "●", "○", "✿", "❀", "★", "☆", "◇", "□", "○"]
COLORS = [
"\033[91m",
"\033[92m",
"\033[93m",
"\033[94m",
"\033[95m",
"\033[96m",
"\033[97m",
]
RESET = "\033[0m"
def __init__(
self,
center_x: Optional[int] = None,
center_y: Optional[int] = None,
count: int = 50,
config: Optional[AnimationConfig] = None,
):
self.center_x = center_x or 40
self.center_y = center_y or 10
self.count = count
self.config = config or DEFAULT_CONFIG
self.particles: list[Particle] = []
self.running = False
def _create_particle(self) -> Particle:
"""Create a single particle"""
angle = random.uniform(0, 2 * 3.14159)
speed = random.uniform(0.5, 2.0)
return Particle(
x=self.center_x,
y=self.center_y,
vx=random.uniform(-1, 1) + (1 if angle < 3.14159 else -1),
vy=random.uniform(-2, 0),
char=random.choice(self.PARTICLE_CHARS),
color=random.choice(self.COLORS),
lifetime=random.randint(20, 40),
)
def _animate(self):
"""Animate particle system"""
self.particles = [self._create_particle() for _ in range(self.count)]
self.running = True
while self.running and self.particles:
lines = []
max_y = 0
for particle in self.particles[:]:
particle.x += particle.vx
particle.y += particle.vy
particle.vy += 0.1
particle.lifetime -= 1
if particle.lifetime <= 0 or particle.y > 50:
self.particles.remove(particle)
continue
x_pos = max(0, int(particle.x))
y_pos = max(0, int(particle.y))
max_y = max(max_y, y_pos)
line_content = " " * x_pos + particle.color + particle.char + self.RESET
lines.append((y_pos, line_content))
if not lines:
break
lines.sort(key=lambda x: x[0])
sys.stdout.write("\033[2J")
sys.stdout.write("\033[H")
for y, content in lines:
sys.stdout.write(content + "\n")
for _ in range(max(0, 20 - max_y)):
sys.stdout.write("\n")
sys.stdout.flush()
time.sleep(0.05)
sys.stdout.write("\033[2J")
sys.stdout.write("\033[H")
sys.stdout.flush()
def explode(self):
"""Trigger the burst animation"""
thread = threading.Thread(target=self._animate, daemon=True)
thread.start()
thread.join()
@staticmethod
def explode_at(message: str, times: int = 3):
"""Show explosion effect around a message"""
for _ in range(times):
burst = ParticleBurst(count=30)
burst.center_x = 50
burst.center_y = 15
burst.explode()
time.sleep(0.3)
print(message)
class SimpleConfetti:
"""Lightweight confetti effect"""
CONFETTI = ["🎉", "🎊", "✨", "💫", "⭐", "🌟", "🎈", "🎁"]
def __init__(self, duration: float = 2.0):
self.duration = duration
self.end_time = 0
def __iter__(self):
"""Generate confetti frames"""
self.end_time = time.time() + self.duration
while time.time() < self.end_time:
confetti = " ".join(random.choices(self.CONFETTI, k=random.randint(5, 15)))
yield f"\r{confetti}"
time.sleep(0.2)
yield "\r" + " " * 50 + "\r"
def show(self):
"""Display confetti"""
try:
for frame in self:
sys.stdout.write(frame)
sys.stdout.flush()
except KeyboardInterrupt:
pass
class ThankYouBurst:
"""Special burst for thank you messages"""
@staticmethod
def show():
"""Show thank you burst animation"""
ParticleBurst.explode_at("🙏 Thank You! 🙏", times=3)
def celebration(message: str = "🎉"):
"""Quick celebration effect"""
SimpleConfetti(duration=1.5).show()
print(message)
|