python中的turtl模块

01.樱花🌸

image.png
cherry_blossoms1.py

  1. import turtle as T
  2. import random
  3. import time
  4. '''
  5. python--turtle库
  6. 01——绘制樱花树
  7. copy自知乎
  8. 链接:https://zhuanlan.zhihu.com/p/106388608
  9. === === === === === === === === === === ===
  10. turtle库的基础命令介绍
  11. (1)画布
  12. 画布canvas是绘图区域,可以设置它的大小和初始位置
  13. turtle.screensize(1000,600,'red') 大小的设置
  14. turtle.setup(width=0.5,height=0.75) 初始位置
  15. (2)画笔
  16. (1)画笔运动的命令
  17. turtle.forward(a) 向当前画笔方向移动a像素长度
  18. turtle.backward(a) 向当前画笔相反方向移动a像素长度
  19. turtle.right(a) 顺时针移动
  20. aturtle.left(a) 逆时针移动
  21. aturtle.pendown() 移动时绘制图形
  22. turtle.goto(x,y) 将画笔移动到坐标为x,y的位置
  23. turtle.penup() 移动时不绘制图形,提起笔
  24. turtle.speed(a) 画笔绘制的速度范围
  25. turtle.circle() 画图,半径为正,表示圆心在画笔的左边画圈
  26. (2)画笔控制命令
  27. turtle.pensize(width) 绘制图形的宽度
  28. turtle.pencolor() 画笔的颜色
  29. turtle.fillcolor(a) 绘制图形的填充颜色
  30. turtle.color(a1,a2) 同时设置pencolor=a1,fillcolor=a2
  31. turtle.filling() 返回当前是否在填充状态
  32. turtle.begin_fill() 准备开始填充图形
  33. turtle.end_fill() 填充完成
  34. turtle.hideturtle() 隐藏箭头显示
  35. turtle.showturtle() 显示箭头
  36. (3)全局控制命令
  37. turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
  38. turtle.reset() 清空窗口,重置turtle状态为起始位置
  39. turtle.undo() 撤销上一个turtle动作
  40. '''
  41. # 绘制樱花
  42. def tree(branch, t):
  43. time.sleep(0.01)
  44. if branch > 3:
  45. if 8 <= branch <= 12:
  46. if random.randint(0, 2) == 0:
  47. # 上色
  48. t.color('snow')
  49. else:
  50. # 上色
  51. t.color('lightcoral')
  52. t.pensize(branch / 3)
  53. elif branch < 8:
  54. if random.randint(0, 1) == 0:
  55. t.color('snow')
  56. else:
  57. t.color('lightcoral')
  58. t.pensize(branch / 2)
  59. else:
  60. t.color('sienna')
  61. t.pensize(branch / 2)
  62. t.forward(branch)
  63. a = 1.5 * random.random()
  64. t.right(20 * a)
  65. b = 1.5 * random.random()
  66. tree(branch - 10 * b, t)
  67. t.left(40 * a)
  68. tree(branch - 10 * b, t)
  69. t.right(20 * a)
  70. t.up()
  71. t.backward(branch)
  72. t.down()
  73. # 绘制花瓣
  74. def petal(m, t):
  75. for i in range(m):
  76. a = 200 - 400 * random.random()
  77. b = 10 - 20 * random.random()
  78. t.up()
  79. t.forward(b)
  80. t.left(90)
  81. t.forward(a)
  82. t.down()
  83. t.color('lightcoral')
  84. t.circle(1)
  85. t.up()
  86. t.backward(a)
  87. t.right(90)
  88. t.backward(b)
  89. def main():
  90. t = T.Turtle()
  91. w = T.Screen()
  92. t.hideturtle() # 隐藏turtle
  93. t.getscreen().tracer(5, 0)
  94. w.screensize(bg='wheat')
  95. t.left(90)
  96. t.up()
  97. t.backward(150)
  98. t.down()
  99. t.color('sienna')
  100. tree(60, t)
  101. petal(200, t)
  102. w.exitonclick()
  103. if __name__ == '__main__':
  104. main()

