当前位置:   article > 正文

Python的turtle模块用法及实例 六:魔法阵七:樱花树 八:小猪佩奇九:多来爱梦_海龟画魔法阵

海龟画魔法阵

目录

turtle:基本用法

一:画圆

二:奥运五环

三:美队盾牌

 四:繁星

 五:星空

 六:魔法阵

七:樱花树

 八:小猪佩奇

九:多来爱梦


 

turtle:基本用法

turtle.down() #移动时绘制图形,缺省时也为绘制

turtle.up() #移动时不绘制图形

turtle.pensize(width) #绘制图形时的宽度

turtle.color(colorstring) #绘制图形时的颜色

turtle.fillcolor(colorstring) #绘制图形的填充颜色

turtle.done() 停留在结束界面

turtle.hideturtle() 隐藏图标

turtle.showturtle() 显示图标

turtle.begin_fill() 开始填充

turtle.fillcolor(颜色) 填充颜色

turtle.end_fill() 结束填充

turtle.setheading() 改变笔头朝向

turtle.pencolor(colorstr)笔画颜色

turtle.pendown()(别名:turtle.pd(),turtle.down()):移动时绘制图形,缺省时也为绘制

 

运动命令:

turtle.forward(degree) #向前移动距离degree代表距离

turtle.backward(degree) #向后移动距离degree代表距离

turtle.right(degree) #向右移动多少度

turtle.left(degree) #向左移动多少度

turtle.goto(x,y) #将画笔移动到坐标为x,y的位置

turtle.stamp() #复制当前图形

turtle.speed(speed) #画笔绘制的速度范围[0,10]整数

turtle.clear() 清空turtle画的笔迹

turtle.reset() 清空窗口,重置turtle状态为起始状态

turtle.undo() (未测试)撤销上一个turtle动作

turtle.isvisible() (未测试)返回当前turtle是否可见

turtle.stamp() (未测试)复制当前图形

turtle.write(‘vshmily’) 写字符串’vshmily’

turtle.write(s[,font=(“font-name”,font_size,“font_type”)]) (未测试)写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项 turtle.circle(7) 画一个半径为7的圆 turtle.circle(77, steps=3) 三边形,画一个半径为77的园的内切多边形

turtle.circle(77, 300) 圆弧为300度

turtle.screensize(800, 600, “green”)

turtle.screensize() #返回默认大小(400, 300)

 

一:画圆

  1. import turtle # 导入模块
  2. turtle.screensize(800, 600, 'green') # 创建画布
  3. turtle.circle(40) # 画笔循环画个圆,以画布中心为起点,半径为40

turtle.screensize() #返回默认大小(400, 300)

58fe9a0f078d47ffadafbcc9e9149824.png

 

  1. import turtle as tur
  2. tur.speed(0) # 设置速度,让画的速度最快
  3. # 第一个图
  4. for i in range(12):
  5. tur.pencolor('black')
  6. tur.right(91)
  7. tur.circle(100)
  8. # 第二个图
  9. # for i in range(50):
  10. # tur.pencolor('red')
  11. # tur.right(63)
  12. # tur.circle(57)
  13. tur.done()

f35b0f26f0ad4c34a3f5a9f65ec5bb5f.png

 4c7372de15ba41fe8c5f4efb09b2585b.png

二:奥运五环

  1. import turtle as tur
  2. tur.speed(0)
  3. tur.pensize(9)
  4. tur.color('black')
  5. tur.circle(80)
  6. tur.penup()
  7. tur.goto(-140,0)
  8. tur.pendown()
  9. tur.color('blue')
  10. tur.circle(80)
  11. tur.penup()
  12. tur.goto(140,0)
  13. tur.pendown()
  14. tur.color('red')
  15. tur.circle(80)
  16. tur.penup()
  17. tur.goto(80,-100)
  18. tur.pendown()
  19. tur.color('yellow')
  20. tur.circle(80)
  21. tur.penup()
  22. tur.goto(-60,-100)
  23. tur.pendown()
  24. tur.color('green')
  25. tur.circle(80)
  26. tur.penup()
  27. tur.goto(-60,160)
  28. tur.pendown()
  29. tur.done()

