520表白——送她一片星空模拟

#代码目标:模拟3D星空
#完整代码评论区留言或者发邮箱

from turtle import *
from random import random,randint

screen = Screen()
width ,height = 1200,600       #画布长宽
screen.setup(width,height)        #输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例

。。。
。。。

printer = Turtle()
printer.hideturtle()
printer.penup()
printer.color('white')

。。。
。。。
。。。

t = Turtle(visible = False,shape='circle')
t.pencolor("white")        #设置画笔的颜色
t.fillcolor("white")       #设置图形填充颜色
t.penup()                  #抬起画笔
t.setheading(-90)          #设置当前朝向角度
t.goto(width/2,randint(-height/2,height/2))  #把画笔移动到定点


stars = []
for i in range(300):
    star = t.clone()        #当前海龟的位置处克隆出另一只位置方向等属性相同的海龟,并且取名叫star
    s =random()/3
    if  s>0.01 and s<0.03:
        star.pencolor("black")
        star.fillcolor("black")
    elif s>0.03 and s<0.04:
        star.pencolor("lightcoral")
        star.fillcolor("lightcoral")    
    elif s>0.05 and s<0.1:
        star.pencolor("green")
        star.fillcolor("green")
    elif s>0.15 and s<0.16:
        star.pencolor("yellow")
        star.fillcolor("yellow")
    elif s>0.19 and s<0.2:
        star.pencolor("red")
        star.fillcolor("red")
    elif s>0.21 and s<0.22:
        star.pencolor("purple")
        star.fillcolor("purple")
    elif s>0.29 and s<0.3:
        star.pencolor("darkorange")
        star.fillcolor("darkorange")   
    elif s>0.31 and s<0.32:
        star.pencolor("red")
        star.fillcolor("yellow")
    elif s>0.32 and s<0.33:
        star.pencolor("yellow")
        star.fillcolor("white")
。。。
。。。
。。。

i = 0
while True:
    i += 0
    for star in stars:

        star.setx(star.xcor() - 3 * star.speed())
        if star.xcor()<-width/2:
            star.hideturtle()
            star.setx(width/2 + randint(1,width))
            star.sety( randint(-height/2,height/2))
            star.showturtle()
    if i>= 100:
        break

效果演示:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值