02.樱花🌸

image.png
cherry_blossoms2.py

  1. from turtle import *
  2. from random import *
  3. from math import *
  4. '''
  5. python--turtle库
  6. 02——绘制樱花树
  7. copy自知乎
  8. 链接:https://zhuanlan.zhihu.com/p/106388608
  9. '''
  10. def tree(n, len):
  11. pd() # 下笔
  12. # 阴影效果
  13. t = cos(radians(heading() + 45)) / 8 + 0.25
  14. pencolor(t, t, t)
  15. pensize(n / 3)
  16. forward(len) # 画树枝
  17. if n > 0:
  18. b = random() * 15 + 10 # 右分支偏转角度
  19. c = random() * 15 + 10 # 左分支偏转角度
  20. d = len * (random() * 0.25 + 0.7) # 下一个分支的长度
  21. # 右转一定角度,画右分支
  22. right(b)
  23. tree(n - 1, d)
  24. # 左转一定角度,画左分支
  25. left(b + c)
  26. tree(n - 1, d)
  27. # 转回来
  28. right(c)
  29. else:
  30. # 画叶子
  31. right(90)
  32. n = cos(radians(heading() - 45)) / 4 + 0.5
  33. pencolor(n, n * 0.8, n * 0.8)
  34. circle(3)
  35. left(90)
  36. # 添加0.3倍的飘落叶子
  37. if random() > 0.7:
  38. pu()
  39. # 飘落
  40. t = heading()
  41. an = -40 + random() * 40
  42. setheading(an)
  43. dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
  44. forward(dis)
  45. setheading(t)
  46. # 画叶子
  47. pd()
  48. right(90)
  49. n = cos(radians(heading() - 45)) / 4 + 0.5
  50. pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)
  51. circle(2)
  52. left(90)
  53. pu()
  54. # 返回
  55. t = heading()
  56. setheading(an)
  57. backward(dis)
  58. setheading(t)
  59. pu()
  60. backward(len) # 退回
  61. def main():
  62. bgcolor(0.5, 0.5, 0.5) # 背景色
  63. ht() # 隐藏turtle
  64. speed(0) # 速度,1-10渐进,0最快
  65. tracer(0, 0)
  66. pu() # 抬笔
  67. backward(100)
  68. left(90) # 左转90度
  69. pu() # 抬笔
  70. backward(300) # 后退300
  71. tree(12, 100) # 递归7层
  72. done()
  73. if __name__ == '__main__':
  74. main()

03.樱花🌸

image.png
cherry_blossoms3.py

  1. from turtle import *
  2. from random import *
  3. from math import *
  4. '''
  5. python--turtle库
  6. 03——绘制樱花树
  7. copy自知乎
  8. 链接:https://zhuanlan.zhihu.com/p/106388608
  9. '''
  10. def tree(n, len):
  11. pd()
  12. t = cos(radians(heading() + 45)) / 8 + 0.25
  13. pencolor(t, t, t)
  14. pensize(n / 4)
  15. forward(len)
  16. if n > 0:
  17. b = random() * 15 + 10
  18. c = random() * 15 + 10
  19. d = len * (random() * 0.35 + 0.6)
  20. right(b)
  21. tree(n - 1, d)
  22. left(b + c)
  23. tree(n - 1, d)
  24. right(c)
  25. else:
  26. right(90)
  27. n = cos(radians(heading() - 45)) / 4 + 0.5
  28. pencolor(n, n, n)
  29. circle(2)
  30. left(90)
  31. pu()
  32. backward(len)
  33. def main():
  34. bgcolor(0.5, 0.5, 0.5)
  35. ht()
  36. speed(0)
  37. tracer(0, 0)
  38. left(90)
  39. pu()
  40. backward(300)
  41. tree(13, 100)
  42. done()
  43. if __name__ == '__main__':
  44. main()

04.狗🐕

