Python Bouncing Colors

Lessons Python Book Chapter 4
Python Book · Chapter 4

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.

Chapter 4 of 6

Neon spheres painting the dark.

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.


The prompt recipe.

Copy and paste this prompt into your AI assistant:

“Write a complete Python script using Pygame that builds a bouncing color trails sandbox. Include: 1. A Pygame window sized 900×600 with a dark background. 2. A list of 5 large spheres, each with a random starting position, random velocity, a radius of about 20 pixels, and a bright neon color. 3. Boundary collision logic: when a sphere’s edge touches any wall of the window, invert the relevant velocity component so it bounces realistically. 4. Trail effect: instead of clearing the screen every frame, blit a semi-transparent dark surface over the window each frame. This creates fading light trails behind the moving spheres. 5. Clean, readable code with comments clearly marking where ‘sphere count’, ‘radius’, ‘speed’, and ‘trail fade rate’ are defined.”

Assemble the sandbox.

Once the AI hands you the code:

1
Copy the code

Copy the AI’s output to your clipboard.

2
Open IDLE

Create a new file with File → New File.

3
Paste and save

Save the file as bouncers.py on your Desktop.

4
Run it

Press F5. Five glowing spheres, physics, trails. Watch them for a moment — then let’s get our hands in the engine.


Twisting the knobs (how to play).

Time to experiment. These are the three knobs that change everything.

👻
Knob 1: Adjust the ghost trails (the decay rate)
Look inside the main game loop for the trail effect:
trail_surface = pygame.Surface((900, 600), pygame.SRCALPHA)
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.

🔵
Knob 2: Inflate the marbles (the radius)
Find the radius value:
radius = 20
or:
“radius”: random.randint(10, 25)

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.

Knob 3: Defy gravity (kinetic acceleration)
Find the bounce logic:
if ball[“x”] + ball[“radius”] >= WIDTH or ball[“x”] – ball[“radius”] <= 0:
  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.


What you just learned (naturally).

Without memorizing anything, you just picked up:

  • Surface alpha blending: Pygame’s SRCALPHA flag 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 vx and vy. 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.
You’re in Python Book · Chapter 4 of 6
Play-First Programming · playfirstprogramming.com A clubhouse for curious builders