Bouncing Ball¶
By the end of this lab you'll be able to:
- Animate a turtle by updating its position in a loop using velocity variables
- Reverse velocity when the ball hits a wall (
vx = -vx) - Use
turtle.tracer(0)andturtle.update()for smooth animation
A colored ball bounces around inside a rectangular boundary. Each frame, the ball moves by its velocity, and when it hits a wall, the velocity component reverses — simulating elastic collision.
Welcome to Bouncing Ball!
This is your first animated turtle program — the turtle moves continuously
inside a loop, and you see smooth motion on screen!
Let's code it together!
How It Works¶
Two variables vx and vy store the ball's velocity. Each step: x += vx, y += vy. If x hits the wall bounds, vx = -vx (reverse horizontal direction). If y hits the bounds, vy = -vy. turtle.tracer(0) turns off auto-drawing so we control when to refresh the screen.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
What Do You Think Will Happen?
The ball starts at (0,0) with velocity (3,2).
After bouncing off all four walls many times, will it return to (0,0)?
Make your guess — then click Run to find out!
Try It Now¶
The ball bounces around the box — reversing direction whenever it hits a wall. Were you right about returning to the origin?
How It Works¶
screen.tracer(0) tells turtle not to draw automatically after each command. screen.update() forces a single screen redraw. This gives smooth animation instead of flicker. The ball's position (x, y) is updated each step, and the boundary check x > boundary detects wall collisions.
Explanation Table¶
| Line | What it does |
|---|---|
screen.tracer(0) |
Disable auto-update for smooth animation |
vx, vy = 3, 2 |
Initial velocity vector |
if x > boundary: vx = -vx |
Reverse x velocity on wall hit |
screen.update() |
Redraw the screen (one frame) |
Learning Check¶
Your Turn — Add a Trail
Add ball.pendown() before the loop to leave a trail.
The ball's path will trace out a geometric pattern — predict what it looks like!
The trail traces out a Lissajous-like figure! With vx=5, vy=3 (ratio 5:3), the path eventually returns to its starting point after tracing a complex geometric pattern.
Experiments¶
-
Make the ball leave a color trail. Change the color each time a wall is hit. You'll know it worked when the trail changes color at each bounce.
-
Two balls. Create a second turtle
ball2with a different starting velocity. You'll know it worked when two balls bounce independently. -
Speed up over time. Add
vx *= 1.002; vy *= 1.002each step. You'll know it worked when the ball gradually bounces faster. -
Gravity simulation. Add
vy -= 0.1each step and setif y < -boundary: vy = abs(vy) * 0.9. You'll know it worked when the ball falls and bounces like gravity.
Animated!
You wrote your first animation loop — position, velocity, collision detection!
These are the building blocks of every physics-based video game.
Up next: Clock Face — animated hands that show the current time.