Boucing balls simulation using python
hello guys it bouncing balls using python.to create the simulation I import Random module & turtle module.
I share the code below that you can easily make it
code is here
import turtle
import random
wn = turtle.Screen()
wn.bgcolor('black')
wn.tracer(0)
bouncing_balls = []
for _ in range(25):
bouncing_balls.append(turtle.Turtle())
colors = ['green','yellow','red','pink','purple','gold','olive','maroon','silver']
shapes = ['triangle','circle','turtle','square']
for ball in bouncing_balls:
ball.shape(random.choice(shapes))
ball.color(random.choice(colors))
ball.up()
ball.speed(0)
x = random.randint(-190,190)
y = random.randint(-190,190)
ball.goto(x,y)
ball.dy = 0
ball.dx = random.randint(-3,3)
ball.da = random.randint(-5,5)
gravity = 0.1
while True:
wn.update()
for ball in bouncing_balls:
ball.rt(ball.da)
ball.dy -= gravity
ball.sety(ball.ycor() + ball.dy)
ball.setx(ball.xcor() + ball.dx)
if ball.xcor() > 190:
ball.dx *= -1
ball.da *= -1
if ball.xcor() < -190:
ball.setx(-190)
ball.dx *= -1
ball.da *= -1
if ball.ycor() < -190:
ball.sety(-190)
ball.dy *= -1
ball.da *= -1
Comments
Post a Comment