| """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) |
|
|