I'll help you create an immersive particle-based narrative using Python, Pygame, and NumPy. This script will combine storytelling with dynamic particle animations. Here's a sophisticated implementation:
import pygame
import numpy as np
from pygame import gfxdraw
import math
from typing import List, Tuple
import random
from dataclasses import dataclass
import colorsys
# Initialize Pygame
pygame.init()
WIDTH, HEIGHT = 1920, 1080
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("The Celestial Dance")
@dataclass
class Particle:
x: float
y: float
vx: float
vy: float
color: Tuple[int, int, int]
alpha: int
size: float
target_x: float = 0
target_y: float = 0
class ParticleSystem:
def __init__(self, num_particles: int):
self.particles = []
self.initialize_particles(num_particles)
def initialize_particles(self, num_particles: int):
for _ in range(num_particles):
particle = Particle(
x=random.randint(0, WIDTH),
y=random.randint(0, HEIGHT),
vx=random.uniform(-1, 1),
vy=random.uniform(-1, 1),
color=(255, 255, 255),
alpha=255,
size=random.uniform(2, 4)
)
self.particles.append(particle)
def update(self, target_positions: List[Tuple[float, float]], transition_speed: float = 0.05):
for i, particle in enumerate(self.particles):
if i < len(target_positions):
target_x, target_y = target_positions[i]
# Calculate direction vector
dx = target_x - particle.x
dy = target_y - particle.y
# Update velocity with easing
particle.vx = dx * transition_speed
particle.vy = dy * transition_speed
# Update position
particle.x += particle.vx
particle.y += particle.vy
# Add some natural movement
particle.x += math.sin(pygame.time.get_ticks() * 0.001 + i) * 0.5
particle.y += math.cos(pygame.time.get_ticks() * 0.001 + i) * 0.5
def draw(self, surface: pygame.Surface):
for particle in self.particles:
# Create a glowing effect
glow_radius = particle.size * 2
glow_surface = pygame.Surface((glow_radius * 2, glow_radius * 2), pygame.SRCALPHA)
for r in range(int(glow_radius), 0, -1):
alpha = int((1 - r/glow_radius) * particle.alpha)
pygame.draw.circle(glow_surface, (*particle.color, alpha),
(glow_radius, glow_radius), r)
surface.blit(glow_surface,
(particle.x - glow_radius, particle.y - glow_radius))
class Story:
def __init__(self):
self.font = pygame.font.Font(None, 36)
self.narrative = [
"In the depths of the cosmic void, a lone star dancer emerged...",
"Her movements painted ribbons of light across the darkness...",
"With each graceful turn, galaxies sparked to life...",
"Time itself seemed to pause, watching her celestial ballet...",
"And in that eternal moment, the universe learned to dream..."
]
self.current_text = ""
self.target_text = ""
self.text_alpha = 0
self.text_index = 0
self.char_index = 0
self.text_delay = 50 # milliseconds per character
self.last_char_time = 0
def update(self, current_time: int):
if self.text_index >= len(self.narrative):
return True # Story complete
if self.char_index == 0:
self.target_text = self.narrative[self.text_index]
if current_time - self.last_char_time > self.text_delay:
if self.char_index < len(self.target_text):
self.current_text = self.target_text[:self.char_index + 1]
self.char_index += 1
self.last_char_time = current_time
self.text_alpha = min(255, self.text_alpha + 5)
else:
self.text_alpha = max(0, self.text_alpha - 2)
if self.text_alpha == 0:
self.text_index += 1
self.char_index = 0
self.current_text = ""
return False
def draw(self, surface: pygame.Surface):
text_surface = self.font.render(self.current_text, True, (255, 255, 255))
text_surface.set_alpha(self.text_alpha)
text_rect = text_surface.get_rect(center=(WIDTH//2, 100))
surface.blit(text_surface, text_rect)
def generate_shape_positions(shape: str, center_x: float, center_y: float,
size: float, num_points: int) -> List[Tuple[float, float]]:
positions = []
if shape == "circle":
for i in range(num_points):
angle = 2 * math.pi * i / num_points
x = center_x + math.cos(angle) * size
y = center_y + math.sin(angle) * size
positions.append((x, y))
elif shape == "spiral":
for i in range(num_points):
angle = 2 * math.pi * i / num_points
r = size * (i / num_points)
x = center_x + math.cos(angle) * r
y = center_y + math.sin(angle) * r
positions.append((x, y))
return positions
def main():
clock = pygame.time.Clock()
particle_system = ParticleSystem(1000)
story = Story()
running = True
while running:
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear screen
screen.fill((0, 0, 0))
# Update story and get current scene
story_complete = story.update(current_time)
if story_complete:
running = False
# Update particle formations based on story progress
time_factor = (current_time % 10000) / 10000.0
if story.text_index == 0:
positions = generate_shape_positions("circle", WIDTH/2, HEIGHT/2, 200, 1000)
elif story.text_index == 1:
positions = generate_shape_positions("spiral", WIDTH/2, HEIGHT/2, 300, 1000)
else:
positions = generate_shape_positions("circle", WIDTH/2, HEIGHT/2,
200 + math.sin(time_factor * 2 * math.pi) * 100,
1000)
# Update and draw particles
particle_system.update(positions)
particle_system.draw(screen)
# Draw story text
story.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()