python语言的特点:面向对象的编程
界面内有多个星星对象,由克隆函数实现turtle.clone()
将星星追加至列表中,循环遍历列表,实现逐个选中星星
import turtle,random screen = turtle.Screen() screen.delay(0)#这里要设为0,否则很卡 width ,height = 676,506 screen.setup(width,height) screen.title("模拟3D星空") #screen.bgpic('1.png') screen.bgcolor('black') #t = Turtle(visible = False,shape='circle') t=turtle.Pen() t.hideturtle() t.shape('circle') t.color('white') t.penup() stars = [] while 1: star = t.clone() s =random.uniform(0.1,0.5) #s=random.randint(1,5)/10 star.shapesize(s) star.speed(int(s*10)) star.setx(width/2 + random.randint(1,width)) star.sety( random.randint(-height/2,height/2)) star.showturtle() stars.append(star) for star in stars: star.setx(star.xcor() - 3*s*10)