01.樱花🌸

cherry_blossoms1.py
import turtle as T
import random
import time
'''
python--turtle库
01——绘制樱花树
copy自知乎
链接:https://zhuanlan.zhihu.com/p/106388608
=== === === === === === === === === === ===
turtle库的基础命令介绍
(1)画布
画布canvas是绘图区域,可以设置它的大小和初始位置
turtle.screensize(1000,600,'red') 大小的设置
turtle.setup(width=0.5,height=0.75) 初始位置
(2)画笔
(1)画笔运动的命令
turtle.forward(a) 向当前画笔方向移动a像素长度
turtle.backward(a) 向当前画笔相反方向移动a像素长度
turtle.right(a) 顺时针移动
aturtle.left(a) 逆时针移动
aturtle.pendown() 移动时绘制图形
turtle.goto(x,y) 将画笔移动到坐标为x,y的位置
turtle.penup() 移动时不绘制图形,提起笔
turtle.speed(a) 画笔绘制的速度范围
turtle.circle() 画图,半径为正,表示圆心在画笔的左边画圈
(2)画笔控制命令
turtle.pensize(width) 绘制图形的宽度
turtle.pencolor() 画笔的颜色
turtle.fillcolor(a) 绘制图形的填充颜色
turtle.color(a1,a2) 同时设置pencolor=a1,fillcolor=a2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏箭头显示
turtle.showturtle() 显示箭头
(3)全局控制命令
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始位置
turtle.undo() 撤销上一个turtle动作
'''
# 绘制樱花
def tree(branch, t):
time.sleep(0.01)
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')
t.pensize(branch / 2)
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)
def main():
t = T.Turtle()
w = T.Screen()
t.hideturtle() # 隐藏turtle
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
tree(60, t)
petal(200, t)
w.exitonclick()
if __name__ == '__main__':
main()
02.樱花🌸

cherry_blossoms2.py
from turtle import *
from random import *
from math import *
'''
python--turtle库
02——绘制樱花树
copy自知乎
链接:https://zhuanlan.zhihu.com/p/106388608
'''
def tree(n, len):
pd() # 下笔
# 阴影效果
t = cos(radians(heading() + 45)) / 8 + 0.25
pencolor(t, t, t)
pensize(n / 3)
forward(len) # 画树枝
if n > 0:
b = random() * 15 + 10 # 右分支偏转角度
c = random() * 15 + 10 # 左分支偏转角度
d = len * (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(len) # 退回
def main():
bgcolor(0.5, 0.5, 0.5) # 背景色
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()
if __name__ == '__main__':
main()
03.樱花🌸

cherry_blossoms3.py
from turtle import *
from random import *
from math import *
'''
python--turtle库
03——绘制樱花树
copy自知乎
链接:https://zhuanlan.zhihu.com/p/106388608
'''
def tree(n, len):
pd()
t = cos(radians(heading() + 45)) / 8 + 0.25
pencolor(t, t, t)
pensize(n / 4)
forward(len)
if n > 0:
b = random() * 15 + 10
c = random() * 15 + 10
d = len * (random() * 0.35 + 0.6)
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, n)
circle(2)
left(90)
pu()
backward(len)
def main():
bgcolor(0.5, 0.5, 0.5)
ht()
speed(0)
tracer(0, 0)
left(90)
pu()
backward(300)
tree(13, 100)
done()
if __name__ == '__main__':
main()
04.狗🐕

