当前位置:   article > 正文

python: turtle绘制有趣的小图像合集_发挥想象,突破思维局限,不断创新,制作turtle的优秀作品

发挥想象,突破思维局限,不断创新,制作turtle的优秀作品

白天停电停网了一天后,又遇到一些事情心情难过,白天看到一些有趣的图像,是用python的turtle库画图。

晚上来电了后,也不想学习了,就看网上一些教程学习画图。

1. 飞机

  1. import turtle
  2. #太阳
  3. turtle.color('red')
  4. turtle.penup()
  5. turtle.goto(250,200)
  6. turtle.pendown()
  7. turtle.begin_fill()
  8. turtle.circle(50)
  9. turtle.end_fill()
  10. turtle.color('black','blue')
  11. turtle.begin_fill()
  12. #飞机
  13. turtle.penup()
  14. turtle.home()
  15. turtle.pendown()
  16. turtle.pensize(5)
  17. turtle.goto(-300,150)
  18. turtle.goto(100,50)
  19. turtle.goto(0,0)
  20. turtle.end_fill()
  21. turtle.goto(-30,-125)
  22. turtle.goto(-50,-50)
  23. turtle.begin_fill()
  24. turtle.goto(-300,150)
  25. turtle.goto(-125,-125)
  26. turtle.goto(-50,-50)
  27. turtle.goto(-30,-125)
  28. turtle.goto(-85,-85)
  29. turtle.end_fill()
  30. #线条
  31. turtle.pensize(3)
  32. turtle.penup()
  33. turtle.goto(75,25)
  34. turtle.pendown()
  35. turtle.goto(200,0)
  36. turtle.penup()
  37. turtle.goto(50,-5)
  38. turtle.pendown()
  39. turtle.goto(250,-30)
  40. turtle.penup()
  41. turtle.goto(10,-80)
  42. turtle.pendown()
  43. turtle.goto(100,-150)
  44. turtle.penup()
  45. turtle.goto(-80,-125)
  46. turtle.pendown()
  47. turtle.goto(120,-200)
  48. turtle.ht()
  49. turtle.exitonclick()

2. 玫瑰花

  1. import turtle as T
  2. import time
  3. T.setup(1000,800,0,0)
  4. T.speed(0)
  5. T.penup()
  6. T.seth(90)
  7. T.fd(340)
  8. T.seth(0)
  9. T.pendown()
  10. T.speed(5)
  11. T.begin_fill()
  12. T.fillcolor('red')
  13. T.circle(50,30)
  14. for i in range(10):
  15. T.fd(1)
  16. T.left(10)
  17. T.circle(40,40)
  18. for i in range(6):
  19. T.fd(1)
  20. T.left(3)
  21. T.circle(80,40)
  22. for i in range(20):
  23. T.fd(0.5)
  24. T.left(5)
  25. T.circle(80,45)
  26. for i in range(10):
  27. T.fd(2)
  28. T.left(1)
  29. T.circle(80,25)
  30. for i in range(20):
  31. T.fd(1)
  32. T.left(4)
  33. T.circle(50,50)
  34. time.sleep(0.1)
  35. T.circle(120,55)
  36. T.speed(0)
  37. T.seth(-90)
  38. T.fd(70)
  39. T.right(150)
  40. T.fd(20)
  41. T.left(140)
  42. T.circle(140,90)
  43. T.left(30)
  44. T.circle(160,100)
  45. T.left(130)
  46. T.fd(25)
  47. T.penup()
  48. T.right(150)
  49. T.circle(40,80)
  50. T.pendown()
  51. T.left(115)
  52. T.fd(60)
  53. T.penup()
  54. T.left(180)
  55. T.fd(60)
  56. T.pendown()
  57. T.end_fill()
  58. T.right(120)
  59. T.circle(-50,50)
  60. T.circle(-20,90)
  61. T.speed(1)
  62. T.fd(75)
  63. T.speed(0)
  64. T.circle(90,110)
  65. T.penup()
  66. T.left(162)
  67. T.fd(185)
  68. T.left(170)
  69. T.pendown()
  70. T.circle(200,10)
  71. T.circle(100,40)
  72. T.circle(-52,115)
  73. T.left(20)
  74. T.circle(100,20)
  75. T.circle(300,20)
  76. T.speed(1)
  77. T.fd(250)
  78. T.penup()
  79. T.speed(0)
  80. T.left(180)
  81. T.fd(250)
  82. T.circle(-300,7)
  83. T.right(80)
  84. T.circle(200,5)
  85. T.pendown()
  86. T.left(60)
  87. T.begin_fill()
  88. T.fillcolor('green')
  89. T.circle(-80,100)
  90. T.right(90)
  91. T.fd(10)
  92. T.left(20)
  93. T.circle(-63,127)
  94. T.end_fill()
  95. T.penup()
  96. T.left(50)
  97. T.fd(20)
  98. T.left(180)
  99. T.pendown()
  100. T.circle(200,25)
  101. T.penup()
  102. T.right(150)
  103. T.fd(180)
  104. T.right(40)
  105. T.pendown()
  106. T.begin_fill()
  107. T.fillcolor('green')
  108. T.circle(-100,80)
  109. T.right(150)
  110. T.fd(10)
  111. T.left(60)
  112. T.circle(-80,98)
  113. T.end_fill()
  114. T.penup()
  115. T.left(60)
  116. T.fd(13)
  117. T.left(180)
  118. T.pendown()
  119. T.speed(1)
  120. T.circle(-200,23)
  121. T.exitonclick()

3. 灰色的樱花树

  1. import turtle as T
  2. from random import *
  3. from math import *
  4. def tree(n, l):
  5. T.pd()
  6. t = cos(radians(T.heading() + 45)) / 8 + 0.25
  7. T.pencolor(t, t, t)
  8. T.pensize(n / 4)
  9. T.forward(l)
  10. if n > 0:
  11. b = random() * 15 + 10
  12. c = random() * 15 + 10
  13. d = l * (random() * 0.35 + 0.6)
  14. T.right(b)
  15. tree(n - 1, d)
  16. T.left(b + c)
  17. tree(n - 1, d)
  18. T.right(c)
  19. else:
  20. T.right(90)
  21. n = cos(radians(T.heading() - 45)) / 4 + 0.5
  22. T.pencolor(n, n, n)
  23. T.circle(2)
  24. T.left(90)
  25. T.pu()
  26. T.backward(l)
  27. T.bgcolor(0.5, 0.5, 0.5)
  28. T.ht()
  29. T.speed(0)
  30. T.tracer(0, 0)
  31. T.left(90)
  32. T.pu()
  33. T.backward(300)
  34. tree(13, 100)
  35. T.done()

4. 落英缤纷

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

3. 动态樱花树

  1. import random
  2. import time
  3. import turtle as T
  4. # 画樱花的躯干(60,t)
  5. def Tree(branch, t):
  6. time.sleep(0.0005)
  7. if branch > 3:
  8. if 8 <= branch <= 12:
  9. if random.randint(0, 2) == 0:
  10. t.color('snow') # 白
  11. else:
  12. t.color('lightcoral') # 淡珊瑚色
  13. t.pensize(branch / 3)
  14. elif branch < 8:
  15. if random.randint(0, 1) == 0:
  16. t.color('snow')
  17. else:
  18. t.color('lightcoral') # 淡珊瑚色
  19. t.pensize(branch / 2)
  20. else:
  21. t.color('sienna') # 赭(zhě)色
  22. t.pensize(branch / 10) # 6
  23. t.forward(branch)
  24. a = 1.5 * random.random()
  25. t.right(20 * a)
  26. b = 1.5 * random.random()
  27. Tree(branch - 10 * b, t)
  28. t.left(40 * a)
  29. Tree(branch - 10 * b, t)
  30. t.right(20 * a)
  31. t.up()
  32. t.backward(branch)
  33. t.down()
  34. # 掉落的花瓣
  35. def Petal(m, t):
  36. for i in range(m):
  37. a = 200 - 400 * random.random()
  38. b = 10 - 20 * random.random()
  39. t.up()
  40. t.forward(b)
  41. t.left(90)
  42. t.forward(a)
  43. t.down()
  44. t.color('lightcoral') # 淡珊瑚色
  45. t.circle(1)
  46. t.up()
  47. t.backward(a)
  48. t.right(90)
  49. t.backward(b)
  50. # 绘图区域
  51. t = T.Turtle()
  52. # 画布大小
  53. w = T.Screen()
  54. t.hideturtle() # 隐藏画笔
  55. t.getscreen().tracer(5, 0)
  56. w.screensize(bg='wheat') # wheat小麦
  57. t.left(90)
  58. t.up()
  59. t.backward(150)
  60. t.down()
  61. t.color('sienna')
  62. # 画樱花的躯干
  63. Tree(60, t)
  64. # 掉落的花瓣
  65. Petal(200, t)
  66. w.exitonclick()