三:美队盾牌

  1. import turtle as tur
  2. tur.speed(0)
  3. tur.penup()
  4. tur.goto(0,-100)
  5. tur.pendown()
  6. tur.color('red')
  7. tur.begin_fill()
  8. tur.circle(150)
  9. tur.end_fill()
  10. tur.penup()
  11. tur.goto(0,-70)
  12. tur.pendown()
  13. tur.color('white')
  14. tur.begin_fill()
  15. tur.circle(120)
  16. tur.end_fill()
  17. tur.penup()
  18. tur.goto(0,-50)
  19. tur.pendown()
  20. tur.color('red')
  21. tur.begin_fill()
  22. tur.circle(100)
  23. tur.end_fill()
  24. tur.penup()
  25. tur.goto(0,-10)
  26. tur.pendown()
  27. tur.color('blue')
  28. tur.begin_fill()
  29. tur.circle(60)
  30. tur.end_fill()
  31. tur.penup()
  32. tur.goto(-55,70)
  33. tur.pendown()
  34. tur.color('yellow')
  35. tur.begin_fill()
  36. for i in range(5):
  37. tur.forward(109)
  38. tur.right(144)
  39. tur.end_fill()
  40. tur.done()

3938282b11ab4a9394f8914c56898fc4.png

 四:繁星

  1. import random
  2. import turtle as tur
  3. tur.speed(0)
  4. tur.colormode(255)
  5. tur.bgcolor('black')
  6. tur.pensize(120)
  7. tur.goto(-300,250)
  8. tur.forward(600)
  9. tur.color(50,50,50)
  10. tur.penup()
  11. tur.goto(-350,130)
  12. tur.pendown()
  13. tur.forward(700)
  14. tur.color(75,75,75)
  15. tur.penup()
  16. tur.goto(-350,10)
  17. tur.pendown()
  18. tur.forward(700)
  19. tur.color(100,100,100)
  20. tur.penup()
  21. tur.goto(-350,-110)
  22. tur.pendown()
  23. tur.forward(700)
  24. tur.color(125,125,125)
  25. tur.penup()
  26. tur.goto(-350,-230)
  27. tur.pendown()
  28. tur.forward(800)
  29. # 画星星
  30. for i in range(10):
  31. tur.pensize(5)
  32. tur.color('yellow')
  33. tur.penup()
  34. tur.goto(random.randint(-200,200),random.randint(-200,200))
  35. tur.pendown()
  36. tur.begin_fill()
  37. for i in range(10):
  38. tur.forward(10)
  39. if i%2 == 0:
  40. tur.left(35)
  41. else:
  42. tur.right(105)
  43. tur.end_fill()
  44. tur.done()

