我是Wikipedia的医学编辑(具有接近零的python经验,我们正在尝试建立社交距离影响的模拟。我试图使一堆圆圈在正方形空间中回弹。我让它们从墙壁上弹起,但是我不确定如何检测球之间的碰撞。我创建了一个is_collided_with定义,但是行>>
if is_collided_with(ball, ball): ball.dy *=-1 ball.dx *=-1
冻结所有内容。如果将其卸下,则可以看到运动(根据我的理解,速度可能会因系统而异)。最终目标是要改变颜色,从健康到传染性,再到治愈,并显示与社会隔离如何与众多跟随它的人一起工作。
这是整个代码,
#bouncing balls
import turtle
import random
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("ball simulator")
wn.tracer(0)
balls = []
for _ in range(10):
balls.append(turtle.Turtle())
for ball in balls:
ball.shape("circle")
ball.color("red")
ball.penup()
ball.speed(1)
x = random.randint(-290,290)
y = random.randint(-290,290)
ball.goto(x, y)
ball.dy = (random.randint(-3, 3))/5+.1
ball.dx = (random.randint(-3, 3))/5+.1
def is_collided_with(a, b):
return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 10
while True:
wn.update()
for ball in balls:
ball.sety(ball.ycor() + ball.dy)
ball.setx(ball.xcor() + ball.dx)
#check for a bounce
if is_collided_with(ball, ball):
ball.dy *=-1
ball.dx *=-1
if ball.ycor() <-300:
ball.dy *=-1
if ball.ycor() >+300:
ball.dy *=-1
if ball.xcor() >+300:
ball.dx *=-1
if ball.xcor() <-300:
ball.dx *=-1
wn.mainloop()
我是Wikipedia的医学编辑(具有接近零的python经验,我们正在尝试建立社交距离影响的模拟。我正在尝试使一圈圆圈反弹...
问题是此行: