Lesson 45 of 51 · Animations
Bouncing Ball
Simulate a bouncing ball with simple physics.
Python
from turtle import *
bgcolor("black")
shape("circle")
color("orange")
penup()
speed(0)
x, y = 0, 0
dx, dy = 3, 4
for frame in range(300):
x += dx
y += dy
if x > 180 or x < -180:
dx = -dx
if y > 180 or y < -180:
dy = -dy
goto(x, y)
update()
done()Output

1
Velocity
dx and dy control the ball's horizontal and vertical speed. Flip them when hitting a wall.