4. 蓝胖子

  1. # * -- utf-8 -- *
  2. # Author: Tang
  3. import turtle as t
  4. t.speed(10)
  5. t.pensize(8)
  6. t.hideturtle()
  7. t.screensize(500, 500, bg='white')
  8. # 猫脸
  9. t.fillcolor('#00A1E8')
  10. t.begin_fill()
  11. t.circle(120)
  12. t.end_fill()
  13. t.pensize(3)
  14. t.fillcolor('white')
  15. t.begin_fill()
  16. t.circle(100)
  17. t.end_fill()
  18. t.pu()
  19. t.home()
  20. t.goto(0, 134)
  21. t.pd()
  22. t.pensize(4)
  23. t.fillcolor("#EA0014")
  24. t.begin_fill()
  25. t.circle(18)
  26. t.end_fill()
  27. t.pu()
  28. t.goto(7, 155)
  29. t.pensize(2)
  30. t.color('white', 'white')
  31. t.pd()
  32. t.begin_fill()
  33. t.circle(4)
  34. t.end_fill()
  35. t.pu()
  36. t.goto(-30, 160)
  37. t.pensize(4)
  38. t.pd()
  39. t.color('black', 'white')
  40. t.begin_fill()
  41. a = 0.4
  42. for i in range(120):
  43. if 0 <= i < 30 or 60 <= i < 90:
  44. a = a + 0.08
  45. t.lt(3) # 向左转3
  46. t.fd(a) # 向前走a的步长
  47. else:
  48. a = a - 0.08
  49. t.lt(3)
  50. t.fd(a)
  51. t.end_fill()
  52. t.pu()
  53. t.goto(30, 160)
  54. t.pensize(4)
  55. t.pd()
  56. t.color('black', 'white')
  57. t.begin_fill()
  58. for i in range(120):
  59. if 0 <= i < 30 or 60 <= i < 90:
  60. a = a + 0.08
  61. t.lt(3) # 向左转3
  62. t.fd(a) # 向前走a的步长
  63. else:
  64. a = a - 0.08
  65. t.lt(3)
  66. t.fd(a)
  67. t.end_fill()
  68. t.pu()
  69. t.goto(-38, 190)
  70. t.pensize(8)
  71. t.pd()
  72. t.right(-30)
  73. t.forward(15)
  74. t.right(70)
  75. t.forward(15)
  76. t.pu()
  77. t.goto(15, 185)
  78. t.pensize(4)
  79. t.pd()
  80. t.color('black', 'black')
  81. t.begin_fill()
  82. t.circle(13)
  83. t.end_fill()
  84. t.pu()
  85. t.goto(13, 190)
  86. t.pensize(2)
  87. t.pd()
  88. t.color('white', 'white')
  89. t.begin_fill()
  90. t.circle(5)
  91. t.end_fill()
  92. t.pu()
  93. t.home()
  94. t.goto(0, 134)
  95. t.pensize(4)
  96. t.pencolor('black')
  97. t.pd()
  98. t.right(90)
  99. t.forward(40)
  100. t.pu()
  101. t.home()
  102. t.goto(0, 124)
  103. t.pensize(3)
  104. t.pencolor('black')
  105. t.pd()
  106. t.left(10)
  107. t.forward(80)
  108. t.pu()
  109. t.home()
  110. t.goto(0, 114)
  111. t.pensize(3)
  112. t.pencolor('black')
  113. t.pd()
  114. t.left(6)
  115. t.forward(80)
  116. t.pu()
  117. t.home()
  118. t.goto(0, 104)
  119. t.pensize(3)
  120. t.pencolor('black')
  121. t.pd()
  122. t.left(0)
  123. t.forward(80)
  124. # 左边的胡子
  125. t.pu()
  126. t.home()
  127. t.goto(0, 124)
  128. t.pensize(3)
  129. t.pencolor('black')
  130. t.pd()
  131. t.left(170)
  132. t.forward(80)
  133. t.pu()
  134. t.home()
  135. t.goto(0, 114)
  136. t.pensize(3)
  137. t.pencolor('black')
  138. t.pd()
  139. t.left(174)
  140. t.forward(80)
  141. t.pu()
  142. t.home()
  143. t.goto(0, 104)
  144. t.pensize(3)
  145. t.pencolor('black')
  146. t.pd()
  147. t.left(180)
  148. t.forward(80)
  149. t.pu()
  150. t.goto(-70, 70)
  151. t.pd()
  152. t.color('black', 'red')
  153. t.pensize(6)
  154. t.seth(-60)
  155. t.begin_fill()
  156. t.circle(80, 40)
  157. t.circle(80, 80)
  158. t.end_fill()
  159. t.pu()
  160. t.home()
  161. t.goto(-80, 70)
  162. t.pd()
  163. t.forward(160)
  164. t.pu()
  165. t.home()
  166. t.goto(-50, 50)
  167. t.pd()
  168. t.pensize(1)
  169. t.fillcolor("#eb6e1a")
  170. t.seth(40)
  171. t.begin_fill()
  172. t.circle(-40, 40)
  173. t.circle(-40, 40)
  174. t.seth(40)
  175. t.circle(-40, 40)
  176. t.circle(-40, 40)
  177. t.seth(220)
  178. t.circle(-80, 40)
  179. t.circle(-80, 40)
  180. t.end_fill()
  181. # 领带
  182. t.pu()
  183. t.goto(-70, 12)
  184. t.pensize(14)
  185. t.pencolor('red')
  186. t.pd()
  187. t.seth(-20)
  188. t.circle(200, 30)
  189. t.circle(200, 10)
  190. # 铃铛
  191. t.pu()
  192. t.goto(0, -46)
  193. t.pd()
  194. t.pensize(3)
  195. t.color("black", '#f8d102')
  196. t.begin_fill()
  197. t.circle(25)
  198. t.end_fill()
  199. t.pu()
  200. t.goto(-5, -40)
  201. t.pd()
  202. t.pensize(2)
  203. t.color("black", '#79675d')
  204. t.begin_fill()
  205. t.circle(5)
  206. t.end_fill()
  207. t.pensize(3)
  208. t.right(115)
  209. t.forward(7)
  210. t.mainloop()

5. 小猪佩奇

  1. #!/usr/bin/env python2
  2. # coding=utf-8
  3. import turtle as t
  4. t.pensize(4)
  5. t.hideturtle()
  6. t.colormode(255)
  7. t.color((255, 155, 192), "pink")
  8. t.setup(840, 500)
  9. t.speed(10)
  10. # 鼻子
  11. t.pu()
  12. t.goto(-100, 100)
  13. t.pd()
  14. t.seth(-30)
  15. t.begin_fill()
  16. a = 0.4
  17. for i in range(120):
  18. if 0 <= i < 30 or 60 <= i < 90:
  19. a = a + 0.08
  20. t.lt(3) # 向左转3
  21. t.fd(a) # 向前走a的步长
  22. else:
  23. a = a - 0.08
  24. t.lt(3)
  25. t.fd(a)
  26. t.end_fill()
  27. t.pu()
  28. t.seth(90)
  29. t.fd(25)
  30. t.seth(0)
  31. t.fd(10)
  32. t.pd()
  33. t.pencolor(255, 155, 192)
  34. t.seth(10)
  35. t.begin_fill()
  36. t.circle(5)
  37. t.color(160, 82, 45)
  38. t.end_fill()
  39. t.pu()
  40. t.seth(0)
  41. t.fd(20)
  42. t.pd()
  43. t.pencolor(255, 155, 192)
  44. t.seth(10)
  45. t.begin_fill()
  46. t.circle(5)
  47. t.color(160, 82, 45)
  48. t.end_fill()
  49. # 头
  50. t.color((255, 155, 192), "pink")
  51. t.pu()
  52. t.seth(90)
  53. t.fd(41)
  54. t.seth(0)
  55. t.fd(0)
  56. t.pd()
  57. t.begin_fill()
  58. t.seth(180)
  59. t.circle(300, -30)
  60. t.circle(100, -60)
  61. t.circle(80, -100)
  62. t.circle(150, -20)
  63. t.circle(60, -95)
  64. t.seth(161)
  65. t.circle(-300, 15)
  66. t.pu()
  67. t.goto(-100, 100)
  68. t.pd()
  69. t.seth(-30)
  70. a = 0.4
  71. for i in range(60):
  72. if 0 <= i < 30 or 60 <= i < 90:
  73. a = a + 0.08
  74. t.lt(3) # 向左转3
  75. t.fd(a) # 向前走a的步长
  76. else:
  77. a = a - 0.08
  78. t.lt(3)
  79. t.fd(a)
  80. t.end_fill()
  81. # 耳朵
  82. t.color((255, 155, 192), "pink")
  83. t.pu()
  84. t.seth(90)
  85. t.fd(-7)
  86. t.seth(0)
  87. t.fd(70)
  88. t.pd()
  89. t.begin_fill()
  90. t.seth(100)
  91. t.circle(-50, 50)
  92. t.circle(-10, 120)
  93. t.circle(-50, 54)
  94. t.end_fill()
  95. t.pu()
  96. t.seth(90)
  97. t.fd(-12)
  98. t.seth(0)
  99. t.fd(30)
  100. t.pd()
  101. t.begin_fill()
  102. t.seth(100)
  103. t.circle(-50, 50)
  104. t.circle(-10, 120)
  105. t.circle(-50, 56)
  106. t.end_fill()
  107. # 眼睛
  108. t.color((255, 155, 192), "white")
  109. t.pu()
  110. t.seth(90)
  111. t.fd(-20)
  112. t.seth(0)
  113. t.fd(-95)
  114. t.pd()
  115. t.begin_fill()
  116. t.circle(15)
  117. t.end_fill()
  118. t.color("black")
  119. t.pu()
  120. t.seth(90)
  121. t.fd(12)
  122. t.seth(0)
  123. t.fd(-3)
  124. t.pd()
  125. t.begin_fill()
  126. t.circle(3)
  127. t.end_fill()
  128. t.color((255, 155, 192), "white")
  129. t.pu()
  130. t.seth(90)
  131. t.fd(-25)
  132. t.seth(0)
  133. t.fd(40)
  134. t.pd()
  135. t.begin_fill()
  136. t.circle(15)
  137. t.end_fill()
  138. t.color("black")
  139. t.pu()
  140. t.seth(90)
  141. t.fd(12)
  142. t.seth(0)
  143. t.fd(-3)
  144. t.pd()
  145. t.begin_fill()
  146. t.circle(3)
  147. t.end_fill()
  148. # 腮
  149. t.color((255, 155, 192))
  150. t.pu()
  151. t.seth(90)
  152. t.fd(-95)
  153. t.seth(0)
  154. t.fd(65)
  155. t.pd()
  156. t.begin_fill()
  157. t.circle(30)
  158. t.end_fill()
  159. # 嘴
  160. t.color(239, 69, 19)
  161. t.pu()
  162. t.seth(90)
  163. t.fd(15)
  164. t.seth(0)
  165. t.fd(-100)
  166. t.pd()
  167. t.seth(-80)
  168. t.circle(30, 40)
  169. t.circle(40, 80)
  170. # 身体
  171. t.color("red", (255, 99, 71))
  172. t.pu()
  173. t.seth(90)
  174. t.fd(-20)
  175. t.seth(0)
  176. t.fd(-78)
  177. t.pd()
  178. t.begin_fill()
  179. t.seth(-130)
  180. t.circle(100, 10)
  181. t.circle(300, 30)
  182. t.seth(0)
  183. t.fd(230)
  184. t.seth(90)
  185. t.circle(300, 30)
  186. t.circle(100, 3)
  187. t.color((255, 155, 192), (255, 100, 100))
  188. t.seth(-135)
  189. t.circle(-80, 63)
  190. t.circle(-150, 24)
  191. t.end_fill()
  192. # 手
  193. t.color((255, 155, 192))
  194. t.pu()
  195. t.seth(90)
  196. t.fd(-40)
  197. t.seth(0)
  198. t.fd(-27)
  199. t.pd()
  200. t.seth(-160)
  201. t.circle(300, 15)
  202. t.pu()
  203. t.seth(90)
  204. t.fd(15)
  205. t.seth(0)
  206. t.fd(0)
  207. t.pd()
  208. t.seth(-10)
  209. t.circle(-20, 90)
  210. t.pu()
  211. t.seth(90)
  212. t.fd(30)
  213. t.seth(0)
  214. t.fd(237)
  215. t.pd()
  216. t.seth(-20)
  217. t.circle(-300, 15)
  218. t.pu()
  219. t.seth(90)
  220. t.fd(20)
  221. t.seth(0)
  222. t.fd(0)
  223. t.pd()
  224. t.seth(-170)
  225. t.circle(20, 90)
  226. # 脚
  227. t.pensize(10)
  228. t.color((240, 128, 128))
  229. t.pu()
  230. t.seth(90)
  231. t.fd(-75)
  232. t.seth(0)
  233. t.fd(-180)
  234. t.pd()
  235. t.seth(-90)
  236. t.fd(40)
  237. t.seth(-180)
  238. t.color("black")
  239. t.pensize(15)
  240. t.fd(20)
  241. t.pensize(10)
  242. t.color((240, 128, 128))
  243. t.pu()
  244. t.seth(90)
  245. t.fd(40)
  246. t.seth(0)
  247. t.fd(90)
  248. t.pd()
  249. t.seth(-90)
  250. t.fd(40)
  251. t.seth(-180)
  252. t.color("black")
  253. t.pensize(15)
  254. t.fd(20)
  255. # 尾巴
  256. t.pensize(4)
  257. t.color((255, 155, 192))
  258. t.pu()
  259. t.seth(90)
  260. t.fd(70)
  261. t.seth(0)
  262. t.fd(95)
  263. t.pd()
  264. t.seth(0)
  265. t.circle(70, 20)
  266. t.circle(10, 330)
  267. t.circle(70, 30)

