import pygame
import sys
import random
import math
pygame.init()
# Screen setup
WIDTH, HEIGHT = 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Whispers of the Infinite Dunes")
clock = pygame.time.Clock()
FPS = 60
# Color Palette
BLACK = (0, 0, 0)
CRIMSON = (220, 40, 60)
GOLDEN = (255, 215, 0)
AQUAMARINE = (127, 255, 212)
VIOLET = (148, 0, 211)
STARDUST = (200, 255, 255)
# Font setup
TITLE_FONT = pygame.font.SysFont('Palatino Linotype', 48, italic=True)
MAIN_FONT = pygame.font.SysFont('Cinzel', 36)
SMALL_FONT = pygame.font.SysFont('Cinzel', 24)
# Story narrative
story_segments = [
{
'text': "In the twilight of a dying star, where cosmic tides sculpt endless dunes of crystalline sand,",
'particles': 'celestial_drift'
},
{
'text': "a lone wanderer traced constellations etched by forgotten gods upon the desert's skin.",
'particles': 'constellation'
},
{
'text': "With each step, the grains sang symphonies of lost civilizations, their echoes woven into spiraling glyphs.",
'particles': 'spiraling_glyphs'
},
{
'text': "As the horizon ignited in auroral flames, a silhouette emerged from the shimmering void -',
'particles': 'void_emergence'
},
{
'text': "'Bearer of the Chrono-Sand,' it intoned, hands cupping an hourglass of liquid starlight.",
'particles': 'hourglass'
}
]
# Particle System
class Particle:
def __init__(self, x, y, target_x, target_y, color, speed=0.2, size=2):
self.pos = pygame.Vector2(x, y)
self.target = pygame.Vector2(target_x, target_y)
self.color = color
self.size = size
self.speed = speed
self.lifespan = 255
self.noise_seed = random.uniform(0, 1000)
self.path = None
def update(self, dt, phase):
"""Update particle movement based on current narrative phase"""
if phase == 'celestial_drift':
self._update_celestial(dt)
elif phase == 'constellation':
self._update_constellation(dt)
elif phase == 'spiraling_glyphs':
self._update_spiral(dt)
elif phase == 'void_emergence':
self._update_void(dt)
elif phase == 'hourglass':
self._update_hourglass(dt)
# Universal decay
self.lifespan -= 1.5
self.lifespan = max(0, self.lifespan)
def _update_celestial(self, dt):
# Celestial drift with Perlin turbulence
direction = (self.target - self.pos) * self.speed * dt / 1000
turbulence = self._turbulence_effect()
self.pos += direction + turbulence * 0.5
def _update_constellation(self, dt):
# Forming celestial patterns
if self.path:
self._follow_path(dt)
else:
self._seek_target(dt * 0.8)
def _update_spiral(self, dt):
# Hypnotic spiral motion
center = pygame.Vector2(WIDTH/2, HEIGHT/2)
to_center = center - self.pos
distance = to_center.length()
if distance > 0:
to_center /= distance
angle = math.sin(pygame.time.get_ticks() * 0.001 + self.noise_seed) * 2
tangent = pygame.Vector2(-to_center.y, to_center.x).rotate(angle)
self.pos += tangent * (50 + distance/3) * dt / 1000
def _update_void(self, dt):
# Merging into shadowy figure
self._seek_target(dt * 1.2)
if self.color == VIOLET:
self.color = self._lerp_color(VIOLET, STARDUST, 1 - self.lifespan/255)
def _update_hourglass(self, dt):
# Flowing through temporal gateway
if self.path:
self._follow_path(dt)
else:
self._seek_target(dt * 0.5)
def _seek_target(self, speed_factor):
direction = (self.target - self.pos) * speed_factor
self.pos += direction
def _turbulence_effect(self):
# 2D Perlin-like noise simulation
t = pygame.time.get_ticks() * 0.001
nx = math.sin(self.noise_seed + t)
ny = math.cos(self.noise_seed * 2 + t * 0.5)
return pygame.Vector2(nx, ny)
def _follow_path(self, dt):
# Path following behavior
if not self.path:
return
future_pos = self.pos + self._velocity * dt/1000 * 10
target_point = self._get_closest_point_on_path(future_pos)
self._velocity = (target_point - self.pos).normalize() * self.speed * 100
self.pos += self._velocity * dt/1000
def _get_closest_point_on_path(self, position):
# Simplified path proximity check
min_dist = float('inf')
closest = self.path[0]
for point in self.path:
dist = position.distance_to(point)
if dist < min_dist:
min_dist = dist
closest = point
return closest
def _lerp_color(self, color1, color2, t):
# Linear interpolation between colors
return (
int(color1[0] + (color2[0] - color1[0]) * t),
int(color1[1] + (color2[1] - color1[1]) * t),
int(color1[2] + (color2[2] - color1[2]) * t)
)
def draw(self, surface):
if self.lifespan <= 0:
return
# Create surface for glowing effect
glow_surface = pygame.Surface((self.size*4, self.size*4), pygame.SRCALPHA)
alpha = int(self.lifespan * 0.7)
pygame.draw.circle(glow_surface, (*self.color, alpha), (self.size*2, self.size*2), self.size*2)
surface.blit(glow_surface, self.pos - pygame.Vector2(self.size*2, self.size*2), special_flags=pygame.BLEND_RGBA_ADD)
# Story Manager
class StoryEngine:
def __init__(self):
self.segment_index = 0
self.char_index = 0
self.text_timer = 0
self.text_delay = 50 # ms per character
self.fade_alpha = 255
self.display_complete = False
self.current_segment = story_segments[0]
def update(self, dt):
if self.display_complete:
return
if self.char_index < len(self.current_segment['text']):
self.text_timer += dt
if self.text_timer >= self.text_delay:
self.char_index += 1
self.text_timer = 0
else:
# Wait then fade
if self.fade_alpha > 0:
self.fade_alpha -= 1.5
else:
self._next_segment()
def _next_segment(self):
self.segment_index += 1
if self.segment_index < len(story_segments):
self.char_index = 0
self.fade_alpha = 255
self.current_segment = story_segments[self.segment_index]
else:
self.display_complete = True
def get_displayed_text(self):
surface = MAIN_FONT.render(
self.current_segment['text'][:self.char_index],
True,
GOLDEN
)
surface.set_alpha(self.fade_alpha)
return surface
# Particle Manager
class ParticleSystem:
def __init__(self):
self.particles = []
self.current_phase = None
self.paths = {
'constellation': self._generate_constellation_path(),
'hourglass': self._generate_hourglass_path()
}
def set_phase(self, phase_name):
if phase_name == self.current_phase:
return
self.current_phase = phase_name
self.particles = self._generate_particles(phase_name)
def _generate_particles(self, phase):
particles = []
num_particles = 150
if phase == 'celestial_drift':
for _ in range(num_particles):
x = random.uniform(0, WIDTH)
y = random.uniform(0, HEIGHT/2)
tx = random.uniform(0, WIDTH)
ty = random.uniform(HEIGHT/2, HEIGHT)
particles.append(Particle(
x, y, tx, ty,
CRIMSON,
speed=0.1 + random.random()*0.1,
size=2 + random.random()
))
elif phase == 'constellation':
path = self.paths['constellation']
for i in range(num_particles):
idx = int(i / num_particles * len(path))
point = path[idx]
offset = pygame.Vector2(
random.uniform(-30, 30),
random.uniform(-30, 30)
)
particles.append(Particle(
point.x + offset.x, point.y + offset.y,
point.x, point.y,
AQUAMARINE,
speed=0.3,
size=2
).__dict__)
particles[-1]['path'] = path
elif phase == 'spiraling_glyphs':
for _ in range(num_particles):
angle = random.uniform(0, 2*math.pi)
radius = random.uniform(0, min(WIDTH, HEIGHT)/2)
x = WIDTH/2 + radius * math.cos(angle)
y = HEIGHT/2 + radius * math.sin(angle)
particles.append(Particle(
x, y, x, y,
(255, 255, 200),
speed=0.2
))
elif phase == 'void_emergence':
center = pygame.Vector2(WIDTH/2, HEIGHT/2)
for _ in range(num_particles):
angle = random.uniform(0, 2*math.pi)
radius = random.uniform(0, 150)
x = center.x + radius * math.cos(angle)
y = center.y + radius * math.sin(angle)
particles.append(Particle(
x, y, center.x, center.y,
VIOLET,
speed=0.5,
size=3
))
elif phase == 'hourglass':
path = self.paths['hourglass']
for i in range(num_particles):
idx = int(i / num_particles * len(path))
particles.append(Particle(
path[idx].x, path[idx].y,
path[idx].x, path[idx].y,
STARDUST,
speed=0.2,
size=1.5
).__dict__)
particles[-1]['path'] = path
return particles
def _generate_constellation_path(self):
# Create a celestial pattern path
points = []
center = pygame.Vector2(WIDTH/2, HEIGHT/2)
base_radius = 150
# Star positions
stars = []
for i in range(7):
angle = i * 2*math.pi / 7
stars.append(center + pygame.Vector2(
base_radius * math.cos(angle) * (1.2 if i % 2 else 1),
base_radius * math.sin(angle) * (1.2 if i % 2 else 1)
))
# Connect stars with Bezier curves
for i in range(len(stars)):
p0 = stars[i]
p1 = stars[(i+1)%len(stars)]
p2 = stars[(i+2)%len(stars)]
for t in range(0, 100, 2):
t_norm = t / 100
pt = self._bezier_quad(p0, p1, p2, t_norm)
points.append(pt)
return points
def _generate_hourglass_path(self):
# Hourglass spiral path
points = []
center = pygame.Vector2(WIDTH/2, HEIGHT/2)
for t in range(0, 300):
angle = t * 0.1
radius = 5 + t * 0.3
x = center.x + radius * math.cos(angle)
y = center.y + radius * math.sin(angle) * 0.5
points.append(pygame.Vector2(x, y))
# Lower bulb
for t in range(300, 600):
angle = t * 0.1
radius = 5 + (600 - t) * 0.3
x = center.x + radius * math.cos(angle)
y = center.y + radius * math.sin(angle) * 0.5 + 150
points.append(pygame.Vector2(x, y))
return points
def _bezier_quad(self, p0, p1, p2, t):
"""Quadratic Bezier curve calculation"""
return p0*(1-t)**2 + p1*2*(1-t)*t + p2*t**2
def update(self, dt, phase):
self.set_phase(phase)
for p in self.particles:
p.update(dt, phase)
# Remove dead particles
self.particles = [p for p in self.particles if p.lifespan > 0]
def draw(self, surface):
for p in self.particles:
p.draw(surface)
# Initialize systems
story_engine = StoryEngine()
particle_system = ParticleSystem()
# Main loop
running = True
while running:
dt = clock.tick(FPS)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update systems
story_engine.update(dt)
current_phase = story_segments[story_engine.segment_index]['particles']
particle_system.update(dt, current_phase)
# Draw everything
screen.fill(BLACK)
# Draw title card
title_text = TITLE_FONT.render("Whispers of the Infinite Dunes", True, GOLDEN)
title_rect = title_text.get_rect(center=(WIDTH/2, 40))
screen.blit(title_text, title_rect)
# Draw narrative text
text_surface = story_engine.get_displayed_text()
text_rect = text_surface.get_rect(center=(WIDTH/2, 80))
screen.blit(text_surface, text_rect)
# Draw particles
particle_system.draw(screen)
pygame.display.flip()
pygame.quit()
sys.exit()