Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # t_Gravity_Bounce_X_and_Y.py
- import turtle
- win = turtle.Screen()
- win.title('Gravity Bounce X and Y')
- win.setup(1000,600)
- win.tracer(0) # skips animation until win.update()
- ball = turtle.Turtle()
- ball.shape('circle')
- ball.color('red')
- ball.up() # lift pen up
- ball.goto(-480,250)
- ball.dy = 0
- ball.dx = 0.5 # will move to the right 1 pixel/loop
- ball.down() # put pen down for drawing
- ground = turtle.Turtle()
- ground.shape('square')
- ground.shapesize(0.5, 80) # turtle is looking to right, 0.5x and 40y stretch
- ground.up()
- ground.goto(0, -275)
- ground.color('green')
- gravity = 0.09 # may need adjustment in windows
- remaining_energy = 0.9 # increase for more bounce, decrease for less
- roll_energy = 0.0008
- win.update()
- while 1:
- ball.dy -= gravity
- ball.goto(ball.xcor()+ball.dx, ball.ycor()+ball.dy)
- if ball.ycor() <= -259 and ball.dy<0:
- ball.dy *= -1
- ball.dy *= remaining_energy
- if ball.dy < 0.5:
- ball.dy = 0
- gravity = 0
- remaining_energy = 0
- if not ball.dy:
- ball.dx = max(0, ball.dx-roll_energy)
- if not ball.dx:
- break
Add Comment
Please, Sign In to add comment