Python Forum
bouncing ball with variable collision points (in time)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bouncing ball with variable collision points (in time)
#1
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
Reply
#2
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.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
Star I was making code for collision for my character izmamonke 2 2,054 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,594 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,166 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  The count variable is giving me a hard time in this code D4isyy 2 1,929 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  How can i change the direction of the ball based on player's xcor and ycor collisions MohammedSohail 0 1,724 May-25-2020, 08:39 AM
Last Post: MohammedSohail
  Player object wont recognize collision with other objects. Jan_97 3 2,659 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  AttributeError: 'Ball' object has no attribute 'hit_paddle' meza1123 1 3,465 Jan-30-2019, 07:25 PM
Last Post: buran
  Would like to input a date variable and determine whether it is within the time range harold 3 2,513 Jan-05-2019, 09:04 AM
Last Post: Gribouillis
  Could not build the ssl module while installing python3.7 from tar ball badfish 1 7,787 Dec-11-2018, 04:33 AM
Last Post: badfish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020