Python画樱花树的代码

 Python画樱花树的代码_第1张图片

 不废话,直接上代码:

import turtle
import random

def draw_sakura(branch_len):
    if branch_len > 3:
        if 8 <= branch_len <= 12:
            if random.randint(0, 2) == 0:
                turtle.color('snow')  # 樱花的颜色
            else:
                turtle.color('lightcoral')
            turtle.pensize(branch_len / 3)
        elif branch_len < 8:
            if random.randint(0, 1) == 0:
                turtle.color('snow')
            else:
                turtle.color('lightcoral')
            turtle.pensize(branch_len / 2)
        else:
            turtle.color('sienna')
            turtle.pensize(branch_len / 10)
        turtle.forward(branch_len)
        a = 1.5 * random.random()
        turtle.right(20 * a)
        b = 1.5 * random.random()
        draw_sakura(branch_len - 10 * b)
        turtle.left(40 * a)
        draw_sakura(branch_len - 10 * b)
        turtle.right(20 * a)
        turtle.up()
        turtle.backward(branch_len)
        turtle.down()

def draw_sky():
    turtle.color("midnightblue")
    turtle.fillcolor("midnightblue")
    turtle.begin_fill()
    turtle.up()
    turtle.goto(-300, 400)
    turtle.down()
    turtle.goto(300, 400)
    turtle.goto(300, -400)
    turtle.goto(-300, -400)
    turtle.goto(-300, 400)
    turtle.end_fill()

def draw_ground():
    turtle.color("lightgrey")
    turtle.fillcolor("lightgrey")
    turtle.begin_fill()
    turtle.up()
    turtle.goto(-300, -400)
    turtle.down()
    turtle.goto(300, -400)
    turtle.goto(300, -500)
    turtle.goto(-300, -500)
    turtle.goto(-300, -400)
    turtle.end_fill()

def main():
    turtle.setup(800, 600)
    turtle.speed(0)
    turtle.left(90)
    turtle.up()
    turtle.backward(150)
    turtle.down()
    draw_sky()
    draw_ground()
    draw_sakura(80)
    turtle.hideturtle()
    turtle.done()

if __name__ == '__main__':
    main()

你可能感兴趣的:(python,python,开发语言)