6. Iphone 4

  1. import turtle
  2. ##圆角矩形,参数:length(宽),high(高),cor_angle(弧度值)cor_rad(圆形轨迹半径的位置)
  3. def round_rectangle(length, high, cor_angle, cor_rad):
  4. for i in range(2):
  5. turtle.fd(high)
  6. turtle.circle(cor_rad, cor_angle)
  7. turtle.fd(length)
  8. turtle.circle(cor_rad, cor_angle)
  9. def main2():
  10. turtle.setup(1300, 800, 0, 0) # 启动窗口的大小,左上角右上角坐标
  11. pythonsize = 2
  12. turtle.pensize(pythonsize) # 运行宽度
  13. turtle.speed(10)
  14. turtle.seth(90) # 启动时运行的方向(角度)
  15. # 最外边框
  16. turtle.pencolor("#8E8e8e")
  17. turtle.penup()
  18. turtle.goto(202, -202)
  19. turtle.pendown()
  20. round_rectangle(244, 484, 90, 30)
  21. # 填充
  22. turtle.penup()
  23. turtle.goto(200, -200)
  24. turtle.pendown()
  25. turtle.begin_fill()
  26. turtle.color("#F0F0F0")
  27. round_rectangle(240, 480, 90, 30)
  28. turtle.end_fill()
  29. # 手机屏
  30. turtle.pencolor("black") # (#3000440
  31. turtle.penup()
  32. turtle.goto(185, -150)
  33. turtle.pendown()
  34. turtle.begin_fill()
  35. turtle.color("black")
  36. round_rectangle(270, 380, 90, 0)
  37. turtle.end_fill()
  38. # 听筒
  39. turtle.penup()
  40. turtle.goto(80, 265)
  41. turtle.pendown()
  42. turtle.begin_fill()
  43. turtle.color("#9d9d9d")
  44. round_rectangle(60, 4, 90, 1)
  45. turtle.end_fill()
  46. # 听筒上面的小黑
  47. turtle.penup()
  48. turtle.goto(67, 290)
  49. turtle.pendown()
  50. turtle.begin_fill()
  51. turtle.color("#3c3c3c")
  52. round_rectangle(36, 4, 90, 1)
  53. turtle.end_fill()
  54. # 摄像头
  55. turtle.penup()
  56. turtle.goto(0, 265)
  57. turtle.pendown()
  58. turtle.begin_fill()
  59. turtle.color("#3c3c3c")
  60. turtle.circle(6, 360)
  61. turtle.end_fill()
  62. # home健
  63. turtle.pencolor("#9d9d9d") # (#3000440
  64. turtle.penup()
  65. turtle.goto(75, -185)
  66. turtle.pendown()
  67. turtle.circle(25, 360)
  68. # home健图案
  69. turtle.pencolor("#9d9d9d") # (#3000440
  70. turtle.penup()
  71. turtle.goto(60, -190)
  72. turtle.pendown()
  73. turtle.draw(10, 10, 90, 5)
  74. main2()

7. 小黄人

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

8. 皮卡丘

  1. # coding:utf-8
  2. import turtle as t
  3. import time
  4. # 皮卡丘
  5. # 基础设置
  6. t.screensize(800, 600)
  7. t.pensize(2) # 设置画笔的大小
  8. t.speed(10) # 设置画笔速度为10
  9. # 画左偏曲线函数
  10. def radian_left(ang, dis, step, n):
  11. for i in range(n):
  12. dis += step # dis增大step
  13. t.lt(ang) # 向左转ang度
  14. t.fd(dis) # 向前走dis的步长
  15. def radian_right(ang, dis, step, n):
  16. for i in range(n):
  17. dis += step
  18. t.rt(ang) # 向左转ang度
  19. t.fd(dis) # 向前走dis的步长
  20. # 画耳朵
  21. def InitEars():
  22. t.color("black", "yellow")
  23. # 左耳朵曲线
  24. t.pu() # 提笔
  25. t.goto(-50, 100) # 笔头初始位置
  26. t.pd() # 下笔
  27. t.setheading(110) # 画笔角度
  28. t.begin_fill()
  29. radian_left(1.2, 0.4, 0.1, 40)
  30. t.setheading(270) # 画笔角度
  31. radian_left(1.2, 0.4, 0.1, 40)
  32. t.setheading(44) # 画笔角度
  33. t.forward(32)
  34. t.end_fill()
  35. # 右耳朵曲线
  36. t.pu() # 提笔
  37. t.goto(50, 100) # 笔头初始位置
  38. t.pd() # 下笔
  39. t.setheading(70) # 画笔角度
  40. t.begin_fill()
  41. radian_right(1.2, 0.4, 0.1, 40)
  42. t.setheading(270) # 画笔角度
  43. radian_right(1.2, 0.4, 0.1, 40)
  44. t.setheading(136) # 画笔角度
  45. t.forward(32)
  46. t.end_fill()
  47. # 耳朵黑
  48. t.begin_fill()
  49. t.fillcolor("black")
  50. t.pu() # 提笔
  51. t.goto(88, 141) # 笔头初始位置
  52. t.pd() # 下笔
  53. t.setheading(35) # 画笔角度
  54. radian_right(1.2, 1.6, 0.1, 16)
  55. t.setheading(270) # 画笔角度
  56. radian_right(1.2, 0.4, 0.1, 25)
  57. t.setheading(132) # 画笔角度
  58. t.forward(31)
  59. t.end_fill()
  60. t.begin_fill()
  61. t.fillcolor("black")
  62. t.pu() # 提笔
  63. t.goto(-88, 141) # 笔头初始位置
  64. t.pd() # 下笔
  65. t.setheading(145) # 画笔角度
  66. radian_left(1.2, 1.6, 0.1, 16)
  67. t.setheading(270) # 画笔角度
  68. radian_left(1.2, 0.4, 0.1, 25)
  69. t.setheading(48) # 画笔角度
  70. t.forward(31)
  71. t.end_fill()
  72. # 画尾巴
  73. def InitTail():
  74. # 尾巴
  75. t.begin_fill()
  76. t.fillcolor("yellow")
  77. t.pu() # 提笔
  78. t.goto(64, -140) # 笔头初始位置
  79. t.pd() # 下笔
  80. t.setheading(10) # 画笔角度
  81. t.forward(20)
  82. t.setheading(90) # 画笔角度
  83. t.forward(20)
  84. t.setheading(10) # 画笔角度
  85. t.forward(10)
  86. t.setheading(80) # 画笔角度
  87. t.forward(100)
  88. t.setheading(35) # 画笔角度
  89. t.forward(80)
  90. t.setheading(260) # 画笔角度
  91. t.forward(100)
  92. t.setheading(205) # 画笔角度
  93. t.forward(40)
  94. t.setheading(260) # 画笔角度
  95. t.forward(37)
  96. t.setheading(205) # 画笔角度
  97. t.forward(20)
  98. t.setheading(260) # 画笔角度
  99. t.forward(25)
  100. t.setheading(175) # 画笔角度
  101. t.forward(30)
  102. t.setheading(100) # 画笔角度
  103. t.forward(13)
  104. t.end_fill()
  105. # 画脚
  106. def InitFoots():
  107. # 脚
  108. t.begin_fill()
  109. t.fillcolor("yellow")
  110. t.pensize(2)
  111. t.pu() # 提笔
  112. t.goto(-70, -200) # 笔头初始位置
  113. t.pd() # 下笔
  114. t.setheading(225) # 画笔角度
  115. radian_left(0.5, 1.2, 0, 12)
  116. radian_left(35, 0.6, 0, 4)
  117. radian_left(1, 1.2, 0, 18)
  118. t.setheading(160) # 画笔角度
  119. t.forward(13)
  120. t.end_fill()
  121. t.begin_fill()
  122. t.fillcolor("yellow")
  123. t.pensize(2)
  124. t.pu() # 提笔
  125. t.goto(70, -200) # 笔头初始位置
  126. t.pd() # 下笔
  127. t.setheading(315) # 画笔角度
  128. radian_right(0.5, 1.2, 0, 12)
  129. radian_right(35, 0.6, 0, 4)
  130. radian_right(1, 1.2, 0, 18)
  131. t.setheading(20) # 画笔角度
  132. t.forward(13)
  133. t.end_fill()
  134. # 画身体
  135. def InitBody():
  136. # 外形轮廓
  137. t.begin_fill()
  138. t.pu() # 提笔
  139. t.goto(112, 0) # 笔头初始位置
  140. t.pd() # 下笔
  141. t.setheading(90) # 画笔角度
  142. t.circle(112, 180)
  143. t.setheading(250) # 画笔角度
  144. radian_left(1.6, 1.3, 0, 50)
  145. radian_left(0.8, 1.5, 0, 25)
  146. t.setheading(255) # 画笔角度
  147. radian_left(0.4, 1.6, 0.2, 27)
  148. radian_left(2.8, 1, 0, 45)
  149. radian_right(0.9, 1.4, 0, 31)
  150. t.setheading(355) # 画笔角度
  151. radian_right(0.9, 1.4, 0, 31)
  152. radian_left(2.8, 1, 0, 45)
  153. radian_left(0.4, 7.2, -0.2, 27)
  154. t.setheading(10) # 画笔角度
  155. radian_left(0.8, 1.5, 0, 25)
  156. radian_left(1.6, 1.3, 0, 50)
  157. t.end_fill()
  158. def InitEyes():
  159. # 左眼睛
  160. t.begin_fill()
  161. t.fillcolor("black")
  162. t.pu() # 提笔
  163. t.goto(-46, 10) # 笔头初始位置
  164. t.pd() # 下笔
  165. t.setheading(90) # 画笔角度
  166. t.circle(5, 360)
  167. t.end_fill()
  168. # 右眼睛
  169. t.begin_fill()
  170. t.fillcolor("black")
  171. t.pu() # 提笔
  172. t.goto(46, 10) # 笔头初始位置
  173. t.pd() # 下笔
  174. t.setheading(-90) # 画笔角度
  175. t.circle(5, 360)
  176. t.end_fill()
  177. # 画脸
  178. def InitFace():
  179. # 脸蛋
  180. t.begin_fill()
  181. t.fillcolor("red")
  182. t.pu() # 提笔
  183. t.goto(-63, -10) # 笔头初始位置
  184. t.pd() # 下笔
  185. t.setheading(90) # 画笔角度
  186. t.circle(10, 360)
  187. t.end_fill()
  188. t.begin_fill()
  189. t.fillcolor("red")
  190. t.pu() # 提笔
  191. t.goto(63, -10) # 笔头初始位置
  192. t.pd() # 下笔
  193. t.setheading(-90) # 画笔角度
  194. t.circle(10, 360)
  195. t.end_fill()
  196. # 嘴巴
  197. t.pensize(2.2)
  198. t.pu() # 提笔
  199. t.goto(0, 0) # 笔头初始位置
  200. t.pd() # 下笔
  201. t.setheading(235) # 画笔角度
  202. radian_right(5, 0.8, 0, 30)
  203. t.pu() # 提笔
  204. t.goto(0, 0) # 笔头初始位置
  205. t.pd() # 下笔
  206. t.setheading(305) # 画笔角度
  207. radian_left(5, 0.8, 0, 30)
  208. # 画手
  209. def InitHands():
  210. # 左手
  211. t.pensize(2)
  212. t.pu() # 提笔
  213. t.goto(-46, -100) # 笔头初始位置
  214. t.pd() # 下笔
  215. t.setheading(285) # 画笔角度
  216. radian_right(0.4, 1.2, 0, 26)
  217. radian_right(5, 0.35, 0, 26)
  218. radian_right(0.3, 1.2, 0, 15)
  219. # 右手
  220. t.pu() # 提笔
  221. t.goto(46, -100) # 笔头初始位置
  222. t.pd() # 下笔
  223. t.setheading(255) # 画笔角度
  224. radian_left(0.4, 1.2, 0, 26)
  225. radian_left(5, 0.35, 0, 26)
  226. radian_left(0.3, 1.2, 0, 15)
  227. def CloseEyes():
  228. # 左眼睛
  229. t.pu() # 提笔
  230. t.goto(-46, 12) # 笔头初始位置
  231. t.pd() # 下笔
  232. t.setheading(180) # 画笔角度
  233. t.forward(10)
  234. # 右眼睛
  235. t.pu() # 提笔
  236. t.goto(46, 12) # 笔头初始位置
  237. t.pd() # 下笔
  238. t.setheading(0) # 画笔角度
  239. t.forward(10)
  240. # 初始化
  241. def Init():
  242. InitEars()
  243. InitTail()
  244. InitFoots()
  245. InitBody()
  246. InitFace()
  247. InitHands()
  248. InitEyes()
  249. # 眨眼睛
  250. def Upgarde():
  251. InitEars()
  252. InitTail()
  253. InitFoots()
  254. InitBody()
  255. InitFace()
  256. InitHands()
  257. CloseEyes()
  258. def Upgarde_Init():
  259. InitEars()
  260. InitTail()
  261. InitFoots()
  262. InitBody()
  263. InitFace()
  264. InitHands()
  265. InitEyes()
  266. def main():
  267. Init()
  268. t.tracer(False)
  269. # 眨眼睛动画
  270. for i in range(30):
  271. if i % 2 == 0:
  272. t.reset()
  273. t.hideturtle()
  274. Upgarde()
  275. t.update()
  276. time.sleep(0.3)
  277. else:
  278. t.reset()
  279. t.hideturtle()
  280. Upgarde_Init()
  281. t.update()
  282. time.sleep(1)
  283. main()
  284. # 结束画笔
  285. t.done()

