Advertisement
Guest User

Bouncing Ball Simulator - AG

a guest
Jul 22nd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import turtle
  2. import random
  3.  
  4. wn = turtle.Screen()
  5. wn.bgcolor("black")
  6. wn.title("Bouncing Ball Simulator")
  7. wn.tracer(0)
  8.  
  9. balls = []
  10.  
  11. for b in range(30):
  12.     balls.append(turtle.Turtle())
  13.  
  14. colors = ["magenta", "cyan", "orange", "yellow", "hot pink", "lime"]
  15. shapes = ["circle", "triangle", "square"]
  16.  
  17. for ball in balls:
  18.     ball.shape("circle")
  19.     ball.color(random.choice(colors))
  20.     ball.penup()
  21.     ball.speed(0)
  22.     x = random.randint( -   290, 290)
  23.     y = random.randint(- 290, 290)
  24.     ball.goto(x, 200)
  25.     ball.dy = 0 #how high up the y axis the ball bounces
  26.     ball.dx = random.randint(-3, 3)
  27.     ball.da = random.randint(-5, 5)
  28.  
  29. gravity = 0.1 #Gravity motion on ball
  30.  
  31. while True:
  32.     wn.update()
  33.  
  34.     for ball in balls:
  35.         ball.rt(ball.da)
  36.         ball.dy -= gravity #Ball speeding up as it falls
  37.         ball.sety(ball.ycor() + ball.dy)
  38.  
  39.         ball.setx(ball.xcor() + ball.dx)
  40.  
  41.         if ball.xcor() > 300: #where does ball bounce on x axis
  42.             ball.dx *= -1
  43.             ball.da *= -1
  44.            
  45.         if ball.xcor() < -300: #where does ball bounce on other side of x axis
  46.             ball.setx(-300)
  47.             ball.dx *= -1
  48.             ball.da *= -1
  49.            
  50.         if ball.ycor() < -300: #where does the ball bounce on y axis
  51.             ball.sety(-300)
  52.             ball.dy *= -1
  53.             ball.da *= -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement