论如何使用python绘制一个樱花

如何使用python绘制樱花树呢?

今天,我就把代码给你。


import turtle as T
import random
import time


T.speed(11111111111)
def Tree(branch, t):
    time.sleep(0.0001)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)

# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle()  # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(50, t)
w.exitonclick()

以上代码可以绘制出一个如图的樱花树

那么,还有一个代码,可以画出圣诞树

from turtle import *
import time

#圣诞树主干
class TreeBackBone():
    def __init__(self):
        pencolor("Green")
        pensize(10)
        self.layer1()
        self.layer2()
        self.layer3()
        self.layer4()
        self.trunk()

    def layer1(self):
        penup()
        hideturtle()
        goto(0, 150)
        showturtle()
        pendown()
        shape(name="classic")
        seth(-120)
        for i in range(10):
            fd(12)
            right(2)
        penup()
        goto(0, 150)
        seth(-60)
        pendown()
        for i in range(10):
            fd(12)
            left(2)
        seth(-150)
        penup()
        fd(10)
        pendown()
        for i in range(5):
            fd(10)
            right(15)
        seth(-150)
        penup()
        fd(8)
        pendown()
        for i in range(5):
            fd(10)
            right(15)
        seth(-155)
        penup()
        fd(5)
        pendown()
        for i in range(5):
            fd(7)
            right(15)

    def layer2(self):
        penup()
        goto(-55, 34)
        pendown()
        seth(-120)
        for i in range(10):
            fd(8)
            right(5)

        penup()
        goto(50, 35)
        seth(-60)
        pendown()
        for i in range(10):
            fd(8)
            left(5)
        seth(-120)
        penup()
        fd(10)
        seth(-145)
        pendown()
        for i in range(5):
            fd(10)
            right(15)
        penup()
        fd(10)
        seth(-145)
        pendown()
        for i in range(5):
            fd(12)
            right(15)
        penup()
        fd(8)
        seth(-145)
        pendown()
        for i in range(5):
            fd(10)
            right(15)
        penup()
        seth(-155)
        fd(8)
        pendown()
        for i in range(5):
            fd(11)
            right(15)

    def layer3(self):
        penup()
        goto(-100, -40)
        seth(-120)
        pendown()
        for i in range(10):
            fd(6)
            right(3)
        penup()
        goto(80, -39)
        seth(-50)
        pendown()
        for i in range(10):
            fd(6)
            left(3)
        seth(-155)
        penup()
        fd(10)
        pendown()
        for i in range(5):
            fd(8)
            right(10)
        penup()
        fd(8)
        seth(-145)
        pendown()
        for i in range(7):
            fd(8)
            right(10)
        penup()
        fd(8)
        seth(-145)
        pendown()
        for i in range(7):
            fd(7)
            right(10)
        penup()
        fd(8)
        seth(-145)
        pendown()
        for i in range(7):
            fd(7)
            right(10)
        penup()
        fd(8)
        seth(-140)
        pendown()
        for i in range(7):
            fd(6)
            right(10)

    def layer4(self):
        penup()
        goto(-120, -95)
        seth(-130)
        pendown()
        for i in range(7):
            fd(10)
            right(5)
        penup()
        goto(100, -95)
        seth(-50)
        pendown()
        for i in range(7):
            fd(10)
            left(5)
        penup()
        seth(-120)
        fd(10)
        seth(-155)
        pendown()
        for i in range(6):
            fd(8)
            right(10)
        penup()
        seth(-160)
        fd(10)
        seth(-155)
        pendown()
        for i in range(6):
            fd(8)
            right(10)
        penup()
        seth(-160)
        fd(10)
        seth(-155)
        pendown()
        for i in range(6):
            fd(8)
            right(10)
        penup()
        seth(-160)
        fd(10)
        seth(-160)
        pendown()
        for i in range(6):
            fd(8)
            right(10)
        penup()
        seth(-160)
        fd(10)
        seth(-160)
        pendown()
        for i in range(6):
            fd(8)
            right(10)
        penup()
        seth(-160)
        fd(10)
        seth(-165)
        pendown()
        for i in range(5):
            fd(10)
            right(11)

    def trunk(self):
        penup()
        goto(-70, -165)
        seth(-85)
        pendown()
        for i in range(3):
            fd(5)
            left(3)
        penup()
        goto(70, -165)
        seth(-95)
        pendown()
        for i in range(3):
            fd(5)
            right(3)
        seth(-170)
        penup()
        fd(10)
        pendown()
        pendown()
        for i in range(10):
            fd(12)
            right(2)
        penup()
        goto(70, -165)
        pendown()
        seth(-90)
        pensize(8)
        pencolor("DarkSeaGreen")
        circle(-20, 90)

        penup()
        goto(30, -185)
        pendown()
        seth(-180)
        pensize(8)
        pencolor("DarkSeaGreen")
        fd(40)

        penup()
        goto(-5, -170)
        pendown()
        seth(-180)
        pensize(8)
        pencolor("DarkSeaGreen")
        fd(35)

        right_up(-70, -150, 160)
        right_up(100, -150, 160)
        right_down(110, -110, 50)
        right_up(160, -140, 150)
        clockwise(80, -120, 180)
        right_up(70, -85, 165)
        right_up(-40, -85, 165)
        right_down(90, -50, 50)
        right_up(130, -80, 150)
        pencolor("Green")
        clockwise(-40, -60, 180)
        pencolor('DarkSeaGreen')
        clockwise(80, -30, 180)
        pencolor("Green")
        clockwise(40, 10, 180)
        pencolor("DarkSeaGreen")
        right_up(-60, 30, 120)
        right_up(-20, -20, 150)
        right_down(45, 40, 60)
        right_up(-30, 40, 170)
        right_up(-30, 110, 115)
        right_down(40, 90, 60)
        right_up(80, 50, 160)
        pencolor("DarkSeaGreen")