dog.py
import turtle as t
def main():
t.screensize(500, 500)
# 【头部轮廓】
t.pensize(5)
t.home()
t.seth(0)
t.pd() #pendown
t.color('black')
t.circle(20, 80) # 0
t.circle(200, 30) # 1
t.circle(30, 60) # 2
t.circle(200, 29.5) # 3
t.color('black')
t.circle(20, 60) # 4
t.circle(-150, 22) # 5
t.circle(-50, 10) # 6
t.circle(50, 70) # 7
# 确定鼻头大概位置 t.xcor和t.ycor乌龟一开始的位置
x_nose = t.xcor()
y_nose = t.ycor()
t.circle(30, 62) # 8
t.circle(200, 15) # 9
# 【鼻子】
t.pu() #penup
t.goto(x_nose, y_nose + 25)
t.seth(90)
t.pd()
t.begin_fill()
t.circle(8)
t.end_fill()
# 【眼睛】
t.pu()
t.goto(x_nose + 48, y_nose + 55)
t.seth(90)
t.pd()
t.begin_fill()
t.circle(8)
t.end_fill()
# 【耳朵】
t.pu()
t.color('#444444')
t.goto(x_nose + 100, y_nose + 110)
t.seth(182)
t.pd()
t.circle(15, 45)
t.color('black')
t.circle(10, 15)
t.circle(90, 70)
t.circle(25, 110)
t.rt(4)
t.circle(90, 70)
t.circle(10, 15)
t.color('#444444')
t.circle(15, 45)
# 【身体】
t.pu()
t.color('black')
t.goto(x_nose + 90, y_nose - 30)
t.seth(-130)
t.pd()
t.circle(250, 28)
t.circle(10, 140)
t.circle(-250, 25)
t.circle(-200, 25)
t.circle(-50, 85)
t.circle(8, 145)
t.circle(90, 45)
t.circle(550, 5)
# 【尾巴】
t.seth(0)
t.circle(60, 85)
t.circle(40, 65)
t.circle(40, 60)
t.lt(150) #left
t.circle(-40, 90)
t.circle(-25, 100)
t.lt(5)
t.fd(20)
t.circle(10, 60)
# 【背部】
t.rt(80) #right
t.circle(200, 35)
# 【项圈】
t.pensize(20)
t.color('#F03C3F')
t.lt(10)
t.circle(-200, 25)
# 【爱心铃铛】
t.pu()
t.fd(18)
t.lt(90)
t.fd(18)
t.pensize(6)
t.seth(35) #setheading
t.color('#FDAF17')
t.begin_fill()
t.lt(135)
t.fd(6)
t.right(180) # 画笔掉头
t.circle(6, -180)
t.backward(8)
t.right(90)
t.forward(6)
t.circle(-6, 180)
t.fd(15)
t.end_fill()
# 【前小腿】
t.pensize(5)
t.pu()
t.color('black')
t.goto(x_nose + 100, y_nose - 125)
t.pd()
t.seth(-50)
t.fd(25)
t.circle(10, 150)
t.fd(25)
# 【后小腿】
t.pensize(4)
t.pu()
t.goto(x_nose + 314, y_nose - 125)
t.pd()
t.seth(-95)
t.fd(25)
t.circle(-5, 150)
t.fd(2)
t.hideturtle()
t.done()
if __name__ == '__main__':
main()
05.心♥

heart.py
from turtle import *
def main():
color('red', 'pink') # 画笔色red,背景色pink
begin_fill()
left(135) # 左转135°
fd(100) # 前进100像素
right(180) # 画笔掉头
circle(30, -180)
backward(35) # 由于此时画笔方向约为绝对方向的135°,需倒退画线
right(90)
forward(35)
circle(-30, 180)
fd(100)
end_fill()
hideturtle()
done()
if __name__ == '__main__':
main()
06.小黄人

minions.py
import turtle
#小黄人
def main():
t = turtle.Turtle()
wn = turtle.Screen()
turtle.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.end_fill()
if __name__ == '__main__':
main()
07.皮卡丘
# coding:utf-8
import turtle as t
import time
# 皮卡丘
# 基础设置
t.screensize(800, 600)
t.pensize(2)  # 设置画笔的大小
t.speed(10)  # 设置画笔速度为10
# 画左偏曲线函数
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()
08.小猪佩奇