image.png
dog.py

  1. import turtle as t
  2. def main():
  3. t.screensize(500, 500)
  4. # 【头部轮廓】
  5. t.pensize(5)
  6. t.home()
  7. t.seth(0)
  8. t.pd() #pendown
  9. t.color('black')
  10. t.circle(20, 80) # 0
  11. t.circle(200, 30) # 1
  12. t.circle(30, 60) # 2
  13. t.circle(200, 29.5) # 3
  14. t.color('black')
  15. t.circle(20, 60) # 4
  16. t.circle(-150, 22) # 5
  17. t.circle(-50, 10) # 6
  18. t.circle(50, 70) # 7
  19. # 确定鼻头大概位置 t.xcor和t.ycor乌龟一开始的位置
  20. x_nose = t.xcor()
  21. y_nose = t.ycor()
  22. t.circle(30, 62) # 8
  23. t.circle(200, 15) # 9
  24. # 【鼻子】
  25. t.pu() #penup
  26. t.goto(x_nose, y_nose + 25)
  27. t.seth(90)
  28. t.pd()
  29. t.begin_fill()
  30. t.circle(8)
  31. t.end_fill()
  32. # 【眼睛】
  33. t.pu()
  34. t.goto(x_nose + 48, y_nose + 55)
  35. t.seth(90)
  36. t.pd()
  37. t.begin_fill()
  38. t.circle(8)
  39. t.end_fill()
  40. # 【耳朵】
  41. t.pu()
  42. t.color('#444444')
  43. t.goto(x_nose + 100, y_nose + 110)
  44. t.seth(182)
  45. t.pd()
  46. t.circle(15, 45)
  47. t.color('black')
  48. t.circle(10, 15)
  49. t.circle(90, 70)
  50. t.circle(25, 110)
  51. t.rt(4)
  52. t.circle(90, 70)
  53. t.circle(10, 15)
  54. t.color('#444444')
  55. t.circle(15, 45)
  56. # 【身体】
  57. t.pu()
  58. t.color('black')
  59. t.goto(x_nose + 90, y_nose - 30)
  60. t.seth(-130)
  61. t.pd()
  62. t.circle(250, 28)
  63. t.circle(10, 140)
  64. t.circle(-250, 25)
  65. t.circle(-200, 25)
  66. t.circle(-50, 85)
  67. t.circle(8, 145)
  68. t.circle(90, 45)
  69. t.circle(550, 5)
  70. # 【尾巴】
  71. t.seth(0)
  72. t.circle(60, 85)
  73. t.circle(40, 65)
  74. t.circle(40, 60)
  75. t.lt(150) #left
  76. t.circle(-40, 90)
  77. t.circle(-25, 100)
  78. t.lt(5)
  79. t.fd(20)
  80. t.circle(10, 60)
  81. # 【背部】
  82. t.rt(80) #right
  83. t.circle(200, 35)
  84. # 【项圈】
  85. t.pensize(20)
  86. t.color('#F03C3F')
  87. t.lt(10)
  88. t.circle(-200, 25)
  89. # 【爱心铃铛】
  90. t.pu()
  91. t.fd(18)
  92. t.lt(90)
  93. t.fd(18)
  94. t.pensize(6)
  95. t.seth(35) #setheading
  96. t.color('#FDAF17')
  97. t.begin_fill()
  98. t.lt(135)
  99. t.fd(6)
  100. t.right(180) # 画笔掉头
  101. t.circle(6, -180)
  102. t.backward(8)
  103. t.right(90)
  104. t.forward(6)
  105. t.circle(-6, 180)
  106. t.fd(15)
  107. t.end_fill()
  108. # 【前小腿】
  109. t.pensize(5)
  110. t.pu()
  111. t.color('black')
  112. t.goto(x_nose + 100, y_nose - 125)
  113. t.pd()
  114. t.seth(-50)
  115. t.fd(25)
  116. t.circle(10, 150)
  117. t.fd(25)
  118. # 【后小腿】
  119. t.pensize(4)
  120. t.pu()
  121. t.goto(x_nose + 314, y_nose - 125)
  122. t.pd()
  123. t.seth(-95)
  124. t.fd(25)
  125. t.circle(-5, 150)
  126. t.fd(2)
  127. t.hideturtle()
  128. t.done()
  129. if __name__ == '__main__':
  130. main()

