Question
Class Challenge:
Create a bouncing ball animation using:
speed() for velocity changes
stamp() for impact marks
home() to reset position
then
Create a bouncing ball animation using:
speed() for velocity changes
stamp() for impact marks
home() to reset position
then
Ask by Martin Munoz. in Hong Kong
Mar 27,2025
Upstudy AI Solution
Tutor-Verified Answer
Answer
import turtle
# Set up the screen
wn = turtle.Screen()
wn.bgcolor("white")
wn.tracer(0)
# Create the ball
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.penup()
ball.speed(0)
ball.goto(0, 0)
# Physics parameters
velocity = 5
gravity = -0.1
dy = velocity
while True:
ball.sety(ball.ycor() + dy)
dy += gravity
if ball.ycor() < -200:
ball.stamp()
ball.home()
dy = velocity
wn.update()
wn.mainloop()
Solution
1. Import the turtle module and create the screen.
```python
import turtle
# Set up the window with automatic updates turned off for smoother animation
wn = turtle.Screen()
wn.bgcolor("white")
wn.tracer(0)
- Create the ball turtle and set its properties. We use:
-
to maximize animation performance. -
for a circular ball. -
so that the turtle does not draw lines. -
for a visible ball color.
-
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.penup()
ball.speed(0) # Fastest animation
- Define the physics parameters.
- Let
be the initial upward speed. - Let
be a negative constant that reduces the upward velocity. - Let
represent the change in -position per frame.
- Let
We can start with:
velocity = 5 # initial upward jump speed
gravity = -0.1 # gravity affects the ball by decreasing the speed
dy = velocity # current vertical speed
- Write the animation loop to update the ball’s position. Use the following ideas:
- Update the ball’s
-coordinate: - Update the vertical speed:
- When the ball touches the “floor”, use:
-
to mark the spot of impact. -
to reset the ball’s position to the origin. - Reset
to its initial
-
- Update the ball’s
Let’s assume the floor is defined at
.
while True:
# Move the ball by updating its y-coordinate
ball.sety(ball.ycor() + dy)
# Update the vertical speed (simulate gravity)
dy += gravity
# Check for bounce against a floor at y = -200
if ball.ycor() < -200:
ball.stamp() # Mark the impact position
ball.home() # Reset the ball to the starting position (0,0)
dy = velocity # Reset the velocity for another bounce
wn.update() # Refresh the screen with the new ball position
- Keep the window open until it is closed by the user.
wn.mainloop()
This code creates a bouncing ball animation using the turtle module. We use:
-
to control the drawing speed, -
to leave an impact mark at each bounce, -
to reset the ball’s position, and - A while loop to continuously update the ball’s position according to the physics governed by
and .
Answered by UpStudy AI and reviewed by a Professional Tutor
Like
error msg


Beyond the Answer
Did you know that the concept of a bouncing ball has fascinated scientists and animators alike for generations? It’s based on principles of physics like gravity and energy conservation, which dictate how objects behave. When you animate a ball, you’re not just creating a fun visual; you’re also mimicking these real-world phenomena, giving your project a touch of science!
To elevate your bouncing ball animation, consider using varying speeds for each bounce to simulate real-world conditions like energy loss. For instance, every time the ball hits the ground, applying a little less speed mimics how a real ball never returns to the original height. Don’t forget to play around with colors and shapes when you stamp each impact mark; this adds a fun flair and helps viewers observe the energy dynamics in your animated scene.