9. 钟表

  1. # -*- coding:utf-8*-
  2. # 用turtlr画时钟
  3. # 以自定义shape的方式实现
  4. import turtle as t
  5. import datetime as d
  6. def skip(step): # 抬笔,跳到一个地方
  7. t.penup()
  8. t.forward(step)
  9. t.pendown()
  10. def drawClock(radius): # 画表盘
  11. t.speed(0)
  12. t.mode("logo") # 以Logo坐标、角度方式
  13. t.hideturtle()
  14. t.pensize(7)
  15. t.home() # 回到圆点
  16. for j in range(60):
  17. skip(radius)
  18. if (j % 5 == 0):
  19. t.forward(20)
  20. skip(-radius - 20)
  21. else:
  22. t.dot(5)
  23. skip(-radius)
  24. t.right(6)
  25. def makePoint(pointName, len): # 钟的指针,时针、分针、秒针
  26. t.penup()
  27. t.home()
  28. t.begin_poly()
  29. t.back(0.1 * len)
  30. t.forward(len * 1.1)
  31. t.end_poly()
  32. poly = t.get_poly()
  33. t.register_shape(pointName, poly) # 注册为一个shape
  34. def drawPoint(): # 画指针
  35. global hourPoint, minPoint, secPoint, fontWriter
  36. makePoint("hourPoint", 100)
  37. makePoint("minPoint", 120)
  38. makePoint("secPoint", 140)
  39. hourPoint = t.Pen() # 每个指针是一只新turtle
  40. hourPoint.shape("hourPoint")
  41. hourPoint.shapesize(1, 1, 6)
  42. minPoint = t.Pen()
  43. minPoint.shape("minPoint")
  44. minPoint.shapesize(1, 1, 4)
  45. secPoint = t.Pen()
  46. secPoint.shape("secPoint")
  47. secPoint.pencolor('red')
  48. fontWriter = t.Pen()
  49. fontWriter.pencolor('gray')
  50. fontWriter.hideturtle()
  51. def getWeekName(weekday):
  52. weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
  53. return weekName[weekday]
  54. def getDate(year, month, day):
  55. return "%s-%s-%s" % (year, month, day)
  56. def realTime():
  57. curr = d.datetime.now()
  58. curr_year = curr.year
  59. curr_month = curr.month
  60. curr_day = curr.day
  61. curr_hour = curr.hour
  62. curr_minute = curr.minute
  63. curr_second = curr.second
  64. curr_weekday = curr.weekday()
  65. t.tracer(False)
  66. secPoint.setheading(360 / 60 * curr_second)
  67. minPoint.setheading(360 / 60 * curr_minute)
  68. hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute)
  69. fontWriter.clear()
  70. fontWriter.home()
  71. fontWriter.penup()
  72. fontWriter.forward(80)
  73. # 用turtle写文字
  74. fontWriter.write(getWeekName(curr_weekday), align="center", font=("Courier", 14, "bold"))
  75. fontWriter.forward(-160)
  76. fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold"))
  77. t.tracer(True)
  78. print(curr_second)
  79. t.ontimer(realTime, 100) # 每隔100毫秒调用一次realTime()
  80. def main():
  81. t.tracer(False)
  82. drawClock(160)
  83. drawPoint()
  84. realTime()
  85. t.tracer(True)
  86. t.mainloop()
  87. if __name__ == '__main__':
  88. main()

10. 汉诺塔

  1. import turtle
  2. class Stack:
  3. def __init__(self):
  4. self.items = []
  5. def isEmpty(self):
  6. return len(self.items) == 0
  7. def push(self, item):
  8. self.items.append(item)
  9. def pop(self):
  10. return self.items.pop()
  11. def peek(self):
  12. if not self.isEmpty():
  13. return self.items[len(self.items) - 1]
  14. def size(self):
  15. return len(self.items)
  16. def drawpole_3(): # 画出汉诺塔的poles
  17. t = turtle.Turtle()
  18. t.hideturtle()
  19. def drawpole_1(k):
  20. t.up()
  21. t.pensize(10)
  22. t.speed(100)
  23. t.goto(400 * (k - 1), 100)
  24. t.down()
  25. t.goto(400 * (k - 1), -100)
  26. t.goto(400 * (k - 1) - 20, -100)
  27. t.goto(400 * (k - 1) + 20, -100)
  28. drawpole_1(0) # 画出汉诺塔的poles[0]
  29. drawpole_1(1) # 画出汉诺塔的poles[1]
  30. drawpole_1(2) # 画出汉诺塔的poles[2]
  31. def creat_plates(n): # 制造n个盘子
  32. plates = [turtle.Turtle() for i in range(n)]
  33. for i in range(n):
  34. plates[i].up()
  35. plates[i].hideturtle()
  36. plates[i].shape("square")
  37. plates[i].shapesize(1, 8 - i)
  38. plates[i].goto(-400, -90 + 20 * i)
  39. plates[i].showturtle()
  40. return plates
  41. def pole_stack(): # 制造poles的栈
  42. poles = [Stack() for i in range(3)]
  43. return poles
  44. def moveDisk(plates, poles, fp, tp): # 把poles[fp]顶端的盘子plates[mov]从poles[fp]移到poles[tp]
  45. mov = poles[fp].peek()
  46. plates[mov].goto((fp - 1) * 400, 150)
  47. plates[mov].goto((tp - 1) * 400, 150)
  48. l = poles[tp].size() # 确定移动到底部的高度(恰好放在原来最上面的盘子上面)
  49. plates[mov].goto((tp - 1) * 400, -90 + 20 * l)
  50. def moveTower(plates, poles, height, fromPole, toPole, withPole): # 递归放盘子
  51. if height >= 1:
  52. moveTower(plates, poles, height - 1, fromPole, withPole, toPole)
  53. moveDisk(plates, poles, fromPole, toPole)
  54. poles[toPole].push(poles[fromPole].pop())
  55. moveTower(plates, poles, height - 1, withPole, toPole, fromPole)
  56. myscreen = turtle.Screen()
  57. drawpole_3()
  58. n = int(input("请输入汉诺塔的层数并回车:\n"))
  59. plates = creat_plates(n)
  60. poles = pole_stack()
  61. for i in range(n):
  62. poles[0].push(i)
  63. moveTower(plates, poles, n, 0, 2, 1)
  64. myscreen.exitonclick()