#圣诞树装饰
class ChristmasTree():
    def __init__(self):        
        speed(0)
        TreeBackBone()
        pencolor("#de8891")
        self.small_bowknots()
        self.big_bowknots()
        self.christmashats()
        self.stars()
        self.socks()
        penup()
        seth(0)
        goto(100, -230)
        pendown()
        write("Merry Christmas ", align="center", font=("Comic Sans MS", 24, "bold"))
        done()

    def small_bowknots(self):
        seth(0)
        unit(40, -160)
        basebowknot(-80, -120)
        red_anticlockwise(-67, -115, 120)
        red_anticlockwise(-86, -123, 150)
        basebowknot(40, -50)
        red_anticlockwise(52, -45, 130)
        red_anticlockwise(34, -55, 160)
        seth(0)
        unit(-20, -60)
        red_anticlockwise(-4, -60, 100)
        red_anticlockwise(-20, -60, 120)
        basebowknot(-30, 20)
        red_anticlockwise(-15, 25, 130)
        red_anticlockwise(-40, 20, 180)
        unit(30, 70)
        red_anticlockwise(45, 70, 100)
        red_anticlockwise(30, 70, 120)

    def big_bowknots(self):
        pencolor("red")
        pensize(5)
        penup()
        seth(0)
        goto(0, 150)
        pendown()
        circle(10)
        seth(-15)
        fd(40)
        seth(90)
        fd(40)
        seth(200)
        fd(40)
        seth(160)
        fd(40)
        seth(-90)
        fd(40)
        seth(15)
        fd(40)
        seth(-70)
        pencolor("red")
        pensize(4)
        fd(40)
        seth(-180)
        fd(10)
        seth(100)
        fd(40)
        seth(-100)
        fd(40)
        seth(-180)
        fd(10)
        seth(70)
        fd(40)
        penup()
        seth(0)
        goto(0, 130)
        pencolor("pink")
        pendown()

    def christmashats(self):
        seth(0)
        pink_anticlockwise(35, 145, 100)
        pink_anticlockwise(-7, 145, 110)
        pencolor("red")
        pensize(7)
        penup()
        goto(-35, 135)
        pendown()
        seth(-20)
        pensize(2)
        penup()
        goto(-30, -120)
        pencolor("black")
        pendown()
        fillcolor("red")
        fd(30)
        circle(4, 180)
        fd(30)
        circle(4, 180)
        penup()
        goto(-25, -115)
        seth(75)
        pendown()
        begin_fill()
        for i in range(5):
            fd(6)
            right(20)
        seth(-10)
        for i in range(5):
            fd(8)
            right(15)
        seth(145)
        for i in range(5):
            fd(5)
            left(2)
        seth(90)
        for i in range(5):
            fd(1)
            left(2)
        seth(-90)
        for i in range(4):
            fd(4)
            right(6)
        seth(161)
        fd(30)
        end_fill()
        pensize(1)
        pencolor("black")

    def stars(self):
        seth(-15)
        star(-120, -70, 10)
        seth(10)
        star(100, -20, 10)
        seth(-10)
        star(10, 40, 10)
        seth(30)
        star(-80, 60, 10)
        star(100, -150, 10)
        star(-140, -150, 10)
        star(20, 120, 10)

    def socks(self):
        seth(-20)
        pensize(2)
        penup()
        goto(-20, 80)
        pencolor("black")
        pendown()
        fillcolor("red")
        fd(25)
        circle(4, 180)
        fd(25)
        circle(4, 180)
        penup()
        goto(-15, 80)
        pendown()
        begin_fill()
        fillcolor("red")
        seth(-120)
        fd(20)
        seth(150)
        fd(5)
        circle(7, 180)
        fd(15)
        circle(5, 90)
        fd(30)
        seth(160)
        fd(18)
        end_fill()


