Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- # Step 1: Getting Started
- wn = turtle.Screen()
- wn.bgcolor("maroon")
- wn.title("Bouncing Ball Simulator")
- wn.tracer(0)
- balls = []
- for _ in range (0):
- balls.append(turtle.Turtle())
- colors = ["indian red", "light salmon", "orange", "white", "salmon"]
- shapes = ["circle", "triangle", "square"]
- for ball in balls:
- ball.penup()
- ball.shape(random.choice(shapes))
- ball.color(random.choice(colors))
- ball.speed(0)
- x = random.randint (-290, 290)
- y = random.randint (200, 400)
- ball.goto(x, y)
- ball.dy = 0
- ball.dx = random.randint(-3, 3)
- ball.da = random.randint(-5, 5)
- def add_ball(x,y):
- ball = turtle.Turtle()
- ball.penup()
- ball.shape(random.choice(shapes))
- ball.color(random.choice(colors))
- ball.speed(0)
- #x = random.randint (-290, 290)
- #y = random.randint (200, 400)
- ball.goto(x, y)
- ball.dy = 0
- ball.dx = random.randint(-3, 3)
- ball.da = random.randint(-5, 5)
- balls.append(ball)
- print(len(balls))
- add_ball(0,0)
- gravity = 0.2
- wn.onclick(add_ball)
- while True:
- wn.update()
- for ball in balls:
- ball.rt(ball.da)
- ball.dy -= gravity
- ball.sety(ball.ycor() + ball.dy)
- ball.setx(ball.xcor() + ball.dx)
- # Check for a ball collision
- if ball.xcor() > 300:
- ball.dx *= -1
- ball.dx *= -1
- if ball.xcor() < -300:
- ball.dx *= -1
- ball.dx *= -1
- # Step 2: Check for a Bounce
- if ball.ycor() < -300:
- ball.sety(-300)
- ball.dy *= -1
- ball.dx *= -1
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement