使用Python绘制樱花的代码和方法详解

大家好,给大家分享一下python画樱花树代码如何读懂,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!

最近在学习Python库turtle的用法,顺便也整理了几个用turtle库画的图形,具体如下PYTHON库“IMITATION”。

一  turtle的基本操作

1. 引用 turtle库

2. 常用的基础函数

(1)绘制状态的函数

(2)颜色控制函数

(3)运动轨迹控制函数

二   turtle的使用

1.绘制樱花树

(1)效果一

代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 10:57
# @Author  : Xuan
# @File    : flower_01.py
import turtle as T
from turtle import *
import random
import time

# 画樱花的躯干(60,t)
def Tree(branch, t):
    setup(800,700)
    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(700,900,bg='#87CEFA')  # 可以设置画布的大小和颜色
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

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

(2)效果二

代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 11:22
# @Author  : Xuan
# @File    : flower_02.py
from turtle import *
from random import *
from math import *

def tree(n,l):
    setup(900,650)
    pd()#下笔
    #阴影效果
    t = cos(radians(heading()+45))/8+0.25
    pencolor(t,t,t)
    pensize(n/3)
    forward(l)#画树枝

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

# bgcolor(0.5,0.5,0.5)#背景色
screensize(700,900,bg='black')  # 可以设置画布的大小和颜色
ht()#隐藏turtle
speed(0)#速度 1-10渐进,0 最快
tracer(0,0)
pu()#抬笔
backward(100)
left(90)#左转90度
pu()#抬笔
backward(300)#后退300
tree(12,100)#递归7层
done()

2.叮当猫

效果图

 