peppa_pig.py
"""
绘制小猪佩奇
"""
from turtle import *
def nose(x,y):
    """画鼻子"""
    penup()
    # 将海龟移动到指定的坐标
    goto(x,y)
    pendown()
    # 设置海龟的方向(0-东、90-北、180-西、270-南)
    setheading(-30)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i <90:
            a = a + 0.08
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    # 设置画笔的颜色(红, 绿, 蓝)
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
def head(x, y):
    """画头"""
    color((255, 155, 192), "pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0<= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3) #向左转3度
            fd(a) #向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()
def ears(x,y):
    """画耳朵"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()
def eyes(x,y):
    """画眼睛"""
    color((255, 155, 192), "white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255, 155, 192), "white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
def cheek(x,y):
    """画脸颊"""
    color((255, 155, 192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()
def mouth(x,y):
    """画嘴巴"""
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)
def setting():
    """设置参数"""
    pensize(4)
    # 隐藏海龟
    hideturtle()
    colormode(255)
    color((255, 155, 192), "pink")
    setup(840, 500)
    speed(10)
def main():
    """主函数"""
    setting() 
    nose(-100, 100)
    head(-69, 167)
    ears(0, 160)
    eyes(0, 140)
    cheek(80, 10)
    mouth(-20, 30)
    done()
if __name__ == '__main__':
    main()
09.国旗

flag.py
"""
用Python的turtle模块绘制国旗
"""
import turtle
def draw_rectangle(x, y, width, height):
    """绘制矩形"""
    turtle.goto(x, y)
    turtle.pencolor('red')
    turtle.fillcolor('red')
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    turtle.end_fill()
def draw_star(x, y, radius):
    """绘制五角星"""
    turtle.setpos(x, y)
    pos1 = turtle.pos()
    turtle.circle(-radius, 72)
    pos2 = turtle.pos()
    turtle.circle(-radius, 72)
    pos3 = turtle.pos()
    turtle.circle(-radius, 72)
    pos4 = turtle.pos()
    turtle.circle(-radius, 72)
    pos5 = turtle.pos()
    turtle.color('yellow', 'yellow')
    turtle.begin_fill()
    turtle.goto(pos3)
    turtle.goto(pos1)
    turtle.goto(pos4)
    turtle.goto(pos2)
    turtle.goto(pos5)
    turtle.end_fill()
def main():
    """主程序"""
    turtle.speed(12)
    turtle.penup()
    x, y = -270, -180
    # 画国旗主体
    width, height = 540, 360
    draw_rectangle(x, y, width, height)
    # 画大星星
    pice = 22
    center_x, center_y = x + 5 * pice, y + height - pice * 5
    turtle.goto(center_x, center_y)
    turtle.left(90)
    turtle.forward(pice * 3)
    turtle.right(90)
    draw_star(turtle.xcor(), turtle.ycor(), pice * 3)
    x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]
    # 画小星星
    for x_pos, y_pos in zip(x_poses, y_poses):
        turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
        turtle.left(turtle.towards(center_x, center_y) - turtle.heading())
        turtle.forward(pice)
        turtle.right(90)
        draw_star(turtle.xcor(), turtle.ycor(), pice)
    # 隐藏海龟
    turtle.ht()
    # 显示绘图窗口
    turtle.mainloop()
if __name__ == '__main__':
    main()
10.冰墩墩

import turtle
if __name__ == '__main__':
    turtle.title('PythonBingDwenDwen')
    turtle.speed(100)  # 速度
    # 左手
    turtle.goto(177, 112)
    turtle.penup()
    turtle.pencolor("lightgray")
    turtle.pensize(3)
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(80)
    turtle.circle(-45, 200)
    turtle.circle(-300, 23)
    turtle.end_fill()
    # 左手内
    turtle.penup()
    turtle.goto(182, 95)
    turtle.pencolor("black")
    turtle.pensize(1)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.setheading(95)
    turtle.pendown()
    turtle.circle(-37, 160)
    turtle.circle(-20, 50)
    turtle.circle(-200, 30)
    turtle.end_fill()
    # 轮廓
    # 头顶
    turtle.penup()
    turtle.goto(-73, 230)
    turtle.pencolor("lightgray")
    turtle.pensize(3)
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(20)
    turtle.circle(-250, 35)
    # 左耳
    turtle.setheading(50)
    turtle.circle(-42, 180)
    # 左侧
    turtle.setheading(-50)
    turtle.circle(-190, 30)
    turtle.circle(-320, 45)
    # 左腿
    turtle.circle(120, 30)
    turtle.circle(200, 12)
    turtle.circle(-18, 85)
    turtle.circle(-180, 23)
    turtle.circle(-20, 110)
    turtle.circle(15, 115)
    turtle.circle(100, 12)
    # 右腿
    turtle.circle(15, 120)
    turtle.circle(-15, 110)
    turtle.circle(-150, 30)
    turtle.circle(-15, 70)
    turtle.circle(-150, 10)
    turtle.circle(200, 35)
    turtle.circle(-150, 20)
    # 右手
    turtle.setheading(-120)
    turtle.circle(50, 30)
    turtle.circle(-35, 200)
    turtle.circle(-300, 23)
    # 右侧
    turtle.setheading(86)
    turtle.circle(-300, 26)
    # 右耳
    turtle.setheading(122)
    turtle.circle(-53, 160)
    turtle.end_fill()
    # 右耳内
    turtle.penup()
    turtle.goto(-130, 180)
    turtle.pencolor("black")
    turtle.pensize(1)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(120)
    turtle.circle(-28, 160)
    turtle.setheading(210)
    turtle.circle(150, 20)
    turtle.end_fill()
    # 左耳内
    turtle.penup()
    turtle.goto(90, 230)
    turtle.setheading(40)
    turtle.begin_fill()
    turtle.pendown()
    turtle.circle(-30, 170)
    turtle.setheading(125)
    turtle.circle(150, 23)
    turtle.end_fill()
    # 右手内
    turtle.penup()
    turtle.goto(-180, -55)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.setheading(-120)
    turtle.pendown()
    turtle.circle(50, 30)
    turtle.circle(-27, 200)
    turtle.circle(-300, 20)
    turtle.setheading(-90)
    turtle.circle(300, 14)
    turtle.end_fill()
    # 左腿内
    turtle.penup()
    turtle.goto(108, -168)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(-115)
    turtle.circle(110, 15)
    turtle.circle(200, 10)
    turtle.circle(-18, 80)
    turtle.circle(-180, 13)
    turtle.circle(-20, 90)
    turtle.circle(15, 60)
    turtle.setheading(42)
    turtle.circle(-200, 29)
    turtle.end_fill()
    # 右腿内
    turtle.penup()
    turtle.goto(-38, -210)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(-155)
    turtle.circle(15, 100)
    turtle.circle(-10, 110)
    turtle.circle(-100, 30)
    turtle.circle(-15, 65)
    turtle.circle(-100, 10)
    turtle.circle(200, 15)
    turtle.setheading(-14)
    turtle.circle(-200, 27)
    turtle.end_fill()
    # 右眼
    # 眼圈
    turtle.penup()
    turtle.goto(-64, 120)
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(40)
    turtle.circle(-35, 152)
    turtle.circle(-100, 50)
    turtle.circle(-35, 130)
    turtle.circle(-100, 50)
    turtle.end_fill()
    # 眼珠
    turtle.penup()
    turtle.goto(-47, 55)
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(25, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(-45, 62)
    turtle.pencolor("darkslategray")
    turtle.fillcolor("darkslategray")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(19, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(-45, 68)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(10, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(-47, 86)
    turtle.pencolor("white")
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(5, 360)
    turtle.end_fill()
    # 左眼
    # 眼圈
    turtle.penup()
    turtle.goto(51, 82)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(120)
    turtle.circle(-32, 152)
    turtle.circle(-100, 55)
    turtle.circle(-25, 120)
    turtle.circle(-120, 45)
    turtle.end_fill()
    # 眼珠
    turtle.penup()
    turtle.goto(79, 60)
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(24, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(79, 64)
    turtle.pencolor("darkslategray")
    turtle.fillcolor("darkslategray")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(19, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(79, 70)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(10, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(79, 88)
    turtle.pencolor("white")
    turtle.fillcolor("white")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(0)
    turtle.circle(5, 360)
    turtle.end_fill()
    # 鼻子
    turtle.penup()
    turtle.goto(37, 80)
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pendown()
    turtle.circle(-8, 130)
    turtle.circle(-22, 100)
    turtle.circle(-8, 130)
    turtle.end_fill()
    # 嘴
    turtle.penup()
    turtle.goto(-15, 48)
    turtle.setheading(-36)
    turtle.begin_fill()
    turtle.pendown()
    turtle.circle(60, 70)
    turtle.setheading(-132)
    turtle.circle(-45, 100)
    turtle.end_fill()
    # 彩虹圈
    turtle.penup()
    turtle.goto(-135, 120)
    turtle.pensize(5)
    turtle.pencolor("cyan")
    turtle.pendown()
    turtle.setheading(60)
    turtle.circle(-165, 150)
    turtle.circle(-130, 78)
    turtle.circle(-250, 30)
    turtle.circle(-138, 105)
    turtle.penup()
    turtle.goto(-131, 116)
    turtle.pencolor("slateblue")
    turtle.pendown()
    turtle.setheading(60)
    turtle.circle(-160, 144)
    turtle.circle(-120, 78)
    turtle.circle(-242, 30)
    turtle.circle(-135, 105)
    turtle.penup()
    turtle.goto(-127, 112)
    turtle.pencolor("orangered")
    turtle.pendown()
    turtle.setheading(60)
    turtle.circle(-155, 136)
    turtle.circle(-116, 86)
    turtle.circle(-220, 30)
    turtle.circle(-134, 103)
    turtle.penup()
    turtle.goto(-123, 108)
    turtle.pencolor("gold")
    turtle.pendown()
    turtle.setheading(60)
    turtle.circle(-150, 136)
    turtle.circle(-104, 86)
    turtle.circle(-220, 30)
    turtle.circle(-126, 102)
    turtle.penup()
    turtle.goto(-120, 104)
    turtle.pencolor("greenyellow")
    turtle.pendown()
    turtle.setheading(60)
    turtle.circle(-145, 136)
    turtle.circle(-90, 83)
    turtle.circle(-220, 30)
    turtle.circle(-120, 100)
    turtle.penup()
    # 爱心
    turtle.penup()
    turtle.goto(220, 115)
    turtle.pencolor("brown")
    turtle.pensize(1)
    turtle.fillcolor("brown")
    turtle.begin_fill()
    turtle.pendown()
    turtle.setheading(36)
    turtle.circle(-8, 180)
    turtle.circle(-60, 24)
    turtle.setheading(110)
    turtle.circle(-60, 24)
    turtle.circle(-8, 180)
    turtle.end_fill()
    # 五环
    turtle.penup()
    turtle.goto(-5, -170)
    turtle.pendown()
    turtle.pencolor("blue")
    turtle.circle(6)
    turtle.penup()
    turtle.goto(10, -170)
    turtle.pendown()
    turtle.pencolor("black")
    turtle.circle(6)
    turtle.penup()
    turtle.goto(25, -170)
    turtle.pendown()
    turtle.pencolor("brown")
    turtle.circle(6)
    turtle.penup()
    turtle.goto(2, -175)
    turtle.pendown()
    turtle.pencolor("lightgoldenrod")
    turtle.circle(6)
    turtle.penup()
    turtle.goto(16, -175)
    turtle.pendown()
    turtle.pencolor("green")
    turtle.circle(6)
    turtle.penup()
    turtle.pencolor("black")
    turtle.goto(-16, -160)
    turtle.write("BEIJING 2022", font=('Arial', 10, 'bold italic'))
    turtle.hideturtle()
    turtle.done()

 
                         
                                

