Bouncing Colors & Light Trails
In Chapter 3, stars drifted peacefully across a void. Nobody pushed back. In this project, the walls push back.
We’re going to build a physical sandbox — glowing neon spheres that bounce off the walls of the Pygame window with real collision math, leaving behind trails of fading light. The same project you built in the HTML book, now running as a standalone desktop application with Pygame’s hardware‑accelerated renderer behind it.
The bouncing will feel crisper. The trails will glow brighter. And the knobs you twist will be more dramatic.
Five large neon spheres move in random directions inside a dark window. Every time a sphere hits a wall, it bounces back with perfect physics. Instead of clearing the screen completely between frames, we paint a semi‑transparent dark layer over it — so old positions fade slowly, leaving gorgeous glowing trails that linger and overlap.
Copy and paste this prompt into your AI assistant:
Once the AI hands you the code:
Copy the AI’s output to your clipboard.
Create a new file with File → New File.
Save the file as bouncers.py on your Desktop.
Press F5. Five glowing spheres, physics, trails. Watch them for a moment — then let’s get our hands in the engine.
Time to experiment. These are the three knobs that change everything.
trail_surface.fill((0, 0, 0, 15)) # Last number is the alpha
screen.blit(trail_surface, (0, 0))
The last number — 15 — controls how quickly old frames fade.
The tweak: Change 15 to 3.
The action: Save and press F5.
Now the trails barely fade at all. Your spheres paint permanent neon signatures until the screen becomes a dense web of glowing geometry.
The tweak: Change it to random.randint(80, 120).
The action: Save and press F5.
Tiny marbles become massive boulders, barely fitting inside the window, crashing into walls in slow, spectacular collisions.
ball[“vx”] = -ball[“vx”]
The tweak: Change -ball["vx"] to -ball["vx"] * 1.05 (and do the same for vy).
The action: Save and press F5.
Every bounce adds 5% more speed. The balls start slow, then within seconds they’re ricocheting so violently they blur into streaks of pure color.
Without memorizing anything, you just picked up:
- Surface alpha blending: Pygame’s
SRCALPHAflag lets you create transparent surfaces. Blitting a nearly invisible dark layer each frame creates motion blur and trail effects. - Velocity vectors: Movement is stored as
vxandvy. Inverting one reverses direction. Multiplying before inverting adds energy. - Dictionary objects: The AI likely stored each ball’s properties in a dictionary —
ball["x"],ball["radius"], etc. A dictionary is just a bundle of named values, perfect for organizing related data.