代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 11:29
# @Author  : Xuan
# @File    : kingduang_cat.py
import turtle as t
# t.speed(5)
t.pensize(8)
t.hideturtle()
t.screensize(500, 500, bg='white')
# 猫脸
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()
t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()
t.pu()
t.home()
t.goto(0, 134)
t.pd()
t.pensize(4)
t.fillcolor("#EA0014")
t.begin_fill()
t.circle(18)
t.end_fill()
t.pu()
t.goto(7, 155)
t.pensize(2)
t.color('white', 'white')
t.pd()
t.begin_fill()
t.circle(4)
t.end_fill()
t.pu()
t.goto(-30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
t.pu()
t.goto(30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
t.pu()
t.goto(-38, 190)
t.pensize(8)
t.pd()
t.right(-30)
t.forward(15)
t.right(70)
t.forward(15)
t.pu()
t.goto(15, 185)
t.pensize(4)
t.pd()
t.color('black', 'black')
t.begin_fill()
t.circle(13)
t.end_fill()
t.pu()
t.goto(13, 190)
t.pensize(2)
t.pd()
t.color('white', 'white')
t.begin_fill()
t.circle(5)
t.end_fill()
t.pu()
t.home()
t.goto(0, 134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)
t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)
t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)
t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)
# 左边的胡子
t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)
t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)
t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)
t.pu()
t.goto(-70, 70)
t.pd()
t.color('black', 'red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80, 40)
t.circle(80, 80)
t.end_fill()
t.pu()
t.home()
t.goto(-80, 70)
t.pd()
t.forward(160)
t.pu()
t.home()
t.goto(-50, 50)
t.pd()
t.pensize(1)
t.fillcolor("#eb6e1a")
t.seth(40)
t.begin_fill()
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(40)
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(220)
t.circle(-80, 40)
t.circle(-80, 40)
t.end_fill()
# 领带
t.pu()
t.goto(-70, 12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.circle(200, 30)
t.circle(200, 10)
# 铃铛
t.pu()
t.goto(0, -46)
t.pd()
t.pensize(3)
t.color("black", '#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()
t.pu()
t.goto(-5, -40)
t.pd()
t.pensize(2)
t.color("black", '#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()
t.pensize(3)
t.right(115)
t.forward(7)
t.mainloop()

3.小黄人

效果图

代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 11:31
# @Author  : Xuan
# @File    : yellow_man.py
import turtle as t
# t = turtle.Turtle()
wn = t.Screen()
t.screensize(700,900,bg='#87CEFA')  # 可以设置画布的大小和颜色
t.colormode(255)
t.hideturtle()
t.speed(0)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()
#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()
#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()
#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(70,30)
t.dot()
#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()
t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()
#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()
#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)
#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()
#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)
t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.exitonclick()

4.小猪佩奇

效果图

 代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 11:26
# @Author  : Xuan
# @File    : little_pigki.py
# !/usr/bin/env python2
# coding=utf-8

import turtle as t

t.pensize(4)#画笔大小
t.hideturtle()#设置画笔可见
t.colormode(255)
t.screensize(700,900,bg='#87CEFA')  # 可以设置画布的大小和颜色
t.color((255, 155, 192), "pink")#画笔颜色
t.setup(840, 500)#显示框的大小
t.speed(0)#画笔速度

# 鼻子
t.pu()#拿起画笔
t.goto(-100, 100)#将画笔移动到x的负半轴100,y轴的100
t.pd()#放下画笔
t.seth(-30)#改变画笔朝向
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()

t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()

# 第一个眼睛轮框
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
#第一个眼睛
t.color("black")#眼睛颜色
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#第二个眼睛轮框
t.color((255, 155, 192), "white")#眼睛轮框的颜色
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
#第二个眼睛
t.color("black")#眼睛颜色
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()

# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)

# 身体
t.color((15,124,215), (255, 99, 71))#衣服内层颜色
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (66,163,242))#衣服外层颜色
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()

# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)

t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)

# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.exitonclick()#点击绘制界面退出

5.皮卡丘

效果

代码如下:

#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/21 11:56
# @Author  : Xuan
# @File    : pikokiu.py
# coding:utf-8
import turtle as t
import time
# 皮卡丘
# 基础设置
t.screensize(800, 600,bg='#87CEFA')#画布大小
t.pensize(2)  # 设置画笔的大小
t.speed(0)  # 设置画笔速度
# 画左偏曲线函数
def radian_left(ang, dis, step, n):
    for i in range(n):
        dis += step  # dis增大step
        t.lt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
def radian_right(ang, dis, step, n):
    for i in range(n):
        dis += step
        t.rt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
# 画耳朵
def InitEars():
    t.color("black", "yellow")
    # 左耳朵曲线
    t.pu()  # 提笔
    t.goto(-50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(110)  # 画笔角度
    t.begin_fill()  #开始绘制
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(44)  # 画笔角度
    t.forward(32)
    t.end_fill()
    # 右耳朵曲线
    t.pu()  # 提笔
    t.goto(50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(70)  # 画笔角度
    t.begin_fill()
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(136)  # 画笔角度
    t.forward(32)
    t.end_fill() #结束绘制
    # 耳朵黑
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(35)  # 画笔角度
    radian_right(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 25)
    t.setheading(132)  # 画笔角度
    t.forward(31)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(145)  # 画笔角度
    radian_left(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 25)
    t.setheading(48)  # 画笔角度
    t.forward(31)
    t.end_fill()
# 画尾巴
def InitTail():
    # 尾巴
    t.begin_fill()
    t.fillcolor("yellow")
    t.pu()  # 提笔
    t.goto(64, -140)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(10)  # 画笔角度
    t.forward(20)
    t.setheading(90)  # 画笔角度
    t.forward(20)
    t.setheading(10)  # 画笔角度
    t.forward(10)
    t.setheading(80)  # 画笔角度
    t.forward(100)
    t.setheading(35)  # 画笔角度
    t.forward(80)
    t.setheading(260)  # 画笔角度
    t.forward(100)
    t.setheading(205)  # 画笔角度
    t.forward(40)
    t.setheading(260)  # 画笔角度
    t.forward(37)
    t.setheading(205)  # 画笔角度
    t.forward(20)
    t.setheading(260)  # 画笔角度
    t.forward(25)
    t.setheading(175)  # 画笔角度
    t.forward(30)
    t.setheading(100)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画脚
def InitFoots():
    # 脚
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(225)  # 画笔角度
    radian_left(0.5, 1.2, 0, 12)
    radian_left(35, 0.6, 0, 4)
    radian_left(1, 1.2, 0, 18)
    t.setheading(160)  # 画笔角度
    t.forward(13)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(315)  # 画笔角度
    radian_right(0.5, 1.2, 0, 12)
    radian_right(35, 0.6, 0, 4)
    radian_right(1, 1.2, 0, 18)
    t.setheading(20)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画身体
def InitBody():
    # 外形轮廓
    t.begin_fill()
    t.pu()  # 提笔
    t.goto(112, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(112, 180)
    t.setheading(250)  # 画笔角度
    radian_left(1.6, 1.3, 0, 50)
    radian_left(0.8, 1.5, 0, 25)
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.6, 0.2, 27)
    radian_left(2.8, 1, 0, 45)
    radian_right(0.9, 1.4, 0, 31)
    t.setheading(355)  # 画笔角度
    radian_right(0.9, 1.4, 0, 31)
    radian_left(2.8, 1, 0, 45)
    radian_left(0.4, 7.2, -0.2, 27)
    t.setheading(10)  # 画笔角度
    radian_left(0.8, 1.5, 0, 25)
    radian_left(1.6, 1.3, 0, 50)
    t.end_fill()
def InitEyes():
    # 左眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
    # 右眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
# 画脸
def InitFace():
    # 脸蛋
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(-63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    # 嘴巴
    t.pensize(2.2)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(235)  # 画笔角度
    radian_right(5, 0.8, 0, 30)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(305)  # 画笔角度
    radian_left(5, 0.8, 0, 30)
# 画手
def InitHands():
    # 左手
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(285)  # 画笔角度
    radian_right(0.4, 1.2, 0, 26)
    radian_right(5, 0.35, 0, 26)
    radian_right(0.3, 1.2, 0, 15)
    # 右手
    t.pu()  # 提笔
    t.goto(46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.2, 0, 26)
    radian_left(5, 0.35, 0, 26)
    radian_left(0.3, 1.2, 0, 15)
def CloseEyes():
    # 左眼睛
    t.pu()  # 提笔
    t.goto(-46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(180)  # 画笔角度
    t.forward(10)
    # 右眼睛
    t.pu()  # 提笔
    t.goto(46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(0)  # 画笔角度
    t.forward(10)
# 初始化
def Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
# 眨眼睛
def Upgarde():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    CloseEyes()
def Upgarde_Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
def main():
    Init()
    t.tracer(False)
    # 眨眼睛动画
    for i in range(30):
        if i % 2 == 0:
            t.reset()
            t.hideturtle()
            Upgarde()
            t.update()
            time.sleep(0.3)
        else:
            t.reset()
            t.hideturtle()
            Upgarde_Init()
            t.update()
            time.sleep(1)
main()
# 结束画笔
t.done()

 更多关于turtle的用法,请移步至:python库之turtle库官方文档入门_sandalphon4869的博客-CSDN博客

附注:如有不当之处,恳请批评指正或留言,谢谢!

物联沃分享整理
物联沃-IOTWORD物联网 » 使用Python绘制樱花的代码和方法详解

发表评论