11. 高达

  1. import turtle
  2. t=turtle.Turtle()
  3. turtle.Turtle().screen.delay(0)
  4. tleft=turtle.Turtle()
  5. #第一部分
  6. t.penup()
  7. t.goto(0,0)
  8. t.pendown()
  9. t.left(20)
  10. t.forward(110)
  11. t.left(25)
  12. t.forward(40)
  13. t.left(100)
  14. t.circle(180,20)
  15. t.right(120)
  16. t.forward(250)
  17. t.left(165)
  18. t.forward(250)
  19. t.right(100)
  20. t.forward(35)
  21. t.left(70)
  22. t.forward(45)
  23. t.left(70)
  24. t.forward(120)
  25. t.left(70)
  26. t.forward(80)
  27. t.left(80)
  28. t.forward(80)
  29. t.left(68)
  30. t.forward(120)
  31. t.left(180)
  32. t.forward(78)
  33. t.right(68)
  34. t.forward(60)
  35. t.right(75)
  36. t.forward(60)
  37. t.right(110)
  38. t.forward(15)
  39. t.left(38)
  40. t.forward(65)
  41. t.right(73)#五边形的直边
  42. t.forward(35)
  43. t.right(70)
  44. t.forward(65)
  45. t.right(68)
  46. t.forward(50)
  47. t.right(80)
  48. t.forward(50)
  49. t.penup()
  50. t.goto(-65,68)
  51. t.pendown()
  52. t.right(7)
  53. t.forward(350)
  54. t.right(165)
  55. t.forward(330)
  56. t.penup()
  57. t.goto(64,65)
  58. t.pendown()
  59. t.left(75)
  60. t.forward(350)
  61. t.left(165)
  62. t.forward(330)
  63. t.penup()
  64. t.goto(300,500)
  65. #第二部分
  66. tleft.left(180)
  67. tleft.right(20)
  68. tleft.forward(110)
  69. tleft.right(25)
  70. tleft.forward(40)
  71. tleft.right(100)
  72. tleft.circle(-180,20)
  73. tleft.left(120)
  74. tleft.forward(250)
  75. tleft.right(165)
  76. tleft.forward(250)
  77. tleft.left(100)
  78. tleft.forward(35)
  79. tleft.penup()
  80. tleft.goto(0,0)
  81. tleft.pendown()
  82. tleft.left(20)
  83. tleft.penup()
  84. tleft.forward(18)
  85. tleft.pendown()
  86. tleft.forward(50)#额头竖线
  87. tleft.penup()
  88. tleft.forward(110)#消除竖线
  89. tleft.pendown()
  90. tleft.left(90)
  91. tleft.forward(30)
  92. tleft.right(90)
  93. tleft.forward(60)
  94. tleft.right(90)
  95. tleft.forward(60)
  96. tleft.right(90)
  97. tleft.forward(60)
  98. tleft.right(90)
  99. tleft.forward(40)
  100. tleft.penup()
  101. tleft.forward(30)
  102. tleft.pendown()
  103. tleft.left(90)
  104. tleft.forward(30)
  105. tleft.right(180)
  106. tleft.forward(100)
  107. tleft.right(90)
  108. tleft.forward(80)
  109. tleft.right(90)
  110. tleft.forward(100)
  111. tleft.penup()
  112. tleft.goto(150,70)
  113. tleft.pendown()
  114. tleft.left(100)
  115. tleft.forward(40)
  116. tleft.right(80)
  117. tleft.circle(-333,40)
  118. tleft.right(160)
  119. tleft.forward(230)
  120. #右半部分
  121. tleft.left(100)
  122. tleft.forward(40)
  123. tleft.left(80)
  124. tleft.forward(20)
  125. tleft.left(100)
  126. tleft.forward(30)
  127. tleft.right(100)
  128. tleft.forward(20)
  129. tleft.right(80)
  130. tleft.forward(30)
  131. tleft.left(80)
  132. tleft.forward(20)
  133. tleft.left(100)
  134. tleft.forward(30)
  135. tleft.right(100)
  136. tleft.forward(20)
  137. tleft.right(80)
  138. tleft.forward(30)
  139. tleft.left(80)
  140. tleft.forward(20)
  141. tleft.left(100)
  142. tleft.forward(30)
  143. tleft.right(100)
  144. tleft.forward(20)
  145. tleft.right(80)
  146. tleft.forward(30)
  147. tleft.left(80)
  148. tleft.forward(20)
  149. tleft.left(100)
  150. tleft.forward(30)
  151. tleft.right(100)
  152. tleft.forward(20)
  153. tleft.right(80)
  154. tleft.forward(30)
  155. tleft.left(80)
  156. tleft.forward(20)
  157. tleft.left(100)
  158. tleft.forward(30)
  159. tleft.right(100)
  160. tleft.forward(20)
  161. tleft.right(80)
  162. tleft.forward(30)
  163. tleft.left(80)
  164. tleft.forward(20)
  165. tleft.left(100)
  166. tleft.forward(30)
  167. tleft.right(100)
  168. tleft.forward(20)
  169. tleft.right(80)
  170. tleft.forward(30)
  171. #右下部分
  172. tleft.left(70)
  173. tleft.forward(30)
  174. tleft.right(110)
  175. tleft.forward(40)
  176. tleft.right(60)
  177. tleft.forward(100)
  178. tleft.right(30)
  179. tleft.circle(200,20)
  180. tleft.left(10)
  181. tleft.forward(80)
  182. #右下部分goto
  183. tleft.penup()
  184. tleft.goto(145,-198)
  185. tleft.pendown()
  186. tleft.left(90)
  187. tleft.forward(30)
  188. tleft.right(30)
  189. tleft.forward(40)
  190. tleft.right(150)
  191. tleft.forward(30)
  192. tleft.backward(30)
  193. tleft.left(90)
  194. tleft.forward(100)
  195. tleft.right(90)
  196. tleft.forward(30)
  197. tleft.backward(30)
  198. tleft.left(90)
  199. tleft.right(30)
  200. tleft.circle(200,20)
  201. tleft.left(10)
  202. tleft.forward(50)
  203. #第三部分脸
  204. t2=turtle.Turtle()
  205. t2.penup()
  206. t2.goto(0,-80)
  207. #尖角
  208. t2.circle(150,extent=90)
  209. t2.pendown()
  210. t2.circle(150,extent=30)
  211. t2.penup()
  212. t2.circle(150,extent=18)
  213. t2.pendown()
  214. t2.circle(150,extent=27)
  215. t2.penup()
  216. t2.circle(150,extent=30)
  217. t2.pendown()
  218. t2.circle(150,extent=27)
  219. t2.penup()
  220. t2.circle(150,extent=18)
  221. t2.pendown()
  222. t2.circle(150,extent=30)
  223. t2.right(100)
  224. t2.forward(40)
  225. #左脸夹
  226. t2.left(80)
  227. t2.circle(333,40)
  228. t2.left(160)
  229. t2.forward(230)
  230. #左半部分
  231. t2.right(100)
  232. t2.forward(40)
  233. t2.right(80)
  234. t2.forward(20)
  235. t2.right(100)
  236. t2.forward(30)
  237. t2.left(100)
  238. t2.forward(20)
  239. t2.left(80)
  240. t2.forward(30)
  241. t2.right(80)
  242. t2.forward(20)
  243. t2.right(100)
  244. t2.forward(30)
  245. t2.left(100)
  246. t2.forward(20)
  247. t2.left(80)
  248. t2.forward(30)
  249. t2.right(80)
  250. t2.forward(20)
  251. t2.right(100)
  252. t2.forward(30)
  253. t2.left(100)
  254. t2.forward(20)
  255. t2.left(80)
  256. t2.forward(30)
  257. t2.right(80)
  258. t2.forward(20)
  259. t2.right(100)
  260. t2.forward(30)
  261. t2.left(100)
  262. t2.forward(20)
  263. t2.left(80)
  264. t2.forward(30)
  265. t2.right(80)
  266. t2.forward(20)
  267. t2.right(100)
  268. t2.forward(30)
  269. t2.left(100)
  270. t2.forward(20)
  271. t2.left(80)
  272. t2.forward(30)
  273. t2.right(80)
  274. t2.forward(20)
  275. t2.right(100)
  276. t2.forward(30)
  277. t2.left(100)
  278. t2.forward(20)
  279. t2.left(80)
  280. t2.forward(30)
  281. t2.right(70)
  282. t2.forward(30)
  283. t2.left(110)
  284. t2.forward(40)
  285. t2.left(60)
  286. t2.forward(100)
  287. t2.left(30)
  288. t2.circle(-200,20)
  289. t2.right(10)
  290. t2.forward(80)
  291. t2.penup()
  292. t2.goto(-145,-198)#左脸颊
  293. t2.pendown()
  294. t2.right(90)
  295. t2.forward(30)
  296. t2.left(30)
  297. t2.forward(40)
  298. t2.left(150)
  299. t2.forward(30)
  300. t2.right(180)
  301. t2.forward(30)
  302. t2.left(90)
  303. t2.forward(100)
  304. t2.left(90)
  305. t2.forward(30)
  306. t2.left(180)
  307. t2.forward(30)
  308. t2.left(120)
  309. t2.circle(-200,20)
  310. t2.right(10)
  311. t2.forward(50)
  312. #左眼
  313. t2.right(135)
  314. t2.forward(70)
  315. t2.left(50)
  316. t2.forward(40)
  317. t2.left(20)
  318. t2.forward(20)
  319. t2.penup()
  320. t2.goto(-100,28)
  321. t2.pendown()
  322. t2.right(70)
  323. t2.forward(65)
  324. t2.left(50)
  325. t2.forward(40)
  326. t2.left(40)
  327. t2.forward(20)
  328. #左眼带
  329. t2.penup()
  330. t2.goto(-105,-10)
  331. t2.pendown()
  332. t2.right(100)
  333. t2.circle(120,extent=20)
  334. t2.circle(60,extent=80)
  335. t2.penup()
  336. t2.goto(-105,-13)
  337. t2.pendown()
  338. t2.right(100)
  339. t2.circle(120,extent=20)
  340. t2.circle(60,extent=80)
  341. t2.penup()
  342. t2.goto(-70,-40)
  343. t2.pendown()
  344. t2.left(10)
  345. t2.forward(30)
  346. t2.penup()
  347. t2.goto(-10,-40)
  348. t2.pendown()
  349. t2.left(35)
  350. t2.forward(30)
  351. t2.penup()
  352. t2.goto(-80,30)
  353. t2.pendown()
  354. t2.right(130)
  355. t2.forward(47)
  356. t2.left(50)
  357. t2.forward(35)
  358. t2.penup()
  359. t2.goto(-60,-45)
  360. t2.pendown()
  361. t2.right(98)
  362. t2.forward(60)
  363. t2.left(20)
  364. t2.forward(80)
  365. t2.left(70)
  366. t2.forward(10)
  367. t2.left(90)
  368. t2.forward(50)
  369. t2.right(60)
  370. t2.forward(30)
  371. t2.right(60)
  372. t2.forward(30)
  373. t2.right(60)
  374. t2.forward(50)
  375. t2.left(90)
  376. t2.forward(10)
  377. t2.left(75)
  378. t2.forward(80)
  379. t2.left(15)
  380. t2.forward(60)
  381. t2.penup()
  382. t2.goto(-80,-140)
  383. t2.pendown()
  384. t2.right(150)
  385. t2.circle(85,extent=45)
  386. t2.left(15)
  387. t2.forward(70)
  388. t2.left(15)
  389. t2.circle(55,extent=55)
  390. t2.penup()
  391. t2.goto(0,-175)
  392. t2.pendown()
  393. t2.left(18)
  394. t2.forward(170)
  395. #右眼
  396. tleft.left(135)
  397. tleft.forward(70)
  398. tleft.right(50)
  399. tleft.forward(40)
  400. tleft.right(20)
  401. tleft.forward(20)
  402. tleft.penup()
  403. tleft.goto(100,28)
  404. tleft.pendown()
  405. tleft.left(70)
  406. tleft.forward(65)
  407. tleft.right(50)
  408. tleft.forward(40)
  409. tleft.right(40)
  410. tleft.forward(20)
  411. #右眼带
  412. tleft.penup()
  413. tleft.goto(105,-10)
  414. tleft.pendown()
  415. tleft.left(100)
  416. tleft.circle(-120,extent=20)
  417. tleft.circle(-60,extent=80)
  418. tleft.penup()
  419. tleft.goto(105,-13)
  420. tleft.pendown()
  421. tleft.left(100)
  422. tleft.circle(-120,extent=20)
  423. tleft.circle(-60,extent=80)
  424. #右眼睛
  425. tleft.penup()
  426. tleft.goto(70,-40)
  427. tleft.pendown()
  428. tleft.right(10)
  429. tleft.forward(30)
  430. tleft.penup()
  431. tleft.goto(10,-40)
  432. tleft.pendown()
  433. tleft.right(35)
  434. tleft.forward(30)
  435. tleft.penup()
  436. tleft.goto(80,30)
  437. tleft.pendown()
  438. tleft.left(130)
  439. tleft.forward(47)
  440. tleft.right(50)
  441. tleft.forward(35)
  442. #鼻子
  443. tleft.penup()
  444. tleft.goto(0,-70)
  445. tleft.pendown()
  446. tleft.left(30)
  447. tleft.forward(20)
  448. tleft.left(72)
  449. tleft.forward(10)
  450. tleft.left(108)
  451. tleft.forward(20)
  452. tleft.right(42)
  453. tleft.forward(20)
  454. tleft.left(108)
  455. tleft.forward(10)
  456. tleft.left(72)
  457. tleft.forward(20)
  458. tleft.penup()
  459. tleft.goto(0,-90)
  460. tleft.pendown()
  461. tleft.left(42)
  462. tleft.forward(20)
  463. tleft.left(72)
  464. tleft.forward(10)
  465. tleft.left(108)
  466. tleft.forward(20)
  467. tleft.right(42)
  468. tleft.forward(20)
  469. tleft.left(108)
  470. tleft.forward(10)
  471. tleft.left(72)
  472. tleft.forward(20)
  473. tleft.penup()
  474. tleft.goto(200,500)
  475. turtle.done()

