Python Forum

Full Version: bouncing ball with variable collision points (in time)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody.
I have a piece of code, generating a bouncing ball with equal time intervals (collision points occur in equal time durations). What I need to do is making this ball to occur in rhythmic intervals and I want to determine this time myself. So to conclude, I want a ball that hit the floor for example 6 times and time duration between collisions are determined by myself.

here is the code I have. what should I do to change is to what I want?
import turtle
wn=turtle.Screen()
wn.bgcolor('black')
wn.title('bouncing ball simulator')

ball=turtle.Turtle()
ball.shape('circle')
ball.color('green')
ball.penup()
ball.speed(0)
ball.goto(0,200)
ball.dy=0

gravity=0.1

while True:
ball.dy-=gravity
ball.sety(ball.ycor()+ball.dy)
if ball.ycor()<-300:
ball.dy*=-1
First off, please edit your post, select the code part and click the python code button (blue/yellow snakes) to format it properly.

For your question, you can adjust the speed of the ball and hence, its timing by changing the speed you update the screen OR by changing how far it jumps with each frame.

You can slow the update rate by adding a delay in your while loop. Try importing time and use the time.sleep() function. It's in seconds so you will want to use a small value like 0.01.

Play with your gravity variable to make the ball "move" faster although you will have to compensate for the bounce getting lower each time through.

If you need to get any more fancy with this app I suggest learning pygame or Vpython. Turtle is pretty limited.