here2share

# t_Gravity_Bounce_X_and_Y.py

Jan 10th, 2021
848
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # t_Gravity_Bounce_X_and_Y.py
  2.  
  3. import turtle
  4.  
  5. win = turtle.Screen()
  6. win.title('Gravity Bounce X and Y')
  7. win.setup(1000,600)
  8. win.tracer(0) # skips animation until win.update()
  9.  
  10. ball = turtle.Turtle()
  11. ball.shape('circle')
  12. ball.color('red')
  13. ball.up() # lift pen up
  14. ball.goto(-480,250)
  15. ball.dy = 0
  16. ball.dx = 0.5 # will move to the right 1 pixel/loop
  17. ball.down() # put pen down for drawing
  18.  
  19. ground = turtle.Turtle()
  20. ground.shape('square')
  21. ground.shapesize(0.5, 80) # turtle is looking to right, 0.5x and 40y stretch
  22. ground.up()
  23. ground.goto(0, -275)
  24. ground.color('green')
  25.  
  26. gravity = 0.09 # may need adjustment in windows
  27. remaining_energy = 0.9 # increase for more bounce, decrease for less
  28. roll_energy = 0.0008
  29.  
  30.  
  31. while 1:
  32.     win.update()
  33.  
  34.     ball.dy -= gravity  
  35.     ball.goto(ball.xcor()+ball.dx, ball.ycor()+ball.dy)
  36.  
  37.     if ball.ycor() <= -259 and ball.dy<0:
  38.         ball.dy *= -1
  39.         ball.dy *= remaining_energy
  40.        
  41.         if ball.dy < 0.5:
  42.             ball.dy = 0
  43.             gravity  = 0
  44.             remaining_energy = 0
  45.     if not ball.dy:
  46.         ball.dx = max(0, ball.dx-roll_energy)
  47.  
  48.     if not ball.dx:
  49.         break
RAW Paste Data