def basebowknot(x, y):
    penup()
    goto(x, y)
    seth(80)
    pendown()
    pensize(2)
    circle(5)
    seth(10)
    fd(15)
    seth(120)
    fd(20)
    seth(240)
    fd(20)
    seth(180)
    fd(20)
    seth(-60)
    fd(20)
    seth(50)
    fd(20)
    seth(-40)
    fd(30)
    seth(-130)
    fd(5)
    seth(135)
    fd(30)
    seth(-60)
    fd(30)
    seth(-150)
    fd(6)
    seth(110)
    fd(30)


def right_up(x, y, z):
    penup()
    goto(x, y)
    seth(-z)
    pendown()
    for angel in range(5):
        fd(10)
        right(10)


def right_down(x, y, z):
    penup()
    goto(x, y)
    seth(-z)
    pendown()
    for angel in range(5):
        fd(10)
        left(10)


def clockwise(x, y, z):
    penup()
    goto(x, y)
    seth(-z)
    pendown()
    for angel in range(5):
        fd(6)
        right(10)
    seth(-150)
    fd(20)


def unit(x, y):
    penup()
    goto(x, y)
    pendown()
    pensize(2)
    circle(5)
    seth(-10)
    fd(15)
    seth(90)
    fd(15)
    seth(200)
    fd(15)
    seth(160)
    fd(15)
    seth(-90)
    fd(15)
    seth(10)
    fd(15)
    seth(-60)
    fd(20)
    seth(-180)
    fd(5)
    seth(110)
    fd(20)
    seth(-90)
    fd(20)
    seth(-180)
    fd(6)
    seth(70)
    fd(15)
    hideturtle()


def red_anticlockwise(x, y, z):
    penup()
    goto(x, y)
    pendown()
    seth(z)
    for po in range(5):
        fd(4)
        left(36)


def pink_anticlockwise(x, y, z):
    penup()
    goto(x, y)
    pencolor("red")
    pendown()
    seth(z)
    for po in range(10):
        fd(4)
        left(18)


def star(x, y, size):
    pensize(2)
    pencolor("black")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    fillcolor("yellow")
    for i in range(5):
        left(72)
        fd(size)
        right(144)
        fd(size)
    end_fill()


if __name__ == '__main__':
    ChristmasTree()


那么,你更喜欢樱花树还是圣诞树呢,快来投票吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值