import pygame
import numpy as np
import math
import time
import sys
# ========== CONFIGURATION ==========
WIDTH, HEIGHT = 1280, 720
FPS = 60
PARTICLE_COUNT = 1200
TEXT_TOP_MARGIN = 60
TEXT_FADE_TIME = 1.5
TEXT_DISPLAY_TIME = 4.5
FONT_PATH = None # Use system default, or set to a .ttf file for more style
# ========== STORY & SCENES ==========
# Each scene: (sentence, particle_shape_function, color_palette, [optional: extra params])
STORY = [
(
"In the hush of midnight, the world held its breath—",
"wave",
[(180, 200, 255), (80, 120, 255), (30, 30, 60)],
),
(
"A single grain of sand tumbled from the unseen, spiraling into the void.",
"spiral",
[(255, 220, 180), (255, 180, 80), (255, 255, 200)],
),
(
"Others followed, drawn by a silent promise, swirling into a golden river.",
"river",
[(255, 220, 120), (255, 180, 80), (255, 255, 200)],
),
(
"From the current, a hand emerged—delicate, trembling, sculpted from light.",
"hand",
[(255, 240, 200), (255, 200, 160), (255, 255, 255)],
),
(
"The hand cupped the darkness, gathering the scattered grains.",
"cupped_hand",
[(200, 180, 255), (120, 80, 255), (255, 255, 255)],
),
(
"Within its palm, the sand shimmered, coalescing into a fragile heart.",
"heart",
[(255, 120, 180), (255, 80, 120), (255, 200, 220)],
),
(
"The heart pulsed, casting ripples of color that painted the void with hope.",
"pulse",
[(120, 255, 180), (80, 255, 120), (200, 255, 220)],
),
(
"And as dawn approached, the grains scattered once more—",
"scatter",
[(255, 255, 255), (200, 220, 255), (180, 180, 200)],
),
(
"Each a promise, each a story, each a world waiting to be born.",
"universe",
[(180, 200, 255), (255, 220, 180), (255, 255, 255)],
),
]
# ========== PARTICLE SHAPE FUNCTIONS ==========
def shape_wave(t, n, width, height):
# Sine wave, undulating
x = np.linspace(width*0.1, width*0.9, n)
y = height/2 + 80 * np.sin(2 * np.pi * (x/width) + t)
return np.stack([x, y], axis=1)
def shape_spiral(t, n, width, height):
# Expanding spiral
theta = np.linspace(0, 4 * np.pi, n)
r = np.linspace(20, 180, n) + 30 * np.sin(t + theta)
x = width/2 + r * np.cos(theta + t/2)
y = height/2 + r * np.sin(theta + t/2)
return np.stack([x, y], axis=1)
def shape_river(t, n, width, height):
# Flowing river, particles move in a wavy band
x = np.linspace(width*0.2, width*0.8, n)
y = height*0.7 + 60 * np.sin(2 * np.pi * (x/width) + t*1.2)
return np.stack([x, y], axis=1)
def shape_hand(t, n, width, height):
# Approximate a hand with parametric curves (fingers as arcs)
base_x = width/2
base_y = height*0.6
points = []
for i in range(n):
angle = np.pi/2 + np.pi * (i / n) * 0.7
r = 90 + 30 * np.sin(3 * angle + t)
if i < n*0.2:
# Thumb
x = base_x - 40 + 30 * np.cos(angle)
y = base_y - 40 + 30 * np.sin(angle)
elif i < n*0.6:
# Fingers
x = base_x + r * np.cos(angle)
y = base_y - r * np.sin(angle)
else:
# Palm curve
palm_angle = np.pi + np.pi * (i - n*0.6) / (n*0.4)
x = base_x + 60 * np.cos(palm_angle)
y = base_y + 60 * np.sin(palm_angle)
points.append([x, y])
return np.array(points)
def shape_cupped_hand(t, n, width, height):
# Cupped hand: more closed, rounder palm
base_x = width/2
base_y = height*0.6
points = []
for i in range(n):
angle = np.pi/2 + np.pi * (i / n) * 0.8
r = 80 + 20 * np.sin(2 * angle + t)
if i < n*0.25:
# Thumb
x = base_x - 35 + 25 * np.cos(angle)
y = base_y - 35 + 25 * np.sin(angle)
elif i < n*0.7:
# Fingers
x = base_x + r * np.cos(angle)
y = base_y - r * np.sin(angle)
else:
# Palm curve
palm_angle = np.pi + np.pi * (i - n*0.7) / (n*0.3)
x = base_x + 55 * np.cos(palm_angle)
y = base_y + 55 * np.sin(palm_angle)
points.append([x, y])
return np.array(points)
def shape_heart(t, n, width, height):
# Heart shape parametric equation
theta = np.linspace(0, 2 * np.pi, n)
x = 16 * np.sin(theta) ** 3
y = 13 * np.cos(theta) - 5 * np.cos(2*theta) - 2 * np.cos(3*theta) - np.cos(4*theta)
scale = 12 + 2 * np.sin(t)
x = width/2 + scale * x
y = height/2 + scale * -y
return np.stack([x, y], axis=1)
def shape_pulse(t, n, width, height):
# Pulsing circle, radiating
theta = np.linspace(0, 2 * np.pi, n)
r = 80 + 30 * np.sin(t*2 + theta*3)
x = width/2 + r * np.cos(theta)
y = height/2 + r * np.sin(theta)
return np.stack([x, y], axis=1)
def shape_scatter(t, n, width, height):
# Particles scatter outward
theta = np.linspace(0, 2 * np.pi, n)
r = 80 + 300 * (np.abs(np.sin(t/2)) ** 1.5)
x = width/2 + r * np.cos(theta + t)
y = height/2 + r * np.sin(theta + t)
return np.stack([x, y], axis=1)
def shape_universe(t, n, width, height):
# Particles form a spiral galaxy
theta = np.linspace(0, 6 * np.pi, n)
r = 40 + 180 * (theta / (6 * np.pi))
x = width/2 + r * np.cos(theta + t/3)
y = height/2 + r * np.sin(theta + t/3)
return np.stack([x, y], axis=1)
SHAPE_FUNCTIONS = {
"wave": shape_wave,
"spiral": shape_spiral,
"river": shape_river,
"hand": shape_hand,
"cupped_hand": shape_cupped_hand,
"heart": shape_heart,
"pulse": shape_pulse,
"scatter": shape_scatter,
"universe": shape_universe,
}
# ========== PARTICLE SYSTEM ==========
class ParticleSystem:
def __init__(self, n, width, height):
self.n = n
self.width = width
self.height = height
self.pos = np.random.rand(n, 2) * [width, height]
self.vel = np.zeros((n, 2))
self.target = self.pos.copy()
self.color = np.zeros((n, 3), dtype=np.uint8)
self.glow = np.ones(n)
self.noise = np.random.rand(n, 2) * 2 * np.pi
def set_shape(self, shape_func, t, palette):
# Compute target positions
target = shape_func(t, self.n, self.width, self.height)
# Assign colors from palette, smoothly interpolate
palette = np.array(palette)
idx = np.linspace(0, len(palette)-1, self.n).astype(int)
color = palette[idx]
self.target = target
self.color = color
def update(self, dt, t):
# Move particles toward target with springy motion
direction = self.target - self.pos
self.vel += 0.08 * direction - 0.04 * self.vel
# Add some swirling noise for organic motion
angle = self.noise[:,0] + t*0.7
swirl = np.stack([np.cos(angle), np.sin(angle)], axis=1) * 0.7
self.vel += swirl
self.pos += self.vel * dt * 0.7
# Glow pulses with time
self.glow = 1.2 + 0.7 * np.sin(t + self.noise[:,1])
def draw(self, surface):
for i in range(self.n):
x, y = int(self.pos[i,0]), int(self.pos[i,1])
color = tuple(self.color[i])
glow = self.glow[i]
# Draw glow: several circles with alpha
for r, alpha in [(12, 30), (7, 80), (3, 180)]:
s = pygame.Surface((r*2, r*2), pygame.SRCALPHA)
pygame.draw.circle(s, color + (int(alpha*glow),), (r, r), r)
surface.blit(s, (x-r, y-r), special_flags=pygame.BLEND_ADD)
# ========== TEXT EFFECTS ==========
def render_glowing_text(text, font, color, glow_color, alpha, surface, y):
# Render main text
text_surf = font.render(text, True, color)
text_surf.set_alpha(int(alpha*255))
x = (surface.get_width() - text_surf.get_width()) // 2
# Render glow
for glow_radius in [8, 16, 32]:
glow_surf = font.render(text, True, glow_color)
glow_surf.set_alpha(int(alpha*60/(glow_radius/8)))
for dx in [-1,0,1]:
for dy in [-1,0,1]:
if dx or dy:
surface.blit(glow_surf, (x+dx*glow_radius//4, y+dy*glow_radius//4))
# Render main text on top
surface.blit(text_surf, (x, y))
# ========== MAIN LOOP ==========
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
clock = pygame.time.Clock()
font_size = 54
font = pygame.font.Font(FONT_PATH, font_size)
font_color = (255, 255, 255)
glow_color = (120, 180, 255)
particle_system = ParticleSystem(PARTICLE_COUNT, WIDTH, HEIGHT)
scene_idx = 0
t_scene = 0
t_global = 0
running = True
fade_in = 0
fade_out = 0
text_alpha = 0
text_state = "fade_in"
last_time = time.time()
while running:
now = time.time()
dt = min(now - last_time, 1/30)
last_time = now
t_global += dt
t_scene += dt
for event in pygame.event.get():
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
):
running = False
# Scene management
if scene_idx >= len(STORY):
# End: fade out
screen.fill((0,0,0))
pygame.display.flip()
pygame.time.wait(2000)
break
sentence, shape_name, palette = STORY[scene_idx]
shape_func = SHAPE_FUNCTIONS[shape_name]
# Text fade in/out logic
if text_state == "fade_in":
fade_in += dt
text_alpha = min(1, fade_in / TEXT_FADE_TIME)
if fade_in >= TEXT_FADE_TIME:
text_state = "display"
fade_in = TEXT_FADE_TIME
elif text_state == "display":
text_alpha = 1
if t_scene >= TEXT_DISPLAY_TIME:
text_state = "fade_out"
fade_out = 0
elif text_state == "fade_out":
fade_out += dt
text_alpha = max(0, 1 - fade_out / TEXT_FADE_TIME)
if fade_out >= TEXT_FADE_TIME:
# Next scene
scene_idx += 1
t_scene = 0
fade_in = 0
fade_out = 0
text_state = "fade_in"
continue
# Particle system: smoothly morph to new shape
morph_t = t_scene if t_scene < 2 else 2 + (t_scene-2)*0.7
particle_system.set_shape(shape_func, morph_t, palette)
particle_system.update(dt, t_global)
# Draw
screen.fill((0,0,0))
particle_system.draw(screen)
render_glowing_text(
sentence, font, font_color, glow_color, text_alpha, screen, TEXT_TOP_MARGIN
)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()