13. 星空

  1. from turtle import *
  2. from random import random,randint
  3. screen = Screen()
  4. width ,height = 800,600
  5. screen.setup(width,height)
  6. screen.bgcolor("black")
  7. screen.mode("logo")
  8. screen.delay(0)#这里要设为0,否则很卡
  9. t = Turtle(visible = False,shape='circle')
  10. t.pencolor("white")
  11. t.fillcolor("white")
  12. t.penup()
  13. t.setheading(-90)
  14. t.goto(width/2,randint(-height/2,height/2))
  15. stars = []
  16. for i in range(200):
  17. star = t.clone()
  18. s =random() /3
  19. star.shapesize(s,s)
  20. star.speed(int(s*10))
  21. star.setx(width/2 + randint(1,width))
  22. star.sety( randint(-height/2,height/2))
  23. star.showturtle()
  24. stars.append(star)
  25. while True:
  26. for star in stars:
  27. star.setx(star.xcor() - 3 * star.speed())
  28. if star.xcor()<-width/2:
  29. star.hideturtle()
  30. star.setx(width/2 + randint(1,width))
  31. star.sety( randint(-height/2,height/2))
  32. star.showturtle()

14. 狮子头

  1. import turtle as t
  2. def hair(): # 画头发
  3. t.penup()
  4. t.goto(-50, 150)
  5. t.pendown()
  6. t.fillcolor('#a2774d')
  7. t.begin_fill()
  8. for j in range(10): # 重复执行10
  9. t.setheading(60 - (j * 36)) # 每次调整初始角度
  10. t.circle(-50, 120) # 画120度的弧
  11. t.end_fill()
  12. def face(): # 画脸
  13. t.penup()
  14. t.goto(0, 100)
  15. t.pendown()
  16. t.fillcolor('#f2ae20')
  17. t.begin_fill()
  18. t.setheading(180)
  19. t.circle(85)
  20. t.end_fill()
  21. # 下巴
  22. t.circle(85, 120)
  23. t.fillcolor('white')
  24. t.begin_fill()
  25. t.circle(85, 120)
  26. t.setheading(135)
  27. t.circle(100, 95)
  28. t.end_fill()
  29. def ears(dir): # 画眼睛,dir用来设置方向,左右眼对称
  30. t.penup()
  31. t.goto((0 - dir) * 30, 90)
  32. t.setheading(90)
  33. t.pendown()
  34. t.fillcolor('#f2ae20')
  35. t.begin_fill()
  36. t.circle(dir * 30)
  37. t.end_fill()
  38. t.penup()
  39. t.goto((0 - dir) * 40, 85)
  40. t.setheading(90)
  41. t.pendown()
  42. t.fillcolor('white')
  43. t.begin_fill()
  44. t.circle(dir * 17)
  45. t.end_fill()
  46. def nose(): # 画鼻子
  47. t.penup()
  48. t.goto(20, 0)
  49. t.setheading(90)
  50. t.pendown()
  51. t.fillcolor('#a2774d')
  52. t.begin_fill()
  53. t.circle(20)
  54. t.end_fill()
  55. def eye(dir): # 画耳朵,dir用来设置方向,左右耳对称
  56. t.penup()
  57. t.goto((0 - dir) * 30, 20)
  58. t.setheading(0)
  59. t.pendown()
  60. t.fillcolor('black')
  61. t.begin_fill()
  62. t.circle(10)
  63. t.end_fill()
  64. def mouth(): # 画嘴巴
  65. t.penup()
  66. t.goto(0, 0)
  67. t.setheading(-90)
  68. t.pendown()
  69. t.forward(50)
  70. t.setheading(0)
  71. t.circle(80, 30)
  72. t.penup()
  73. t.goto(0, -50)
  74. t.setheading(180)
  75. t.pendown()
  76. t.circle(-80, 30)
  77. hair()
  78. ears(1)
  79. ears(-1)
  80. face()
  81. eye(1)
  82. eye(-1)
  83. mouth()
  84. nose()
  85. t.done()

16. 我太难了

  1. import turtle
  2. # 画指定的任意圆弧
  3. def arc(sa, ea, x, y, r): # start angle,end angle,circle center,radius
  4. turtle.penup()
  5. turtle.goto(x, y)
  6. turtle.setheading(0)
  7. turtle.left(sa)
  8. turtle.fd(r)
  9. turtle.pendown()
  10. turtle.left(90)
  11. turtle.circle(r, (ea - sa))
  12. return turtle.position()
  13. turtle.hideturtle()
  14. # 画脸
  15. turtle.speed(5)
  16. turtle.setup(900, 600, 200, 200)
  17. turtle.pensize(5)
  18. turtle.right(90)
  19. turtle.penup()
  20. turtle.fd(100)
  21. turtle.left(90)
  22. turtle.pendown()
  23. turtle.begin_fill()
  24. turtle.pencolor("#B26A0F") # head side color
  25. turtle.circle(150)
  26. turtle.fillcolor("#F9E549") # face color
  27. turtle.end_fill()
  28. # 画嘴
  29. turtle.penup()
  30. turtle.goto(77, 20)
  31. turtle.pencolor("#744702")
  32. turtle.goto(0, 50)
  33. turtle.right(30)
  34. turtle.fd(110)
  35. turtle.right(90)
  36. turtle.pendown()
  37. turtle.begin_fill()
  38. turtle.fillcolor("#925902") # mouth color
  39. turtle.circle(-97, 160)
  40. turtle.goto(92, -3)
  41. turtle.end_fill()
  42. turtle.penup()
  43. turtle.goto(77, -25)
  44. # 画牙齿
  45. turtle.pencolor("white")
  46. turtle.begin_fill()
  47. turtle.fillcolor("white")
  48. turtle.goto(77, -24)
  49. turtle.goto(-81, 29)
  50. turtle.goto(-70, 43)
  51. turtle.goto(77, -8)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(0, -100)
  55. turtle.setheading(0)
  56. turtle.pendown()
  57. # 画左边眼泪
  58. turtle.left(90)
  59. turtle.penup()
  60. turtle.fd(150)
  61. turtle.right(60)
  62. turtle.fd(-150)
  63. turtle.pendown()
  64. turtle.left(20)
  65. turtle.pencolor("#155F84") # tear side color
  66. turtle.fd(150)
  67. turtle.right(180)
  68. position1 = turtle.position()
  69. turtle.begin_fill()
  70. turtle.fillcolor("#7EB0C8") # tear color
  71. turtle.fd(150)
  72. turtle.right(20)
  73. turtle.left(270)
  74. turtle.circle(-150, 18)
  75. turtle.right(52)
  76. turtle.fd(110)
  77. position2 = turtle.position()
  78. turtle.goto(-33, 90)
  79. turtle.end_fill()
  80. # 画右边眼泪
  81. turtle.penup()
  82. turtle.goto(0, 0)
  83. turtle.setheading(0)
  84. turtle.left(90)
  85. turtle.fd(50)
  86. turtle.right(150)
  87. turtle.fd(150)
  88. turtle.left(150)
  89. turtle.fd(100)
  90. turtle.pendown()
  91. turtle.begin_fill()
  92. turtle.fd(-100)
  93. turtle.fillcolor("#7EB0C8") # tear color
  94. turtle.right(60)
  95. turtle.circle(150, 15)
  96. turtle.left(45)
  97. turtle.fd(66)
  98. turtle.goto(77, 20)
  99. turtle.end_fill()
  100. # 画眼睛
  101. turtle.penup()
  102. turtle.pencolor("#6C4E00") # eye color
  103. turtle.goto(-65, 75)
  104. turtle.setheading(0)
  105. turtle.left(27)
  106. turtle.fd(38)
  107. turtle.pendown()
  108. turtle.begin_fill()
  109. turtle.fillcolor("#6C4E00") # eye color
  110. turtle.left(90)
  111. turtle.circle(38, 86)
  112. turtle.goto(position2[0], position2[1])
  113. turtle.goto(position1[0], position1[1])
  114. turtle.end_fill()
  115. # 画手
  116. turtle.pencolor("#D57E18") # hand side color
  117. turtle.begin_fill()
  118. turtle.fillcolor("#EFBD3D") # hand color
  119. # 第一个手指
  120. arc(-110, 10, 110, -40, 30)
  121. turtle.circle(300, 35)
  122. turtle.circle(13, 120)
  123. turtle.setheading(-50)
  124. turtle.fd(20)
  125. turtle.setheading(130)
  126. # 第二个手指
  127. turtle.circle(200, 15)
  128. turtle.circle(12, 180)
  129. turtle.fd(40)
  130. turtle.setheading(137)
  131. # 第三个手指
  132. turtle.circle(200, 16)
  133. turtle.circle(12, 160)
  134. turtle.setheading(-35)
  135. turtle.fd(45)
  136. turtle.setheading(140)
  137. # 第四个手指
  138. turtle.circle(200, 13)
  139. turtle.circle(11, 160)
  140. turtle.setheading(-35)
  141. turtle.fd(40)
  142. turtle.setheading(145)
  143. # 第五个手指
  144. turtle.circle(200, 9)
  145. turtle.circle(10, 180)
  146. turtle.setheading(-31)
  147. turtle.fd(50)
  148. # 画最后手腕的部分
  149. turtle.setheading(-45)
  150. turtle.pensize(7)
  151. turtle.right(5)
  152. turtle.circle(180, 35)
  153. turtle.end_fill()
  154. turtle.begin_fill()
  155. turtle.setheading(-77)
  156. turtle.pensize(5)
  157. turtle.fd(50)
  158. turtle.left(-270)
  159. turtle.fd(7)
  160. turtle.pencolor("#EFBD3D")
  161. turtle.circle(30, 180)
  162. turtle.end_fill()
  163. # 测试
  164. # res=arc(70,220,90,50,300)
  165. # print(res[0],res[1])
  166. turtle.done()

