Python用turtle模块画一棵漂亮的星星树

Python用turtle模块画一棵漂亮的星星树

童话中,树上长满星星,五颜六色的,非常漂亮的星星树
今天,用Python代码可以实现,电脑帮你画出来,每次执行都完全随机
五角星拥有随机的颜色和大小,如图
Python画的星星树
下面是我写的源代码,相当简短哦

import turtle , random
turtle.screensize(900,600,'papayawhip')
turtle.title('星星树-羊')
turtle.speed(0)
turtle.hideturtle() 
def star(n):
    r,g,b=(random.randint(1,10)/10 for i in range(3))
    turtle.color(r,g,b)
    turtle.begin_fill()
    for i in range(5):
        turtle.fd(n)
        turtle.left(144)
    turtle.end_fill()
def tree(m,l):
    turtle.pensize(m*2)
    turtle.color('saddlebrown')
    turtle.pd()
    d = l+(random.randint(0,8)) 
    turtle.fd(d)
    if m>0:
        b = random.randint(5,12)+10 
        c = random.randint(5,12)+10 
        turtle.right(b)
        tree(m-1,d)
        turtle.left(b+c)
        tree(m-1,d)
        turtle.right(c)
    else:
        star(random.randint(20,50))
    turtle.pu()
    turtle.backward(d)
def main():
    turtle.pu()
    turtle.goto(0,-300)
    turtle.left(90)
    tree(7,35)
if __name__ == "__main__":
    main()
    turtle.done()
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页