TNT Tool Time — Entry #7 — Python Processing
Xmas Lights
Stars that twinkle like Christmas lights — and a tree waiting to be decorated.
Pure Python Processing. Copy it, run it, then ask AI to build a Christmas tree and
constrain the lights to it. Holiday spirit, powered by code.
Twinkling Stars That Look Like Christmas Lights
This Python Processing sketch was created as group work in TNT’s Foundations class. It draws 400 twinkling colored lights (styled as stars) against a deep purple night sky. Each “star” cycles independently through the full color spectrum while its brightness pulses smoothly — the effect looks remarkably like a string of Christmas lights.
We are showcasing this as-is, then using AI to transform it: draw a polygonal Christmas tree on the canvas and constrain all the lights to fall only on the tree. Copy the code, paste it into Python Processing, and watch it run. Then start the holiday adventure.
Note on randomSeed(100):
The star positions are recalculated every frame, but randomSeed(100) resets the random number sequence each time — so the positions never change. The animation is the color and brightness changing, not the positions. Remove the seed and watch what happens.
Your Turn — Get Into the Holiday Spirit
Hit Copy above, paste into Python Processing (File → New, then paste), and run it. The lights twinkle immediately. Then follow the stages below.
💻 Stage 1 — Run It, Understand It
Watch the animation. Notice:
- Colors cycle independently —
hue_family = map(sin(frameCount*.05 + 5*ndx), -1, 1, 0, 360)gives each star a different phase in the color cycle. - Brightness pulses —
glow = map(sin(frameCount*.1 + ndx), -1, 1, 0, 100)makes each star fade in and out at a slightly different rate. - Positions never move —
randomSeed(100)resets the sequence every frame. Remove it and see what happens!
🌲 Stage 2 — Draw a Christmas Tree
Ask your AI assistant:
You are a patient Python Processing teacher. I have a twinkling stars sketch. Add a function draw_tree() that draws a simple Christmas tree as a green filled triangle using beginShape(), vertex(), and endShape(CLOSE). Center the tree horizontally on the 600-wide canvas, with the tip at y=50 and the base at y=360. Add a brown rectangle for the trunk. Call draw_tree() from draw(), after the background and before draw_stars().
📌 Stage 3 — Constrain Lights to the Tree
Right now stars appear everywhere. After drawing the tree, modify draw_stars() so lights only appear inside the triangle. Ask AI to add a point_in_triangle() helper and wrap each circle in an if check:
def point_in_triangle(px, py, ax, ay, bx, by, cx, cy):
# Returns True if (px, py) is inside the triangle (ax,ay),(bx,by),(cx,cy)
def sign(p1x, p1y, p2x, p2y, p3x, p3y):
return (p1x - p3x) * (p2y - p3y) - (p2x - p3x) * (p1y - p3y)
d1 = sign(px, py, ax, ay, bx, by)
d2 = sign(px, py, bx, by, cx, cy)
d3 = sign(px, py, cx, cy, ax, ay)
has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
return not (has_neg and has_pos)
⭐ Stage 4 — Decorate!
Once the lights are on the tree, keep going:
Full C.R.A.F.T. breakdown at Tool Time Home ↓