青少年python——turtle绘图 模拟3D星空

 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)
        
            


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值