17. 如果让你重新来过,你会不会爱我

 

  1. #SquareSpiral1.py
  2. import turtle as t
  3. # t = turtle.Pen()
  4. t.bgcolor("black")
  5. sides=eval(input("输入要绘制的边的数目,请输入2-6的数字!"))
  6. colors=["red","yellow","green","blue","orange","purple"]
  7. for x in range(150):
  8. t.pencolor(colors[x%sides])
  9. t.forward(x*3/sides+x)
  10. t.left(360/sides+1)
  11. t.width(x*sides/200)
  12. t.exitonclick()
  13. print("####结束####")

18. 数码管时间

  1. import turtle as t
  2. import datetime
  3. def drawGap(): #绘制数码管间隔
  4. t.penup()
  5. t.fd(5)
  6. def drawLine(draw): #绘制单段数码管
  7. drawGap()
  8. t.pendown() if draw else t.penup()
  9. t.fd(40)
  10. drawGap()
  11. t.right(90)
  12. def drawDigit(d): #根据数字绘制七段数码管
  13. drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
  14. drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
  15. drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
  16. drawLine(True) if d in [0,2,6,8] else drawLine(False)
  17. t.left(90)
  18. drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
  19. drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
  20. drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
  21. t.left(180)
  22. t.penup()
  23. t.fd(20)
  24. def drawDate(date):
  25. t.pencolor("red")
  26. for i in date:
  27. if i == '-':
  28. t.write('年', font=("Arial", 18, "normal"))
  29. t.pencolor("green")
  30. t.fd(40)
  31. elif i == '=':
  32. t.write('月', font=("Arial", 18, "normal"))
  33. t.pencolor("blue")
  34. t.fd(40)
  35. elif i == '+':
  36. t.write('日', font=("Arial", 18, "normal"))
  37. else:
  38. drawDigit(eval(i))
  39. def main():
  40. t.setup(800, 350, 200, 200)
  41. t.penup()
  42. t.fd(-350)
  43. t.pensize(5)
  44. drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
  45. t.hideturtle()
  46. t.exitonclick()
  47. main()

20. 永远滴神

  1. from turtle import *
  2. import turtle as t
  3. t.screensize(500, 500)
  4. # 【头部轮廓】
  5. t.pensize(5)
  6. t.home()
  7. t.seth(0)
  8. t.pd()
  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. # 确定鼻头大概位置
  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()
  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) # 1
  47. t.color('black')
  48. t.circle(10, 15) # 2
  49. t.circle(90, 70) # 3
  50. t.circle(25, 110) # 4
  51. t.rt(4)
  52. t.circle(90, 70) # 5
  53. t.circle(10, 15) # 6
  54. t.color('#444444')
  55. t.circle(15, 45) # 7
  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) # 1
  63. t.circle(10, 140) # 2
  64. t.circle(-250, 25) # 3
  65. t.circle(-200, 25) # 4
  66. t.circle(-50, 85) # 5
  67. t.circle(8, 145) # 6
  68. t.circle(90, 45) # 7
  69. t.circle(550, 5) # 8
  70. # 【尾巴】
  71. t.seth(0)
  72. t.circle(60, 85) # 1
  73. t.circle(40, 65) # 2
  74. t.circle(40, 60) # 3
  75. t.lt(150)
  76. t.circle(-40, 90) # 4
  77. t.circle(-25, 100) # 5
  78. t.lt(5)
  79. t.fd(20)
  80. t.circle(10, 60) # 6
  81. # 【背部】
  82. t.rt(80)
  83. t.circle(200, 35)
  84. # 【项圈】
  85. t.pensize(20)
  86. t.color('#F03C3F')
  87. t.lt(10)
  88. t.circle(-200, 25) # 5
  89. # 【爱心铃铛】
  90. t.pu()
  91. t.fd(18)
  92. t.lt(90)
  93. t.fd(18)
  94. t.pensize(6)
  95. t.seth(35)
  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()

21. I了I了

  1. import turtle as t
  2. from time import sleep
  3. def go_to(x, y):
  4. t.up()
  5. t.goto(x, y)
  6. t.down()
  7. def head(x, y, r):
  8. go_to(x, y)
  9. t.speed(20)
  10. t.circle(r)
  11. leg(x, y)
  12. def leg(x, y):
  13. t.right(90)
  14. t.forward(180)
  15. t.right(30)
  16. t.forward(100)
  17. t.left(120)
  18. go_to(x, y - 180)
  19. t.forward(100)
  20. t.right(120)
  21. t.forward(100)
  22. t.left(120)
  23. hand(x, y)
  24. def hand(x, y):
  25. go_to(x, y - 60)
  26. t.forward(100)
  27. t.left(60)
  28. t.forward(100)
  29. go_to(x, y - 90)
  30. t.right(60)
  31. t.forward(100)
  32. t.right(60)
  33. t.forward(100)
  34. t.left(60)
  35. eye(x, y)
  36. def eye(x, y):
  37. go_to(x - 50, y + 130)
  38. t.right(90)
  39. t.forward(50)
  40. go_to(x + 40, y + 130)
  41. t.forward(50)
  42. t.left(90)
  43. def big_Circle(size):
  44. t.speed(20)
  45. for i in range(150):
  46. t.forward(size)
  47. t.right(0.3)
  48. def line(size):
  49. t.speed(20)
  50. t.forward(51 * size)
  51. def small_Circle(size):
  52. t.speed(20)
  53. for i in range(210):
  54. t.forward(size)
  55. t.right(0.786)
  56. def heart(x, y, size):
  57. go_to(x, y)
  58. t.left(150)
  59. t.begin_fill()
  60. line(size)
  61. big_Circle(size)
  62. small_Circle(size)
  63. t.left(120)
  64. small_Circle(size)
  65. big_Circle(size)
  66. line(size)
  67. t.end_fill()
  68. def main():
  69. t.pensize(2)
  70. t.color('red', 'pink')
  71. head(-120, 100, 100)
  72. heart(250, -80, 1)
  73. go_to(100, -300)
  74. t.write("To: 智慧与美貌并存的", move=True, align="left", font=("楷体", 20, "normal"))
  75. t.done()
  76. main()

22. 胖达

  1. import turtle as t
  2. t.title("熊猫宝宝")
  3. t.shape("classic")
  4. t.pensize(3)
  5. t.color("black")
  6. t.fillcolor("black")
  7. t.speed(100)
  8. t.hideturtle()
  9. #左耳
  10. t.penup()
  11. t.goto(-105,97)
  12. t.setheading(160)
  13. t.begin_fill()
  14. t.pendown()
  15. t.circle(-30,230)
  16. t.setheading(180)
  17. t.circle(37,90)
  18. t.end_fill()
  19. #右耳
  20. t.penup()
  21. t.goto(105,97)
  22. t.setheading(20)
  23. t.begin_fill()
  24. t.pendown()
  25. t.circle(30,230)
  26. t.setheading(0)
  27. t.circle(-37,90)
  28. t.end_fill()
  29. #头部轮廓
  30. t.penup()
  31. t.goto(-67,140)
  32. t.setheading(30)
  33. t.pendown()
  34. t.circle(-134,60)
  35. t.penup()
  36. t.goto(-50,-25)
  37. t.setheading(180)
  38. t.pendown()
  39. t.circle(-100,30)
  40. t.circle(-30,90)
  41. t.setheading(100)
  42. t.circle(-200,20)
  43. t.penup()
  44. t.goto(50,-25)
  45. t.setheading(0)
  46. t.pendown()
  47. t.circle(100,30)
  48. t.circle(30,90)
  49. t.setheading(80)
  50. t.circle(200,20)
  51. #两熊猫眼
  52. #左眼
  53. t.penup()
  54. t.goto(-90,25)
  55. t.setheading(-45)
  56. t.begin_fill()
  57. t.pendown()
  58. #椭圆绘制技巧
  59. a = 0.2
  60. for i in range(120):
  61. if 0<=i<30 or 60<=i<90:
  62. a=a+0.1
  63. t.lt(3) #向左转3
  64. t.fd(a) #向前走a的步长
  65. else:
  66. a=a-0.1
  67. t.lt(3)
  68. t.fd(a)
  69. t.end_fill()
  70. t.fillcolor("white")
  71. t.penup()
  72. t.goto(-53,43)
  73. t.setheading(0)
  74. t.begin_fill()
  75. t.pendown()
  76. t.circle(13,360)
  77. t.end_fill()
  78. t.penup()
  79. t.pensize(4)
  80. t.goto(-60,57)
  81. t.setheading(30)
  82. t.pendown()
  83. t.circle(-12,60)
  84. #右眼
  85. t.penup()
  86. t.goto(90,25)
  87. t.setheading(45)
  88. t.pensize(2)
  89. t.fillcolor("black")
  90. t.begin_fill()
  91. t.pendown()
  92. #椭圆绘制技巧
  93. a = 0.2
  94. for i in range(120):
  95. if 0<=i<30 or 60<=i<90:
  96. a=a+0.1
  97. t.lt(3) #向左转3
  98. t.fd(a) #向前走a的步长
  99. else:
  100. a=a-0.1
  101. t.lt(3)
  102. t.fd(a)
  103. t.end_fill()
  104. t.fillcolor("white")
  105. t.penup()
  106. t.goto(53,43)
  107. t.setheading(0)
  108. t.begin_fill()
  109. t.pendown()
  110. t.circle(13,360)
  111. t.end_fill()
  112. t.penup()
  113. t.pensize(4)
  114. t.goto(60,57)
  115. t.setheading(150)
  116. t.pendown()
  117. t.circle(12,60)
  118. #鼻子和嘴吧
  119. t.penup()
  120. t.goto(-16,20)
  121. t.setheading(-90)
  122. t.fillcolor("black")
  123. t.begin_fill()
  124. t.pendown()
  125. a = 0.2
  126. for i in range(120):
  127. if 0 <= i < 30 or 60 <= i < 90:
  128. a = a + 0.03
  129. t.lt(3)
  130. t.fd(a)
  131. else:
  132. a = a - 0.03
  133. t.lt(3)
  134. t.fd(a)
  135. t.end_fill()
  136. t.penup()
  137. t.goto(-24,0)
  138. t.setheading(-60)
  139. t.pendown()
  140. t.circle(28,120)
  141. #熊肢体
  142. #左肢
  143. t.penup()
  144. t.goto(-65,-24)
  145. t.setheading(-140)
  146. t.begin_fill()
  147. t.pendown()
  148. t.circle(100,40)
  149. t.setheading(180)
  150. t.circle(30,40)
  151. t.setheading(-40)
  152. t.circle(40,40)
  153. t.setheading(-150)
  154. a = 0.5
  155. for i in range(120):
  156. if 0<=i<30 or 60<=i<90:
  157. a=a+0.05
  158. t.lt(3) #向左转3
  159. t.fd(a) #向前走a的步长
  160. elif 30 <= i < 60 or 90<=i <100:
  161. a=a-0.05
  162. t.lt(3)
  163. t.fd(a)
  164. t.setheading(93)
  165. t.circle(-150,30)
  166. t.end_fill()
  167. t.penup()
  168. t.goto(-85,-115)
  169. t.setheading(-150)
  170. t.color("gray","gray")
  171. t.begin_fill()
  172. t.pendown()
  173. a = 0.3
  174. for i in range(120):
  175. if 0<=i<30 or 60<=i<90:
  176. a=a+0.03
  177. t.lt(3) #向左转3
  178. t.fd(a) #向前走a的步长
  179. else:
  180. a=a-0.03
  181. t.lt(3)
  182. t.fd(a)
  183. t.end_fill()
  184. #每个脚趾绘制函数
  185. def toe(x,y):
  186. t.begin_fill()
  187. t.goto(x,y)
  188. t.circle(3,360)
  189. t.end_fill()
  190. t.penup()
  191. toe(-98,-120)
  192. toe(-96,-110)
  193. toe(-88,-105)
  194. toe(-80,-105)
  195. #右肢
  196. t.color("black")
  197. t.penup()
  198. t.goto(65,-24)
  199. t.setheading(-40)
  200. t.begin_fill()
  201. t.pendown()
  202. t.circle(-100,40)
  203. t.setheading(0)
  204. t.circle(-30,40)
  205. t.setheading(-140)
  206. t.circle(-40,40)
  207. t.setheading(-30)
  208. a = 0.5
  209. for i in range(120):
  210. if 0<=i<30 or 60<=i<90:
  211. a=a+0.05
  212. t.rt(3) #向左转3
  213. t.fd(a) #向前走a的步长
  214. elif 30 <= i < 60 or 90<=i <100:
  215. a=a-0.05
  216. t.rt(3)
  217. t.fd(a)
  218. t.setheading(87)
  219. t.circle(150,30)
  220. t.end_fill()
  221. t.penup()
  222. t.goto(85,-115)
  223. t.setheading(150)
  224. t.color("gray","gray")
  225. t.begin_fill()
  226. t.pendown()
  227. a = 0.3
  228. for i in range(120):
  229. if 0<=i<30 or 60<=i<90:
  230. a=a+0.03
  231. t.lt(3) #向左转3
  232. t.fd(a) #向前走a的步长
  233. else:
  234. a=a-0.03
  235. t.lt(3)
  236. t.fd(a)
  237. t.end_fill()
  238. t.penup()
  239. toe(98,-120)
  240. toe(96,-110)
  241. toe(88,-105)
  242. toe(80,-105)
  243. t.goto(-57,-140)
  244. t.color("black")
  245. t.setheading(-20)
  246. t.pendown()
  247. t.circle(165,40)
  248. t.penup()
  249. t.goto(0,180)
  250. t.write("Zuo baby is the most lovely girl", align = "center"
  251. , font = ("Times", 18, "bold"))
  252. t.done()