dc86be03a5f24904af1b378cc41ad85f.png

 五:星空

  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(int(-height/2),int(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(int(-height/2),int(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(int(-height/2),int(height/2)))
  32. star.showturtle()

c643cbe006a44a36a31398afeafce3f7.png

 六:魔法阵

  1. import turtle as t
  2. def tcyuan(x, y, r):
  3. t.fillcolor("black")
  4. t.begin_fill()
  5. t.seth(0)
  6. y = y - r
  7. t.penup()
  8. t.goto(x, y)
  9. t.pendown()
  10. t.circle(r)
  11. t.end_fill()
  12. def yuan(x, y, r):
  13. t.seth(0)
  14. y = y - r
  15. t.penup()
  16. t.goto(x, y)
  17. t.pendown()
  18. t.circle(r)
  19. def yueliang():
  20. R = 110 - 1
  21. r = R - 22 - 1
  22. # 月亮填充
  23. t.penup()
  24. t.goto(-350 + 2 * R, 0)
  25. t.seth(90)
  26. t.fillcolor("black")
  27. t.begin_fill()
  28. t.circle(R, 359)
  29. t.left(90)
  30. t.fd(2)
  31. t.left(90)
  32. t.circle(-r, 359)
  33. t.left(90)
  34. t.fd(2)
  35. t.pendown()
  36. t.end_fill()
  37. # 轮廓
  38. yuan(-350 + R, 0, R)
  39. yuan(-350 + 44 + r - 2, 0, r - 2)
  40. def zhixian(R, r, count, jiaodu):
  41. t.seth(90 + jiaodu)
  42. # t.goto(0, 0)
  43. for i in range(count):
  44. t.penup()
  45. t.goto(0, 0)
  46. t.fd(r)
  47. t.pendown()
  48. t.fd(R - r)
  49. t.left(360 / count)
  50. def zfx(R, r):
  51. jiange = 10
  52. # t.pensize(jiange)
  53. t.seth(90)
  54. big = pow((R ** 2) * 2, 0.5)
  55. small = big - 2 * jiange
  56. for i in range(13):
  57. # 大线
  58. t.penup()
  59. t.goto(0, 0)
  60. t.fd(R)
  61. t.pendown()
  62. t.right(135)
  63. t.fd(big)
  64. # 小线
  65. t.left(135)
  66. t.penup()
  67. t.goto(0, 0)
  68. t.fd(pow((small ** 2) / 2, 0.5))
  69. t.pendown()
  70. t.right(135)
  71. t.fd(small)
  72. # 粗线
  73. t.pensize(8)
  74. t.pencolor("black")
  75. t.left(135)
  76. t.penup()
  77. t.goto(0, 0)
  78. t.fd((R + pow((small ** 2) / 2, 0.5)) / 2)
  79. t.pendown()
  80. t.right(135)
  81. t.fd((big + small) / 2)
  82. t.pensize(2)
  83. t.pencolor("yellow")
  84. t.seth(90 + i * 30)
  85. else:
  86. # 大线
  87. t.penup()
  88. t.goto(0, 0)
  89. t.fd(R)
  90. t.right(135)
  91. t.fd(big / 2)
  92. t.pendown()
  93. t.fd(big / 2)
  94. # 小线
  95. t.left(135)
  96. t.penup()
  97. t.goto(0, 0)
  98. t.fd(pow((small ** 2) / 2, 0.5))
  99. t.right(135)
  100. t.fd(small / 2)
  101. t.pendown()
  102. t.fd(small / 2)
  103. # 粗线
  104. t.pensize(8)
  105. t.pencolor("black")
  106. t.left(135)
  107. t.penup()
  108. t.goto(0, 0)
  109. t.fd((R + pow((small ** 2) / 2, 0.5)) / 2)
  110. t.right(135)
  111. t.fd((big + small) / 2 / 2)
  112. t.pendown()
  113. t.fd((big + small) / 2 / 2)
  114. t.pensize(2)
  115. t.pencolor("yellow")
  116. t.seth(90 + i * 30)
  117. def wjx(r, jiaodu):
  118. t.fillcolor("black")
  119. t.penup()
  120. t.goto(0, 0)
  121. t.seth(90 + jiaodu)
  122. t.fd(r)
  123. t.pendown()
  124. t.right(18)
  125. t.begin_fill()
  126. for i in range(5):
  127. t.right(144)
  128. t.forward(144)
  129. t.left(72)
  130. t.forward(144)
  131. t.end_fill()
  132. if jiaodu != 0:
  133. t.seth(90 + jiaodu)
  134. for i in range(1, 6):
  135. t.penup()
  136. t.goto(0, 0)
  137. t.left(72)
  138. t.pendown()
  139. t.fd(r)
  140. def xingzuo():
  141. r = 250
  142. t.penup()
  143. t.goto(20, -35)
  144. t.seth(-45)
  145. t.fd(r)
  146. t.pendown()
  147. xz = ['♒', '♓', '♈', '♉', '♌', '♍', '♎', '♏']
  148. for i in range(4):
  149. t.write(xz[i], font=("", 20, ""))
  150. t.penup()
  151. t.right(90)
  152. t.circle(-300, 30)
  153. t.left(90)
  154. t.pendown()
  155. t.penup()
  156. t.goto(-r / 4 + 10, 5)
  157. t.seth(135)
  158. t.fd(r)
  159. for i in range(4, 8):
  160. t.write(xz[i], font=("", 20, ""))
  161. t.penup()
  162. t.right(90)
  163. t.circle(-300, 30)
  164. t.left(90)
  165. t.pendown()
  166. def dxnb(s):
  167. t.penup()
  168. t.fd(-19)
  169. t.left(90)
  170. t.fd(2)
  171. t.pendown()
  172. t.write(s, font=["KaiTi", 30, "bold"])
  173. def taiyang():
  174. def haicao(r, i):
  175. # 海藻
  176. t.fillcolor("black")
  177. t.penup()
  178. if i == 0:
  179. t.goto(256, r)
  180. elif i == 1:
  181. t.goto(256 - r, 0)
  182. else:
  183. t.goto(256, -r)
  184. t.pendown()
  185. t.begin_fill()
  186. t.seth(2 + i * 90)
  187. t.circle(r / 2, 105)
  188. t.left(10)
  189. t.circle(-r / 3, 90)
  190. t.circle(r / 3, 60)
  191. t.left(20)
  192. t.circle(r / 3, -80)
  193. t.left(50)
  194. t.circle(-r + 10, -40)
  195. t.right(30)
  196. t.circle(r / 2 + 10, -50)
  197. t.penup()
  198. if i == 0:
  199. t.goto(256, r)
  200. elif i == 1:
  201. t.goto(256 - r, 0)
  202. else:
  203. t.goto(256, -r)
  204. t.pendown()
  205. t.end_fill()
  206. t.seth(2 + i * 90)
  207. t.circle(r / 2, 105)
  208. t.left(10)
  209. t.circle(-r / 3, 90)
  210. t.begin_fill()
  211. t.circle(r / 3, 60)
  212. t.left(20)
  213. t.circle(r / 3, -80)
  214. t.left(50)
  215. t.circle(-r + 10, -40)
  216. t.right(30)
  217. t.circle(r / 2 + 10, -50)
  218. t.right(30)
  219. t.circle(r / 2 - 2, 110)
  220. t.circle(-r / 3, 70)
  221. t.left(7)
  222. t.circle(r / 3, 85)
  223. t.end_fill()
  224. t.penup()
  225. if i == 0:
  226. t.goto(256, r)
  227. t.pendown()
  228. t.seth(180 - (2 + i * 90))
  229. t.circle(-(r / 2), 105)
  230. elif i == 1:
  231. t.goto(256 - r, 0)
  232. t.pendown()
  233. t.seth(- (2 + i * 90))
  234. t.circle(-(r / 2), 105)
  235. else:
  236. t.goto(256, -r)
  237. t.pendown()
  238. t.seth(180 - (2 + i * 90))
  239. t.circle(-(r / 2), 105)
  240. t.begin_fill()
  241. t.left(-10)
  242. t.circle(-(-r / 3), 90)
  243. t.circle(-(r / 3), 60)
  244. t.left(-20)
  245. t.circle(-(r / 3), -80)
  246. t.left(-50)
  247. t.circle(-(-r + 10), -40)
  248. t.right(-30)
  249. t.circle(-(r / 2 + 10), -50)
  250. t.end_fill()
  251. t.penup()
  252. if i == 0:
  253. t.goto(256, r)
  254. t.pendown()
  255. t.seth(180 - (2 + i * 90))
  256. t.circle(-(r / 2), 105)
  257. elif i == 1:
  258. t.goto(256 - r, 0)
  259. t.pendown()
  260. t.seth(- (2 + i * 90))
  261. t.circle(-(r / 2), 105)
  262. else:
  263. t.goto(256, -r)
  264. t.pendown()
  265. t.seth(180 - (2 + i * 90))
  266. t.circle(-(r / 2), 105)
  267. t.pendown()
  268. t.left(-10)
  269. t.circle(-(-r / 3), 90)
  270. t.circle(-(r / 3), 60)
  271. t.left(-20)
  272. t.begin_fill()
  273. t.circle(-(r / 3), -80)
  274. t.left(-50)
  275. t.circle(-(-r + 10), -40)
  276. t.right(-30)
  277. t.circle(-(r / 2 + 10), -50)
  278. t.right(-30)
  279. t.circle(-(r / 2 - 2), 110)
  280. t.circle(-(-r / 3), 70)
  281. t.left(-7)
  282. t.circle(-(r / 3), 85)
  283. t.end_fill()
  284. def xhaicao(r, i):
  285. t.penup()
  286. t.goto(256 + r, 0)
  287. t.seth(-90)
  288. t.circle(-r, 20)
  289. t.pendown()
  290. t.begin_fill()
  291. t.seth(30)
  292. t.circle(-r / 3, 100)
  293. t.circle(r / 6, 140)
  294. t.circle(-r / 11, 100)
  295. t.left(80)
  296. t.circle(-r / 2, -30)
  297. t.circle(r / 4, -140)
  298. t.circle(-r / 3, -60)
  299. t.end_fill()
  300. t.penup()
  301. t.goto(256 + r, 0)
  302. t.seth(-90)
  303. t.circle(-r, 30)
  304. t.pendown()
  305. t.seth(45)
  306. t.circle(-r / 4, 100)
  307. t.right(20)
  308. t.circle(r / 4, 140)
  309. t.right(10)
  310. t.circle(-r / 11, 90)
  311. t.penup()
  312. t.goto(256 + r, 0)
  313. t.seth(90)
  314. t.circle(r, 20)
  315. t.pendown()
  316. t.begin_fill()
  317. t.seth(-30)
  318. t.circle(-(-r / 3), 100)
  319. t.circle(-(r / 6), 140)
  320. t.circle(-(-r / 11), 100)
  321. t.left(-80)
  322. t.circle(-(-r / 2), -30)
  323. t.circle(-(r / 4), -140)
  324. t.circle(-(-r / 3), -60)
  325. t.end_fill()
  326. t.penup()
  327. t.goto(256 + r, 0)
  328. t.seth(90)
  329. t.circle(r, 30)
  330. t.pendown()
  331. t.seth(-45)
  332. t.circle(-(-r / 4), 100)
  333. t.right(-25)
  334. t.circle(-(r / 4), 140)
  335. t.right(-10)
  336. t.circle(-(-r / 11), 90)
  337. r = 50
  338. # 海藻
  339. haicao(r, 0)
  340. haicao(r, 1)
  341. haicao(r, 2)
  342. xhaicao(r, 3)
  343. # 大三角形
  344. t.fillcolor("black")
  345. for i in range(1, 4):
  346. temp = 3
  347. t.penup()
  348. t.goto(256, 0)
  349. t.seth(i * 90)
  350. t.pendown()
  351. t.begin_fill()
  352. t.right(22.5)
  353. t.fd(r)
  354. if i == 1:
  355. t.goto(256, 3 * r - temp)
  356. t.goto(256, 0)
  357. t.seth(i * 90 + 22.5)
  358. t.fd(r)
  359. t.goto(256, 3 * r - temp)
  360. elif i == 2:
  361. t.goto(256 - 3 * r + temp, 0)
  362. t.goto(256, 0)
  363. t.seth(i * 90 + 22.5)
  364. t.fd(r)
  365. t.goto(256 - 3 * r + temp, 0)
  366. else:
  367. t.goto(256, -3 * r + temp)
  368. t.goto(256, 0)
  369. t.seth(i * 90 + 22.5)
  370. t.fd(r)
  371. t.goto(256, -3 * r + temp)
  372. t.end_fill()
  373. # 小三角形
  374. x = pow(((2 * r) ** 2) / 2, 0.5) - 8
  375. for i in range(1, 5):
  376. t.penup()
  377. t.goto(256, 0)
  378. t.seth(i * 90)
  379. t.pendown()
  380. t.begin_fill()
  381. t.right(22.5)
  382. t.fd(r)
  383. if i == 1:
  384. t.goto(256 + x, x)
  385. t.goto(256, 0)
  386. t.right(45)
  387. t.fd(r)
  388. t.goto(256 + x, x)
  389. elif i == 2:
  390. t.goto(256 - x, x)
  391. t.goto(256, 0)
  392. t.right(45)
  393. t.fd(r)
  394. t.goto(256 - x, x)
  395. elif i == 3:
  396. t.goto(256 - x, -x)
  397. t.goto(256, 0)
  398. t.right(45)
  399. t.fd(r)
  400. t.goto(256 - x, -x)
  401. else:
  402. t.goto(256 + x, -x)
  403. t.goto(256, 0)
  404. t.right(45)
  405. t.fd(r)
  406. t.goto(256 + x, -x)
  407. t.end_fill()
  408. # 圆
  409. # t.begin_fill()
  410. tcyuan(256, 0, r)
  411. # 初始化
  412. t.setup(1500, 800, 0, 0)
  413. t.speed(0)
  414. t.bgcolor("black")
  415. t.pencolor("yellow")
  416. t.pensize(2)
  417. # 最大的圆
  418. yuan(0, 0, 350)
  419. yuan(0, 0, 325)
  420. yuan(0, 0, 321)
  421. yuan(0, 0, 306)
  422. zhixian(321, 306, 72, 0)
  423. # 小圆
  424. yuan(0, 0, 204)
  425. yuan(0, 0, 200)
  426. yuan(0, 0, 186)
  427. zhixian(200, 186, 72, 0)
  428. # 正方形边框以及直线
  429. zhixian(290, 213, 12, 0)
  430. zhixian(248, 205, 12, 15)
  431. zfx(306, 204)
  432. # 里五角星
  433. wjx(200, 36)
  434. # 月亮
  435. yueliang()
  436. # 太阳
  437. taiyang()
  438. # 最小圆
  439. tcyuan(0, 328, 22)
  440. dxnb("北")
  441. tcyuan(0, -328, 22)
  442. dxnb("南")
  443. tcyuan(-328, 0, 22)
  444. dxnb("西")
  445. tcyuan(328, 0, 22)
  446. dxnb("東")
  447. # 外五角星
  448. wjx(200, 0)
  449. # 星座
  450. xingzuo()
  451. t.penup()
  452. t.goto(-500, -500)
  453. t.pendown()
  454. t.done()

396b0e716d2b4104a0b1c0ca5609bc95.png

七:樱花树

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

65c2eccce71e4929878bc1eb1cbb4b08.png

 八:小猪佩奇

  1. # -*- coding:utf-8 -*-
  2. from turtle import*
  3. def nose(x,y):#鼻子
  4. penup()#提起笔
  5. goto(x,y)#定位
  6. pendown()#落笔,开始画
  7. setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
  8. begin_fill()#准备开始填充图形
  9. a=0.4
  10. for i in range(120):
  11. if 0<=i<30 or 60<=i<90:
  12. a=a+0.08
  13. left(3) #向左转3度
  14. forward(a) #向前走a的步长
  15. else:
  16. a=a-0.08
  17. left(3)
  18. forward(a)
  19. end_fill()#填充完成
  20. penup()
  21. setheading(90)
  22. forward(25)
  23. setheading(0)
  24. forward(10)
  25. pendown()
  26. pencolor(255,155,192)#画笔颜色
  27. setheading(10)
  28. begin_fill()
  29. circle(5)
  30. color(160,82,45)#返回或设置pencolor和fillcolor
  31. end_fill()
  32. penup()
  33. setheading(0)
  34. forward(20)
  35. pendown()
  36. pencolor(255,155,192)
  37. setheading(10)
  38. begin_fill()
  39. circle(5)
  40. color(160,82,45)
  41. end_fill()
  42. def head(x,y):#头
  43. color((255,155,192),"pink")
  44. penup()
  45. goto(x,y)
  46. setheading(0)
  47. pendown()
  48. begin_fill()
  49. setheading(180)
  50. circle(300,-30)
  51. circle(100,-60)
  52. circle(80,-100)
  53. circle(150,-20)
  54. circle(60,-95)
  55. setheading(161)
  56. circle(-300,15)
  57. penup()
  58. goto(-100,100)
  59. pendown()
  60. setheading(-30)
  61. a=0.4
  62. for i in range(60):
  63. if 0<=i<30 or 60<=i<90:
  64. a=a+0.08
  65. lt(3) #向左转3度
  66. fd(a) #向前走a的步长
  67. else:
  68. a=a-0.08
  69. lt(3)
  70. fd(a)
  71. end_fill()
  72. def ears(x,y): #耳朵
  73. color((255,155,192),"pink")
  74. penup()
  75. goto(x,y)
  76. pendown()
  77. begin_fill()
  78. setheading(100)
  79. circle(-50,50)
  80. circle(-10,120)
  81. circle(-50,54)
  82. end_fill()
  83. penup()
  84. setheading(90)
  85. forward(-12)
  86. setheading(0)
  87. forward(30)
  88. pendown()
  89. begin_fill()
  90. setheading(100)
  91. circle(-50,50)
  92. circle(-10,120)
  93. circle(-50,56)
  94. end_fill()
  95. def eyes(x,y):#眼睛
  96. color((255,155,192),"white")
  97. penup()
  98. setheading(90)
  99. forward(-20)
  100. setheading(0)
  101. forward(-95)
  102. pendown()
  103. begin_fill()
  104. circle(15)
  105. end_fill()
  106. color("black")
  107. penup()
  108. setheading(90)
  109. forward(12)
  110. setheading(0)
  111. forward(-3)
  112. pendown()
  113. begin_fill()
  114. circle(3)
  115. end_fill()
  116. color((255,155,192),"white")
  117. penup()
  118. seth(90)
  119. forward(-25)
  120. seth(0)
  121. forward(40)
  122. pendown()
  123. begin_fill()
  124. circle(15)
  125. end_fill()
  126. color("black")
  127. penup()
  128. setheading(90)
  129. forward(12)
  130. setheading(0)
  131. forward(-3)
  132. pendown()
  133. begin_fill()
  134. circle(3)
  135. end_fill()
  136. def cheek(x,y):#腮
  137. color((255,155,192))
  138. penup()
  139. goto(x,y)
  140. pendown()
  141. setheading(0)
  142. begin_fill()
  143. circle(30)
  144. end_fill()
  145. def mouth(x,y): #嘴
  146. color(239,69,19)
  147. penup()
  148. goto(x,y)
  149. pendown()
  150. setheading(-80)
  151. circle(30,40)
  152. circle(40,80)
  153. def body(x,y):#身体
  154. color("red",(255,99,71))
  155. penup()
  156. goto(x,y)
  157. pendown()
  158. begin_fill()
  159. setheading(-130)
  160. circle(100,10)
  161. circle(300,30)
  162. setheading(0)
  163. forward(230)
  164. setheading(90)
  165. circle(300,30)
  166. circle(100,3)
  167. color((255,155,192),(255,100,100))
  168. setheading(-135)
  169. circle(-80,63)
  170. circle(-150,24)
  171. end_fill()
  172. def hands(x,y):#手
  173. color((255,155,192))
  174. penup()
  175. goto(x,y)
  176. pendown()
  177. setheading(-160)
  178. circle(300,15)
  179. penup()
  180. setheading(90)
  181. forward(15)
  182. setheading(0)
  183. forward(0)
  184. pendown()
  185. setheading(-10)
  186. circle(-20,90)
  187. penup()
  188. setheading(90)
  189. forward(30)
  190. setheading(0)
  191. forward(237)
  192. pendown()
  193. setheading(-20)
  194. circle(-300,15)
  195. penup()
  196. setheading(90)
  197. forward(20)
  198. setheading(0)
  199. forward(0)
  200. pendown()
  201. setheading(-170)
  202. circle(20,90)
  203. def foot(x,y):#脚
  204. pensize(10)
  205. color((240,128,128))
  206. penup()
  207. goto(x,y)
  208. pendown()
  209. setheading(-90)
  210. forward(40)
  211. setheading(-180)
  212. color("black")
  213. pensize(15)
  214. fd(20)
  215. pensize(10)
  216. color((240,128,128))
  217. penup()
  218. setheading(90)
  219. forward(40)
  220. setheading(0)
  221. forward(90)
  222. pendown()
  223. setheading(-90)
  224. forward(40)
  225. setheading(-180)
  226. color("black")
  227. pensize(15)
  228. fd(20)
  229. def tail(x,y):#尾巴
  230. pensize(4)
  231. color((255,155,192))
  232. penup()
  233. goto(x,y)
  234. pendown()
  235. seth(0)
  236. circle(70,20)
  237. circle(10,330)
  238. circle(70,30)
  239. def setting(): #参数设置
  240. pensize(4)
  241. hideturtle() #使乌龟无形(隐藏)
  242. colormode(255) #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内
  243. color((255,155,192),"pink")
  244. setup(840,500)
  245. speed(10)
  246. def main():
  247. setting() #画布、画笔设置
  248. nose(-100,100) #鼻子
  249. head(-69,167) #头
  250. ears(0,160) #耳朵
  251. eyes(0,140) #眼睛
  252. cheek(80,10) #腮
  253. mouth(-20,30) #嘴
  254. body(-32,-8) #身体
  255. hands(-56,-45) #手
  256. foot(2,-177) #脚
  257. tail(148,-155) #尾巴
  258. done()
  259. if __name__ == '__main__':
  260. main()

九:多来爱梦

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

11fdbeb7edbd42d2a92a7dc041db989d.png

 

 

 

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

闽ICP备14008679号