Python——畫一棵漂亮的櫻花樹

  • one 櫻花樹

動態生成櫻花
效果圖(這個是動態的):
櫻花樹

代碼:

import turtle as T
import random
import time

# 畫櫻花的軀幹(60,t)
def Tree(branch, t):
    time.sleep(0.0005)
    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(200, t)
w.exitonclick()

飄落效果
效果圖:
飄落

代碼:

import turtle as F
from random import *
from math import *

def tree(n,l,f):
    f.pd()#下筆
    #陰影效果
    t = cos(radians(f.heading()+45))/8+0.25
    f.pencolor(t,t,t)
    f.pensize(n/3)
    f.forward(l)#畫樹枝

    if n>0:
        b = random()*15+10 #右分支偏轉角度
        c = random()*15+10 #左分支偏轉角度
        d = l*(random()*0.25+0.7) #下一個分支的長度
        #右轉一定角度,畫右分支
        f.right(b)
        tree(n-1,d,f)
        #左轉一定角度,畫左分支
        f.left(b+c)
        tree(n-1,d,f)
        #轉回來
        f.right(c)
    else:
        #畫葉子
        f.right(90)
        n=cos(radians(f.heading()-45))/4+0.5
        f.pencolor(n,n*0.8,n*0.8)
        f.circle(3)
        f.left(90)
        #添加0.3倍的飄落葉子
        if(random()>0.7):
            f.pu()
            #飄落
            t = f.heading()
            an = -40 +random()*40
            f.setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            f.forward(dis)
            f.setheading(t)
            #畫葉子
            f.pd()
            f.right(90)
            n = cos(radians(f.heading()-45))/4+0.5
            f.pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
            f.circle(2)
            f.left(90)
            f.pu()
            #返回
            t=f.heading()
            f.setheading(an)
            f.backward(dis)
            f.setheading(t)
    f.pu()
    f.backward(l)#退回


# 繪圖區域
f = F.Turtle()
# 畫布大小
w = F.Screen()

w.bgcolor(0.5,0.5,0.5)#背景色
f.ht()#隱藏turtle
f.speed(10)#速度 1-10漸進,0 最快
w.tracer(0,0)
f.pu()#擡筆
f.backward(100)
f.left(90)#左轉90度
f.pu()#擡筆
f.backward(300)#後退300
tree(12,100,f)#遞歸7層
F.done()


  • 暗色效果

效果:

暗色
代碼:

#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import turtle as F
from random import *
from math import *

def tree(n, l, f):
    f.pd()
    t = cos(radians(f.heading() + 45)) / 8 + 0.25
    f.pencolor(t, t, t)
    f.pensize(n / 4)
    f.forward(l)
    if n > 0:
        b = random() * 15 + 10
        c = random() * 15 + 10
        d = l * (random() * 0.35 + 0.6)
        f.right(b)
        tree(n - 1, d, f)
        f.left(b + c)
        tree(n - 1, d, f)
        f.right(c)
    else:
        f.right(90)
        n = cos(radians(f.heading() - 45)) / 4 + 0.5
        f.pencolor(n, n, n)
        f.circle(2)
        f.left(90)
    f.pu()
    f.backward(l)

# 繪圖區域
f = F.Turtle()
# 畫布大小
w = F.Screen()
f.getscreen().tracer(5, 0)
f.hideturtle()  # 隱藏畫筆
w.screensize(bg='seashell')

w.bgcolor(0.5, 0.5, 0.5)
f.ht()
f.speed(0)
w.tracer(0, 0)
f.left(90)
f.pu()
f.backward(300)
tree(13, 100,f)
F.done()
  • two 玫瑰花

效果(有繪製過程)

玫瑰

代碼 :


#!/usr/bin/env python 
# -*- coding:utf-8 -*-
from turtle import *

import time
import turtle as t

t.setup(1000,800,0,0)
t.speed(0)
t.penup()
t.seth(90)
t.fd(340)
t.seth(0)
t.pendown()

t.speed(5)
t.begin_fill()
t.fillcolor('red')
t.circle(50,30)

for i in range(10):
    t.fd(1)
    t.left(10)

t.circle(40,40)

for i in range(6):
    t.fd(1)
    t.left(3)

t.circle(80,40)

for i in range(20):
    t.fd(0.5)
    t.left(5)

t.circle(80,45)

for i in range(10):
    t.fd(2)
    t.left(1)

t.circle(80,25)

for i in range(20):
    t.fd(1)
    t.left(4)

t.circle(50,50)

time.sleep(0.1)

t.circle(120,55)

t.speed(0)

t.seth(-90)
t.fd(70)

t.right(150)
t.fd(20)

t.left(140)
t.circle(140,90)

t.left(30)
t.circle(160,100)

t.left(130)
t.fd(25)

t.penup()
t.right(150)
t.circle(40,80)
t.pendown()

t.left(115)
t.fd(60)

t.penup()
t.left(180)
t.fd(60)
t.pendown()

t.end_fill()

t.right(120)
t.circle(-50,50)
t.circle(-20,90)

t.speed(1)
t.fd(75)

t.speed(0)
t.circle(90,110)

t.penup()
t.left(162)
t.fd(185)
t.left(170)
t.pendown()
t.circle(200,10)
t.circle(100,40)
t.circle(-52,115)
t.left(20)
t.circle(100,20)
t.circle(300,20)
t.speed(1)
t.fd(250)

t.penup()
t.speed(0)
t.left(180)
t.fd(250)
t.circle(-300,7)
t.right(80)
t.circle(200,5)
t.pendown()

t.left(60)
t.begin_fill()
t.fillcolor('green')
t.circle(-80,100)
t.right(90)
t.fd(10)
t.left(20)
t.circle(-63,127)
t.end_fill()

t.penup()
t.left(50)
t.fd(20)
t.left(180)

t.pendown()
t.circle(200,25)

t.penup()
t.right(150)

t.fd(180)

t.right(40)
t.pendown()
t.begin_fill()
t.fillcolor('green')
t.circle(-100,80)
t.right(150)
t.fd(10)
t.left(60)
t.circle(-80,98)
t.end_fill()

t.penup()
t.left(60)
t.fd(13)
t.left(180)

t.pendown()
t.speed(1)
t.circle(-200,23)

t.exitonclick()


  • three 聖誕樹

聖誕樹 (動態生成效果)

聖誕樹

#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import turtle as T
import random
import time

n = 100.0
# 繪圖區域

t = T.Turtle()
# 畫布大小
w = T.Screen()
t.hideturtle()  # 隱藏畫筆
t.getscreen().tracer(5, 0)
w.screensize(bg='seashell')
t.left(90)
t.forward(3*n)
t.color("orange", "yellow")
t.begin_fill()
t.left(126)
t.speed("fastest")

for i in range(5):
    t.forward(n/5)
    t.right(144)
    t.forward(n/5)
    t.left(72)
t.end_fill()
t.right(126)

t.color("dark green")
t.backward(n*4.8)
def tree(d, s):
    if d <= 0: return
    t.forward(s)
    tree(d-1, s*.8)
    t.right(120)
    tree(d-3, s*.5)
    t.right(120)
    tree(d-3, s*.5)
    t.right(120)
    t.backward(s)
tree(15, n)
t.backward(n/2)

for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    t.up()
    t.forward(b)
    t.left(90)
    t.forward(a)
    t.down()
    if random.randint(0, 1) == 0:
            t.color('tomato')
    else:
        t.color('wheat')
    t.circle(2)
    t.up()
    t.backward(a)
    t.right(90)
    t.backward(b)

time.sleep(60)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章