23.  给女票的小爱心

  1. import turtle
  2. import time
  3. Name=['Z','J','H']
  4. def draw_circle():
  5. for i in range (200):
  6. turtle.right(1)
  7. turtle.forward(1)
  8. def draw_love():
  9. turtle.color('red','pink')
  10. turtle.pensize(2)
  11. turtle.speed(10)
  12. turtle.goto(0,0)
  13. turtle.begin_fill()
  14. turtle.left(140)
  15. turtle.forward(112)
  16. draw_circle()
  17. turtle.left(120)
  18. draw_circle()
  19. turtle.forward(112)
  20. turtle.end_fill()
  21. def draw_name():
  22. turtle.pensize(50)
  23. turtle.up()
  24. turtle.goto(-50,142.7)
  25. turtle.goto(100,-10)
  26. turtle.write("I Love you")
  27. turtle.done()
  28. draw_love()
  29. draw_name()
  30. turtle.exitonclick()

25. 丘比特之箭

  1. from turtle import*
  2. import turtle
  3. turtle.setup(750,500)
  4. turtle.penup()
  5. turtle.pensize(25)
  6. turtle.pencolor("pink")
  7. turtle.fd(-230)
  8. turtle.seth(90)
  9. turtle.pendown()
  10. turtle.circle(-50,180)
  11. turtle.circle(50,-180)
  12. turtle.circle(75,-50)
  13. turtle.circle(-190,-45)
  14. turtle.penup()
  15. turtle.fd(185)
  16. turtle.seth(180)
  17. turtle.fd(120)
  18. turtle.seth(90)
  19. turtle.pendown()
  20. turtle.circle(-75,-50)
  21. turtle.circle(190,-45)
  22. turtle.penup()
  23. turtle.fd(184)
  24. turtle.seth(0)
  25. turtle.fd(80)
  26. turtle.seth(90)
  27. turtle.pendown()
  28. turtle.circle(-50,180)
  29. turtle.circle(50,-180)
  30. turtle.circle(75,-50)
  31. turtle.circle(-190,-45)
  32. turtle.penup()
  33. turtle.fd(185)
  34. turtle.seth(180)
  35. turtle.fd(120)
  36. turtle.seth(90)
  37. turtle.pendown()
  38. turtle.circle(-75,-50)
  39. turtle.circle(190,-45)
  40. turtle.penup()
  41. turtle.fd(150)
  42. turtle.seth(180)
  43. turtle.fd(300)
  44. turtle.pencolor("red")
  45. turtle.pensize(10)
  46. turtle.pendown()
  47. turtle.fd(-500)
  48. turtle.seth(90)
  49. turtle.fd(30)
  50. turtle.fd(-60)
  51. turtle.seth(30)
  52. turtle.fd(60)
  53. turtle.seth(150)
  54. turtle.fd(60)
  55. turtle.done()

27.  雪花

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jan 12 14:35:14 2018
  4. @author: Administrator
  5. """
  6. from turtle import *
  7. import turtle as T
  8. from random import *
  9. def ground():
  10. T.hideturtle()
  11. T.speed(100)
  12. for i in range(400):
  13. T.pensize(randint(5,10))
  14. x=randint(-400,350)
  15. y=randint(-280,-1)
  16. r=-y/280
  17. g=-y/280
  18. b=-y/280
  19. T.pencolor(r,g,b)
  20. T.penup()
  21. T.goto(x,y)
  22. T.pendown()
  23. T.forward(randint(40,100))
  24. def snow():
  25. T.hideturtle()
  26. T.speed(100)
  27. T.pensize(2)
  28. for i in range(100):
  29. r=random()
  30. g=random()
  31. b=random()
  32. T.pencolor(r,g,b)
  33. T.penup()
  34. T.setx(randint(-350,350))
  35. T.sety(randint(1,270))
  36. T.pendown()
  37. dens=randint(8,12)
  38. snowsize=randint(10,14)
  39. for j in range(dens):
  40. T.forward(snowsize)
  41. T.backward(snowsize)
  42. T.right(360/dens)
  43. def main():
  44. T.setup(800, 600, 0, 0)
  45. T.tracer(False)
  46. T.bgcolor("black")
  47. snow()
  48. ground()
  49. T.tracer(True)
  50. T.mainloop()
  51. main()

29. 五星红旗

  1. import turtle
  2. import math
  3. turtle.pensize(1)
  4. turtle.up()
  5. turtle.left(90)
  6. turtle.forward(200)
  7. turtle.left(90)
  8. turtle.forward(300)
  9. turtle.down()
  10. turtle.color("red")
  11. turtle.begin_fill()
  12. for i in range(2):
  13. turtle.left(90)
  14. turtle.forward(400)
  15. turtle.left(90)
  16. turtle.forward(600)
  17. turtle.end_fill()
  18. turtle.up()
  19. turtle.left(90)
  20. turtle.forward(100)
  21. turtle.left(90)
  22. turtle.forward(100)
  23. turtle.left(90)
  24. turtle.forward(60)
  25. turtle.down()
  26. # 大五角星
  27. turtle.color("yellow")
  28. turtle.right(162)
  29. def paint1():
  30. turtle.begin_fill()
  31. l = (60 * math.cos(math.radians(18))) / (1 + math.sin(math.radians(18)))
  32. for i in range(5):
  33. turtle.forward(l)
  34. turtle.left(72)
  35. turtle.forward(l)
  36. turtle.right(144)
  37. turtle.end_fill()
  38. paint1()
  39. # 画五角星
  40. def paint2():
  41. turtle.right(162)
  42. turtle.begin_fill()
  43. l = (20 * math.cos(math.radians(18))) / (1 + math.sin(math.radians(18)))
  44. for i in range(5):
  45. turtle.forward(l)
  46. turtle.left(72)
  47. turtle.forward(l)
  48. turtle.right(144)
  49. turtle.end_fill()
  50. def paint3():
  51. turtle.right(72)
  52. turtle.begin_fill()
  53. l = (20 * math.cos(math.radians(18))) / (1 + math.sin(math.radians(18)))
  54. for i in range(5):
  55. turtle.forward(l)
  56. turtle.left(72)
  57. turtle.forward(l)
  58. turtle.right(144)
  59. turtle.end_fill()
  60. def paint4():
  61. turtle.left(18)
  62. turtle.begin_fill()
  63. l = (20 * math.cos(math.radians(18))) / (1 + math.sin(math.radians(18)))
  64. for i in range(5):
  65. turtle.forward(l)
  66. turtle.left(72)
  67. turtle.forward(l)
  68. turtle.right(144)
  69. turtle.end_fill()
  70. # 第一个小五角星
  71. turtle.up()
  72. turtle.left(72)
  73. turtle.forward(120)
  74. turtle.down()
  75. paint2()
  76. # 第二个小五角星
  77. turtle.up()
  78. turtle.left(162)
  79. turtle.forward(20)
  80. turtle.right(90)
  81. turtle.forward(40)
  82. turtle.right(math.degrees(math.atan(1 / 7)))
  83. turtle.forward(20)
  84. turtle.down()
  85. paint2()
  86. # 第三个小五角星
  87. turtle.up()
  88. turtle.left(180 - math.degrees(math.atan(1 / 7)))
  89. turtle.forward(20)
  90. turtle.left(90)
  91. turtle.forward(20 * (1 / 7))
  92. turtle.down()
  93. paint3()
  94. # 第四个小五角星
  95. turtle.up()
  96. turtle.right(18)
  97. turtle.forward(60)
  98. turtle.right(90)
  99. turtle.forward(20)
  100. turtle.down()
  101. paint4()
  102. turtle.color("red")

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/276721
推荐阅读
相关标签
  

闽ICP备14008679号