画树步骤二,蓝桥杯创意Python省赛分形树丛绘制参考答案

画树步骤二,蓝桥杯创意Python省赛分形树丛绘制参考答案

Python创意画从林

"""
    画树步骤二,蓝桥杯创意Python省赛分形树丛绘制参考答案
    python闪电画从林.
"""

from turtle import *
from random import *

def draw_tree(t,n,length):
    """
       画树的递归函数
    """
    k = - x/abs(x)
    if n > 0:
        t.fd(length)
        t.left(k * 30)
        draw_tree(t,n-1,length-6)
        t.right(k * 15)
        draw_tree(t,n-1,length-6)
        t.right(k * 15)
        t.bk(length)

width,height = 800,600
screen = Screen()
screen.setup(width,height)
screen.title('蓝桥杯创意Python省赛分形树丛绘制参考答案by 李兴球')
screen.tracer(0)

t = Turtle()
t.left(90)
t.penup()

for _ in range(50):
    x = randint(-width//2,width//2)
    y = randint(-height//2,height//2)
    while x == 0 :
        x = randint(-width//2,width//2)    
    t.goto(x,y)
    n = randint(5,7)
    d = randint(30,60)
    t.pendown()
    draw_tree(t,n,d)    
    t.color('white')
    t.fd(d)
    t.color('black')
    t.penup()
screen.update()
screen.mainloop()

 

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。