import pygame
import numpy as np
import random
import math
from pygame import gfxdraw
from typing import List, Tuple, Dict, Optional
# Initialize Pygame
pygame.init()
pygame.font.init()
# Screen setup
WIDTH, HEIGHT = 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sandscript: A Tale of Luminescent Sands")
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
# Font setup
FONT_PATH = "assets/fonts/Cinzel-Regular.ttf" # Replace with an elegant font
FONT_SIZE = 36
font = pygame.font.Font(FONT_PATH, FONT_SIZE)
# Particle settings
PARTICLE_COUNT = 1000
MAX_SPEED = 0.5
ATTRACTION_RADIUS = 200
REPULSION_RADIUS = 50
FADE_DURATION = 60 # Frames
# Story text (original narrative)
STORY = [
"Long ago, in a realm where time flowed like sand through an hourglass...",
"A wanderer emerged from the dunes, their silhouette shimmering in the twilight.",
"They carried a vessel—an ancient urn—filled with stardust and forgotten dreams.",
"As they walked, the sands whispered secrets of the cosmos.",
"The wind sculpted fleeting figures: a phoenix, a serpent, a weeping king.",
"And then, the urn cracked open, spilling light into the void.",
"The particles danced, forming constellations of memories long buried.",
"The wanderer reached out, and the sands became a bridge to another world.",
"A final breath. The sands settled. The story began anew."
]
# Particle class
class Particle:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
self.vx = random.uniform(-MAX_SPEED, MAX_SPEED)
self.vy = random.uniform(-MAX_SPEED, MAX_SPEED)
self.size = random.uniform(1, 3)
self.color = (255, 255, 255)
self.target_x = None
self.target_y = None
self.target_size = None
self.target_color = None
self.life = 0
self.max_life = random.randint(300, 1000)
self.attraction_point = None
self.attraction_strength = 0
self.shape = "circle" # "circle", "square", "star", "figure"
self.glow = 0
def update(self, particles: List['Particle'], current_story_index: int):
self.life += 1
# Apply attraction/repulsion based on story phase
if self.attraction_point:
dx = self.attraction_point[0] - self.x
dy = self.attraction_point[1] - self.y
dist = max(1, math.sqrt(dx * dx + dy * dy))
force = self.attraction_strength / dist
self.vx += dx / dist * force
self.vy += dy / dist * force
# Random motion with damping
self.vx *= 0.99
self.vy *= 0.99
self.vx += random.uniform(-0.01, 0.01)
self.vy += random.uniform(-0.01, 0.01)
# Update position
self.x += self.vx
self.y += self.vy
# Boundary checks (soft wrap)
if self.x < 0: self.x = WIDTH
if self.x > WIDTH: self.x = 0
if self.y < 0: self.y = HEIGHT
if self.y > HEIGHT: self.y = 0
# Glow effect based on story intensity
self.glow = 0.5 + 0.5 * math.sin(self.life * 0.05)
# Shape transitions based on story
if current_story_index == 4: # "phoenix, serpent, weeping king"
if random.random() < 0.01:
self.shape = random.choice(["phoenix", "serpent", "king"])
# Color shifts
if current_story_index == 5: # "urn cracked open"
self.color = (
min(255, int(150 + 100 * math.sin(self.life * 0.02))),
min(255, int(100 + 100 * math.sin(self.life * 0.03 + 1))),
min(255, int(200 + 50 * math.sin(self.life * 0.04 + 2)))
)
elif current_story_index == 6: # "constellations"
self.color = (
min(255, int(200 + 50 * math.sin(self.life * 0.01))),
min(255, int(200 + 50 * math.sin(self.life * 0.02 + 1))),
min(255, int(255))
)
def draw(self, surface):
# Apply glow effect
alpha = int(200 * self.glow)
color_with_alpha = (*self.color, alpha)
# Draw based on shape
if self.shape == "circle":
pygame.gfxdraw.filled_circle(
surface, int(self.x), int(self.y), int(self.size), color_with_alpha
)
elif self.shape == "phoenix":
# Simplified phoenix shape (particles form wings)
pass # (Would use more complex shape logic in full implementation)
# ... other shape cases
# Text animation
class TextAnimation:
def __init__(self, text: str, duration: int):
self.text = text
self.duration = duration
self.frame = 0
self.alpha = 0
self.fade_in = True
self.fade_out = False
self.visible = False
def update(self):
self.frame += 1
if self.fade_in and self.alpha < 255:
self.alpha += 5
if self.alpha >= 255:
self.fade_in = False
self.visible = True
if not self.fade_in and self.frame > self.duration // 2:
self.fade_out = True
if self.fade_out and self.alpha > 0:
self.alpha -= 5
if self.alpha <= 0:
self.visible = False
def draw(self, surface):
if not self.visible:
return
text_surface = font.render(self.text, True, (255, 215, 0)) # Gold text
text_surface.set_alpha(self.alpha)
text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 4))
surface.blit(text_surface, text_rect)
# Main function
def main():
particles = [Particle(random.randint(0, WIDTH), random.randint(0, HEIGHT))
for _ in range(PARTICLE_COUNT)]
story_animations = [TextAnimation(text, 300) for text in STORY]
current_story_index = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLACK)
# Update and draw particles
for particle in particles:
particle.update(particles, current_story_index)
particle.draw(screen)
# Update and draw text
if current_story_index < len(story_animations):
story_animations[current_story_index].update()
story_animations[current_story_index].draw(screen)
# Transition to next story segment
if not story_animations[current_story_index].visible:
current_story_index += 1
# Change particle behavior based on new story segment
if current_story_index < len(story_animations):
for particle in particles:
# Example: Make particles attract to center for "urn cracked open"
if current_story_index == 5:
particle.attraction_point = (WIDTH // 2, HEIGHT // 2)
particle.attraction_strength = 0.1
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()