Below is a self-contained Python 3 program that interleaves an original short story with a real–time, sand-art-style particle show.
Run it with:
pip install pygame
python sand_art_story.py
(Everything else—math, glow, timing—comes from the standard library + pygame.)
# sand_art_story.py
# -------------------------------------------------------------
# A particle-driven sand-art narrative.
# Written from scratch, 100 % original words & code.
#
# Author: ChatGPT (2024) – released into the public domain ❤️
# -------------------------------------------------------------
import pygame
import random
import math
from pygame import gfxdraw
# -------------------------------------------------------------
# CONFIGURATION
# -------------------------------------------------------------
W, H = 1280, 720 # Window size
FPS = 60
MAX_PARTICLES = 1500 # Total grains of “sand”
TEXT_FONT = "dejavusansmono" # Anything you have installed
TEXT_SIZE = 42
TEXT_FADE_TIME = 2.0 # seconds
TEXT_HOLD_TIME = 4.0 # seconds
SCENE_TOTAL_TIME = TEXT_FADE_TIME*2 + TEXT_HOLD_TIME
# -------------------------------------------------------------
# THE STORY (10 sentences, Nobel-Prize dream 😉)
# -------------------------------------------------------------
STORY = [
"From the hush of the cosmic canvas, a lone ember stirred.",
"It pulsed with yearning, drawing kindred sparks into a crimson spiral.",
"As heat became harmony, the spiral condensed into the silhouette "
"of a child made of dusk-lit sand.",
"The child raised a trembling hand and turned the rupture of night "
"into a circlet of shimmering dawn.",
"In the infant light, a sapling unfurled—"
"a luminous tree whose leaves whispered every untold memory.",
"A thousand birds of sapphire dust burst from the branches, "
"stitching the sky with impossible songs.",
"Their wings beat time itself into a river that curled, braided, "
"and looped, an infinity of radiant echo.",
"The river reached out like an open palm, offering its glimmering "
"current to all who hungered for wonder.",
"Stars, innumerable and newborn, accepted the gift, "
"crowning the darkness with fierce gentleness.",
"And when every shadow had been taught to glow, "
"the ember finally rested, becoming the quiet heartbeat of creation."
]
# -------------------------------------------------------------
# SHAPE HELPERS
# -------------------------------------------------------------
def gen_spiral(n=1200, turns=4, radius=260, center=(W//2, H//2)):
cx, cy = center
pts = []
for i in range(n):
t = turns * 2*math.pi * i / n
r = radius * i / n
x = cx + r*math.cos(t)
y = cy + r*math.sin(t)
pts.append((x, y))
return pts
def gen_circle(n=1000, radius=220, center=(W//2, H//2)):
cx, cy = center
return [
(cx + radius*math.cos(2*math.pi*i/n),
cy + radius*math.sin(2*math.pi*i/n))
for i in range(n)
]
def gen_figure(n=1000, center=(W//2, H//2+30)):
# stick-figure: head circle + torso + arms + legs
cx, cy = center
pts = []
# head
for i in range(n//3):
t = 2*math.pi*i/(n//3)
pts.append((cx+40*math.cos(t), cy-160+40*math.sin(t)))
# body
for y in range(-120, 0, 4):
pts.append((cx, cy+y))
# arms
for x in range(-70, 71, 4):
pts.append((cx+x, cy-80))
# legs
for i in range(60):
pts.append((cx-25+i*0.5, cy+i))
pts.append((cx+25-i*0.5, cy+i))
return pts[:n]
def gen_ring(n=900, radius_inner=140, radius_outer=180, center=(W//2, H//2)):
# circlet / halo
pts = gen_circle(n, radius_outer, center)
pts += gen_circle(n, radius_inner, center)
return pts[:n]
def gen_tree(n=1000, center=(W//2, H//2+50)):
cx, cy = center
pts = []
# trunk
for y in range(0, 120, 4):
for x in range(-15, 16, 10):
pts.append((cx+x, cy+y))
# leaves (triangle)
height = 200
for i in range(n - len(pts)):
t = random.random()
x = (1-t)*(cx-height/2) + t*(cx+height/2)
y = cy - height + t*(-50)
pts.append((x, y))
return pts[:n]
def gen_bird(n=900, center=(W//2, H//2-50)):
cx, cy = center
pts = []
# two wings as Bezier-ish curves
for i in range(n//2):
t = i/(n//2)
x = cx - 220*t
y = cy - 60*math.sin(math.pi*t)
pts.append((x, y))
for i in range(n//2):
t = i/(n//2)
x = cx + 220*t
y = cy - 60*math.sin(math.pi*t)
pts.append((x, y))
return pts
def gen_infinity(n=1000, center=(W//2, H//2)):
cx, cy = center
pts = []
a = 160
for i in range(n):
t = 2*math.pi * i / n
denom = math.sin(t)**2 + 1
x = cx + a*math.cos(t)/denom
y = cy + a*math.sin(t)*math.cos(t)/denom
pts.append((x, y))
return pts
def gen_hand(n=1000, center=(W//2, H//2)):
# crude open-palm: circle (+) 4 fingers
cx, cy = center
pts = gen_circle(n//2, 90, (cx, cy+30))
finger_len = 120
for f in [-60, -20, 20, 60]:
fx = cx + f
for i in range(0, finger_len, 6):
pts.append((fx, cy - 30 - i))
return pts[:n]
def gen_star(n=1000, radius=240, center=(W//2, H//2)):
cx, cy = center
pts = []
for i in range(5):
angle = 2*math.pi * i / 5 - math.pi/2
inner_angle = angle + math.pi/5
pts.append((cx + radius*math.cos(angle),
cy + radius*math.sin(angle)))
pts.append((cx + radius*0.5*math.cos(inner_angle),
cy + radius*0.5*math.sin(inner_angle)))
# Interpolate along edges
cloud = []
for i in range(len(pts)):
x1, y1 = pts[i]
x2, y2 = pts[(i+1)%len(pts)]
steps = n//len(pts)
for s in range(steps):
t = s/steps
cloud.append(((1-t)*x1+t*x2, (1-t)*y1+t*y2))
return cloud[:n]
def gen_explosion(n=1000):
# scatter everywhere
return [(random.uniform(0, W), random.uniform(0, H)) for _ in range(n)]
# Map each scene to its target-shape generator + main hue
SCENES = [
(gen_spiral, (255, 90, 30)),
(gen_spiral, (255, 40, 40)),
(gen_figure, (255, 130, 80)),
(gen_ring, (255, 190, 0)),
(gen_tree, ( 80, 255, 80)),
(gen_bird, ( 80, 200, 255)),
(gen_infinity, (190, 140, 255)),
(gen_hand, (255, 240, 190)),
(gen_star, (255, 255, 160)),
(gen_explosion, (255, 255, 255))
]
# -------------------------------------------------------------
# PARTICLE CLASS
# -------------------------------------------------------------
class Particle:
def __init__(self):
self.x = random.uniform(0, W)
self.y = random.uniform(0, H)
self.vx = self.vy = 0.0
self.tx = self.ty = self.x
self.color = [255, 255, 255]
def update(self, dt):
# spring toward target
ax = (self.tx - self.x)*0.8
ay = (self.ty - self.y)*0.8
self.vx += ax*dt
self.vy += ay*dt
# damping
self.vx *= 0.84
self.vy *= 0.84
self.x += self.vx*dt*60
self.y += self.vy*dt*60
def draw(self, surf):
# glow by drawing concentric translucent circles
r1 = 3
r2 = 6
r3 = 10
col = self.color
gfxdraw.filled_circle(surf, int(self.x), int(self.y), r3,
(*col, 20))
gfxdraw.filled_circle(surf, int(self.x), int(self.y), r2,
(*col, 60))
gfxdraw.filled_circle(surf, int(self.x), int(self.y), r1,
(*col, 200))
# -------------------------------------------------------------
# MAIN APPLICATION
# -------------------------------------------------------------
def main():
pygame.init()
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("Sand-Art Story")
clock = pygame.time.Clock()
font = pygame.font.SysFont(TEXT_FONT, TEXT_SIZE, bold=True)
# pre-create particles
particles = [Particle() for _ in range(MAX_PARTICLES)]
current_scene = 0
scene_time = 0.0
# Initialise first shape
targets = SCENES[0][0](MAX_PARTICLES)
recolor = SCENES[0][1]
running = True
while running:
dt = clock.tick(FPS)/1000.0
scene_time += dt
# ----- event handling
for e in pygame.event.get():
if e.type == pygame.QUIT or (
e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE):
running = False
# ----- scene progression
if scene_time >= SCENE_TOTAL_TIME and current_scene < len(STORY)-1:
# advance
current_scene += 1
targets = SCENES[current_scene][0](MAX_PARTICLES)
recolor = SCENES[current_scene][1]
scene_time = 0.0
# ----- assign targets & colors
for i, p in enumerate(particles):
tx, ty = targets[i % len(targets)]
p.tx, p.ty = tx, ty
# smooth color shift
for c in range(3):
p.color[c] += (recolor[c] - p.color[c])*0.05
# ----- update physics
for p in particles:
p.update(dt)
# ----- drawing
screen.fill((0, 0, 0))
for p in particles:
p.draw(screen)
# ----- draw text with fade logic
alpha = 255
if scene_time < TEXT_FADE_TIME: # fade in
alpha = int(255 * (scene_time/TEXT_FADE_TIME))
elif scene_time > TEXT_FADE_TIME + TEXT_HOLD_TIME: # fade out
t = (scene_time - TEXT_FADE_TIME - TEXT_HOLD_TIME) / TEXT_FADE_TIME
alpha = int(255 * (1.0 - t))
txt_surface = font.render(STORY[current_scene], True, (255, 255, 255))
txt_surface.set_alpha(alpha)
text_rect = txt_surface.get_rect(center=(W//2, TEXT_SIZE))
screen.blit(txt_surface, text_rect)
pygame.display.flip()
pygame.quit()
# -------------------------------------------------------------
# ENTRY POINT
# -------------------------------------------------------------
if __name__ == "__main__":
main()