Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("Bouncing Ball Simulator")
- wn.tracer(0)
- balls = []
- for b in range(30):
- balls.append(turtle.Turtle())
- colors = ["magenta", "cyan", "orange", "yellow", "hot pink", "lime"]
- shapes = ["circle", "triangle", "square"]
- for ball in balls:
- ball.shape("circle")
- ball.color(random.choice(colors))
- ball.penup()
- ball.speed(0)
- x = random.randint( - 290, 290)
- y = random.randint(- 290, 290)
- ball.goto(x, 200)
- ball.dy = 0 #how high up the y axis the ball bounces
- ball.dx = random.randint(-3, 3)
- ball.da = random.randint(-5, 5)
- gravity = 0.1 #Gravity motion on ball
- while True:
- wn.update()
- for ball in balls:
- ball.rt(ball.da)
- ball.dy -= gravity #Ball speeding up as it falls
- ball.sety(ball.ycor() + ball.dy)
- ball.setx(ball.xcor() + ball.dx)
- if ball.xcor() > 300: #where does ball bounce on x axis
- ball.dx *= -1
- ball.da *= -1
- if ball.xcor() < -300: #where does ball bounce on other side of x axis
- ball.setx(-300)
- ball.dx *= -1
- ball.da *= -1
- if ball.ycor() < -300: #where does the ball bounce on y axis
- ball.sety(-300)
- ball.dy *= -1
- ball.da *= -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement