源码见:https://want595.blog.csdn.net/

1.模拟星球
我们将每一个星球看成一个质点,每一个质点用小海龟表示,通过克隆小海龟生成一个个星球
t=tu.Turtle(visible=False,shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.goto(ra.randint(width/2,width),ra.randint(-height/2,height/2))
for i in range(99):
star=t.clone()
s=ra.uniform(0,1)/3
star.shapesize(s,s)
star.speed(ra.randint(2,5))
star.setx(ra.randint(width/2,width))
star.sety(ra.randint(-height/2,height/2))
star.showturtle()
stars.append(star)
2.浪漫星空
通过循环来模拟星空
当每个星球从左边消失时,我们将他从新加入右侧,继续循环
while True:
for star in stars:
star.setx(star.xcor()-star.speed())
if star.xcor()<-width/2:
star.hideturtle()
star.setx(ra.randint(width/2,width))
star.sety(ra.randint(-height/2,height/2))
star.showturtle()