05.心♥

image.png
heart.py

  1. from turtle import *
  2. def main():
  3. color('red', 'pink') # 画笔色red,背景色pink
  4. begin_fill()
  5. left(135) # 左转135°
  6. fd(100) # 前进100像素
  7. right(180) # 画笔掉头
  8. circle(30, -180)
  9. backward(35) # 由于此时画笔方向约为绝对方向的135°,需倒退画线
  10. right(90)
  11. forward(35)
  12. circle(-30, 180)
  13. fd(100)
  14. end_fill()
  15. hideturtle()
  16. done()
  17. if __name__ == '__main__':
  18. main()

06.小黄人

image.png
minions.py

  1. import turtle
  2. #小黄人
  3. def main():
  4. t = turtle.Turtle()
  5. wn = turtle.Screen()
  6. turtle.colormode(255)
  7. t.hideturtle()
  8. t.speed(0)
  9. t.penup()
  10. t.pensize(4)
  11. t.goto(100, 0)
  12. t.pendown()
  13. t.left(90)
  14. t.color((0, 0, 0), (255, 255, 0))
  15. # 身体绘制上色
  16. t.begin_fill()
  17. t.forward(200)
  18. t.circle(100, 180)
  19. t.forward(200)
  20. t.circle(100, 180)
  21. t.end_fill()
  22. # 右眼睛绘制上色
  23. t.pensize(12)
  24. t.penup()
  25. t.goto(-100, 200)
  26. t.pendown()
  27. t.right(100)
  28. t.circle(500, 23)
  29. t.pensize(3)
  30. t.penup()
  31. t.goto(0, 200)
  32. t.pendown()
  33. t.seth(270)
  34. t.color("black", "white")
  35. t.begin_fill()
  36. t.circle(30)
  37. t.end_fill()
  38. t.penup()
  39. t.goto(15, 200)
  40. t.pendown()
  41. t.color("black", "black")
  42. t.begin_fill()
  43. t.circle(15)
  44. t.end_fill()
  45. t.penup()
  46. t.goto(35, 205)
  47. t.color("black", "white")
  48. t.begin_fill()
  49. t.circle(5)
  50. t.end_fill()
  51. # 左眼睛绘制上色
  52. t.pensize(3)
  53. t.penup()
  54. t.goto(0, 200)
  55. t.pendown()
  56. t.seth(90)
  57. t.color("black", "white")
  58. t.begin_fill()
  59. t.circle(30)
  60. t.end_fill()
  61. t.penup()
  62. t.goto(-15, 200)
  63. t.pendown()
  64. t.color("black", "black")
  65. t.begin_fill()
  66. t.circle(15)
  67. t.end_fill()
  68. t.penup()
  69. t.goto(-35, 205)
  70. t.color("black", "white")
  71. t.begin_fill()
  72. t.circle(5)
  73. t.end_fill()
  74. # 嘴绘制上色
  75. t.penup()
  76. t.goto(-20, 100)
  77. t.pendown()
  78. t.seth(270)
  79. t.color("black", "white")
  80. t.begin_fill()
  81. t.circle(20, 180)
  82. t.left(90)
  83. t.forward(40)
  84. t.end_fill()
  85. # 裤子绘制上色
  86. t.penup()
  87. t.goto(-100, 0)
  88. t.pendown()
  89. t.seth(0)
  90. t.color("black", "blue")
  91. t.begin_fill()
  92. t.forward(20)
  93. t.left(90)
  94. t.forward(40)
  95. t.right(90)
  96. t.forward(160)
  97. t.right(90)
  98. t.forward(40)
  99. t.left(90)
  100. t.forward(20)
  101. t.seth(270)
  102. t.penup()
  103. t.goto(-100, 0)
  104. t.circle(100, 180)
  105. t.end_fill()
  106. # 左裤子腰带
  107. t.penup()
  108. t.goto(-70, 20)
  109. t.pendown()
  110. t.color("black", "blue")
  111. t.begin_fill()
  112. t.seth(45)
  113. t.forward(15)
  114. t.left(90)
  115. t.forward(60)
  116. t.seth(270)
  117. t.forward(15)
  118. t.left(40)
  119. t.forward(50)
  120. t.end_fill()
  121. t.left(180)
  122. t.goto(-70, 30)
  123. t.dot()
  124. # 右裤腰带
  125. t.penup()
  126. t.goto(70, 20)
  127. t.pendown()
  128. t.color("black", "blue")
  129. t.begin_fill()
  130. t.seth(135)
  131. t.forward(15)
  132. t.right(90)
  133. t.forward(60)
  134. t.seth(270)
  135. t.forward(15)
  136. t.right(40)
  137. t.forward(50)
  138. t.end_fill()
  139. t.left(180)
  140. t.goto(70, 30)
  141. t.dot()
  142. # 脚
  143. t.penup()
  144. t.goto(4, -100)
  145. t.pendown()
  146. t.seth(270)
  147. t.color("black", "black")
  148. t.begin_fill()
  149. t.forward(30)
  150. t.left(90)
  151. t.forward(40)
  152. t.seth(20)
  153. t.circle(10, 180)
  154. t.circle(400, 2)
  155. t.seth(90)
  156. t.forward(20)
  157. t.goto(4, -100)
  158. t.end_fill()
  159. t.penup()
  160. t.goto(-4, -100)
  161. t.pendown()
  162. t.seth(270)
  163. t.color("black", "black")
  164. t.begin_fill()
  165. t.forward(30)
  166. t.right(90)
  167. t.forward(40)
  168. t.seth(20)
  169. t.circle(10, -225)
  170. t.circle(400, -3)
  171. t.seth(90)
  172. t.forward(21)
  173. t.goto(-4, -100)
  174. t.end_fill()
  175. # 左手
  176. t.penup()
  177. t.goto(-100, 50)
  178. t.pendown()
  179. t.seth(225)
  180. t.color("black", "yellow")
  181. t.begin_fill()
  182. t.forward(40)
  183. t.left(90)
  184. t.forward(35)
  185. t.seth(90)
  186. t.forward(50)
  187. t.end_fill()
  188. # 右手
  189. t.penup()
  190. t.goto(100, 50)
  191. t.pendown()
  192. t.seth(315)
  193. t.color("black", "yellow")
  194. t.begin_fill()
  195. t.forward(40)
  196. t.right(90)
  197. t.forward(36)
  198. t.seth(90)
  199. t.forward(50)
  200. t.end_fill()
  201. #
  202. t.penup()
  203. t.goto(0, -100)
  204. t.pendown()
  205. t.forward(30)
  206. #
  207. t.penup()
  208. t.goto(0, -20)
  209. t.pendown()
  210. t.color("yellow")
  211. t.begin_fill()
  212. t.seth(45)
  213. t.forward(20)
  214. t.circle(10, 180)
  215. t.right(90)
  216. t.circle(10, 180)
  217. t.forward(20)
  218. t.end_fill()
  219. #
  220. t.penup()
  221. t.color("black")
  222. t.goto(-100, -20)
  223. t.pendown()
  224. t.circle(30, 90)
  225. t.penup()
  226. t.goto(100, -20)
  227. t.pendown()
  228. t.circle(30, -90)
  229. # 头顶
  230. t.penup()
  231. t.goto(2, 300)
  232. t.pendown()
  233. t.begin_fill()
  234. t.seth(135)
  235. t.circle(100, 40)
  236. t.end_fill()
  237. t.penup()
  238. t.goto(2, 300)
  239. t.pendown()
  240. t.begin_fill()
  241. t.seth(45)
  242. t.circle(100, 40)
  243. t.end_fill()
  244. if __name__ == '__main__':
  245. main()

07.皮卡丘

皮卡丘.mp4pikachu.py

# 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.小猪佩奇

image.png
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.国旗

image.png
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.冰墩墩

image.png

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()