Python绘制哆啦A梦、皮卡丘、圣诞树

àì夳堔傛蜴生んèń 2022-10-31 15:48 357阅读 0赞

Python绘制哆啦A梦、皮卡丘


Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

  • 官网:https://docs.python.org/zh-cn/3/library/turtle.html
  • https://github.com/PerpetualSmile

参考了网络代码,跟过turtle库用法可以参考turtle官网


turtle库绘制哆啦A梦

在这里插入图片描述

  1. from turtle import *
  2. # 无轨迹跳跃
  3. def my_goto(x, y):
  4. penup()
  5. goto(x, y)
  6. pendown()
  7. # 眼睛
  8. def eyes():
  9. fillcolor("#ffffff")
  10. begin_fill()
  11. tracer(False)
  12. a = 2.5
  13. for i in range(120):
  14. if 0 <= i < 30 or 60 <= i < 90:
  15. a -= 0.05
  16. lt(3)
  17. fd(a)
  18. else:
  19. a += 0.05
  20. lt(3)
  21. fd(a)
  22. tracer(True)
  23. end_fill()
  24. # 胡须
  25. def beard():
  26. my_goto(-32, 135)
  27. seth(165)
  28. fd(60)
  29. my_goto(-32, 125)
  30. seth(180)
  31. fd(60)
  32. my_goto(-32, 115)
  33. seth(193)
  34. fd(60)
  35. my_goto(37, 135)
  36. seth(15)
  37. fd(60)
  38. my_goto(37, 125)
  39. seth(0)
  40. fd(60)
  41. my_goto(37, 115)
  42. seth(-13)
  43. fd(60)
  44. # 嘴巴
  45. def mouth():
  46. my_goto(5, 148)
  47. seth(270)
  48. fd(100)
  49. seth(0)
  50. circle(120, 50)
  51. seth(230)
  52. circle(-120, 100)
  53. # 围巾
  54. def scarf():
  55. fillcolor('#e70010')
  56. begin_fill()
  57. seth(0)
  58. fd(200)
  59. circle(-5, 90)
  60. fd(10)
  61. circle(-5, 90)
  62. fd(207)
  63. circle(-5, 90)
  64. fd(10)
  65. circle(-5, 90)
  66. end_fill()
  67. # 鼻子
  68. def nose():
  69. my_goto(-10, 158)
  70. seth(315)
  71. fillcolor('#e70010')
  72. begin_fill()
  73. circle(20)
  74. end_fill()
  75. # 黑眼睛
  76. def black_eyes():
  77. seth(0)
  78. my_goto(-20, 195)
  79. fillcolor('#000000')
  80. begin_fill()
  81. circle(13)
  82. end_fill()
  83. pensize(6)
  84. my_goto(20, 205)
  85. seth(75)
  86. circle(-10, 150)
  87. pensize(3)
  88. my_goto(-17, 200)
  89. seth(0)
  90. fillcolor('#ffffff')
  91. begin_fill()
  92. circle(5)
  93. end_fill()
  94. my_goto(0, 0)
  95. # 脸
  96. def face():
  97. fd(183)
  98. lt(45)
  99. fillcolor('#ffffff')
  100. begin_fill()
  101. circle(120, 100)
  102. seth(180)
  103. # print(pos())
  104. fd(121)
  105. pendown()
  106. seth(215)
  107. circle(120, 100)
  108. end_fill()
  109. my_goto(63.56, 218.24)
  110. seth(90)
  111. eyes()
  112. seth(180)
  113. penup()
  114. fd(60)
  115. pendown()
  116. seth(90)
  117. eyes()
  118. penup()
  119. seth(180)
  120. fd(64)
  121. # 头型
  122. def head():
  123. penup()
  124. circle(150, 40)
  125. pendown()
  126. fillcolor('#00a0de')
  127. begin_fill()
  128. circle(150, 280)
  129. end_fill()
  130. # 画哆啦A梦
  131. def Doraemon():
  132. # 头部
  133. head()
  134. # 围脖
  135. scarf()
  136. # 脸
  137. face()
  138. # 红鼻子
  139. nose()
  140. # 嘴巴
  141. mouth()
  142. # 胡须
  143. beard()
  144. # 身体
  145. my_goto(0, 0)
  146. seth(0)
  147. penup()
  148. circle(150, 50)
  149. pendown()
  150. seth(30)
  151. fd(40)
  152. seth(70)
  153. circle(-30, 270)
  154. fillcolor('#00a0de')
  155. begin_fill()
  156. seth(230)
  157. fd(80)
  158. seth(90)
  159. circle(1000, 1)
  160. seth(-89)
  161. circle(-1000, 10)
  162. # print(pos())
  163. seth(180)
  164. fd(70)
  165. seth(90)
  166. circle(30, 180)
  167. seth(180)
  168. fd(70)
  169. # print(pos())
  170. seth(100)
  171. circle(-1000, 9)
  172. seth(-86)
  173. circle(1000, 2)
  174. seth(230)
  175. fd(40)
  176. # print(pos())
  177. circle(-30, 230)
  178. seth(45)
  179. fd(81)
  180. seth(0)
  181. fd(203)
  182. circle(5, 90)
  183. fd(10)
  184. circle(5, 90)
  185. fd(7)
  186. seth(40)
  187. circle(150, 10)
  188. seth(30)
  189. fd(40)
  190. end_fill()
  191. # 左手
  192. seth(70)
  193. fillcolor('#ffffff')
  194. begin_fill()
  195. circle(-30)
  196. end_fill()
  197. # 脚
  198. my_goto(103.74, -182.59)
  199. seth(0)
  200. fillcolor('#ffffff')
  201. begin_fill()
  202. fd(15)
  203. circle(-15, 180)
  204. fd(90)
  205. circle(-15, 180)
  206. fd(10)
  207. end_fill()
  208. my_goto(-96.26, -182.59)
  209. seth(180)
  210. fillcolor('#ffffff')
  211. begin_fill()
  212. fd(15)
  213. circle(15, 180)
  214. fd(90)
  215. circle(15, 180)
  216. fd(10)
  217. end_fill()
  218. # 右手
  219. my_goto(-133.97, -91.81)
  220. seth(50)
  221. fillcolor('#ffffff')
  222. begin_fill()
  223. circle(30)
  224. end_fill()
  225. # 口袋
  226. my_goto(-103.42, 15.09)
  227. seth(0)
  228. fd(38)
  229. seth(230)
  230. begin_fill()
  231. circle(90, 260)
  232. end_fill()
  233. my_goto(5, -40)
  234. seth(0)
  235. fd(70)
  236. seth(-90)
  237. circle(-70, 180)
  238. seth(0)
  239. fd(70)
  240. # 铃铛
  241. my_goto(-103.42, 15.09)
  242. fd(90)
  243. seth(70)
  244. fillcolor('#ffd200')
  245. # print(pos())
  246. begin_fill()
  247. circle(-20)
  248. end_fill()
  249. seth(170)
  250. fillcolor('#ffd200')
  251. begin_fill()
  252. circle(-2, 180)
  253. seth(10)
  254. circle(-100, 22)
  255. circle(-2, 180)
  256. seth(180 - 10)
  257. circle(100, 22)
  258. end_fill()
  259. goto(-13.42, 15.09)
  260. seth(250)
  261. circle(20, 110)
  262. seth(90)
  263. fd(15)
  264. dot(10)
  265. my_goto(0, -150)
  266. # 画眼睛
  267. black_eyes()
  268. if __name__ == '__main__':
  269. screensize(800, 600, "#f0f0f0")
  270. pensize(3) # 画笔宽度
  271. speed(9) # 画笔速度
  272. Doraemon()
  273. my_goto(100, -300)
  274. write('CSDN北山啦', font=("Bradley Hand ITC", 30, "bold"))
  275. mainloop()

turtle绘制皮卡丘

在这里插入图片描述

  1. import turtle
  2. def getPosition(x, y):
  3. turtle.setx(x)
  4. turtle.sety(y)
  5. print(x, y)
  6. class Pikachu:
  7. def __init__(self):
  8. self.t = turtle.Turtle()
  9. t = self.t
  10. t.pensize(3)
  11. t.speed(9)
  12. t.ondrag(getPosition)
  13. def noTrace_goto(self, x, y):
  14. self.t.penup()
  15. self.t.goto(x, y)
  16. self.t.pendown()
  17. def leftEye(self, x, y):
  18. self.noTrace_goto(x, y)
  19. t = self.t
  20. t.seth(0)
  21. t.fillcolor('#333333')
  22. t.begin_fill()
  23. t.circle(22)
  24. t.end_fill()
  25. self.noTrace_goto(x, y+10)
  26. t.fillcolor('#000000')
  27. t.begin_fill()
  28. t.circle(10)
  29. t.end_fill()
  30. self.noTrace_goto(x+6, y + 22)
  31. t.fillcolor('#ffffff')
  32. t.begin_fill()
  33. t.circle(10)
  34. t.end_fill()
  35. def rightEye(self, x, y):
  36. self.noTrace_goto(x, y)
  37. t = self.t
  38. t.seth(0)
  39. t.fillcolor('#333333')
  40. t.begin_fill()
  41. t.circle(22)
  42. t.end_fill()
  43. self.noTrace_goto(x, y+10)
  44. t.fillcolor('#000000')
  45. t.begin_fill()
  46. t.circle(10)
  47. t.end_fill()
  48. self.noTrace_goto(x-6, y + 22)
  49. t.fillcolor('#ffffff')
  50. t.begin_fill()
  51. t.circle(10)
  52. t.end_fill()
  53. def mouth(self, x, y):
  54. self.noTrace_goto(x, y)
  55. t = self.t
  56. t.fillcolor('#88141D')
  57. t.begin_fill()
  58. # 下嘴唇
  59. l1 = []
  60. l2 = []
  61. t.seth(190)
  62. a = 0.7
  63. for i in range(28):
  64. a += 0.1
  65. t.right(3)
  66. t.fd(a)
  67. l1.append(t.position())
  68. self.noTrace_goto(x, y)
  69. t.seth(10)
  70. a = 0.7
  71. for i in range(28):
  72. a += 0.1
  73. t.left(3)
  74. t.fd(a)
  75. l2.append(t.position())
  76. # 上嘴唇
  77. t.seth(10)
  78. t.circle(50, 15)
  79. t.left(180)
  80. t.circle(-50, 15)
  81. t.circle(-50, 40)
  82. t.seth(233)
  83. t.circle(-50, 55)
  84. t.left(180)
  85. t.circle(50, 12.1)
  86. t.end_fill()
  87. # 舌头
  88. self.noTrace_goto(17, 54)
  89. t.fillcolor('#DD716F')
  90. t.begin_fill()
  91. t.seth(145)
  92. t.circle(40, 86)
  93. t.penup()
  94. for pos in reversed(l1[:20]):
  95. t.goto(pos[0], pos[1]+1.5)
  96. for pos in l2[:20]:
  97. t.goto(pos[0], pos[1]+1.5)
  98. t.pendown()
  99. t.end_fill()
  100. # 鼻子
  101. self.noTrace_goto(-17, 94)
  102. t.seth(8)
  103. t.fd(4)
  104. t.back(8)
  105. # 红脸颊
  106. def leftCheek(self, x, y):
  107. turtle.tracer(False)
  108. t = self.t
  109. self.noTrace_goto(x, y)
  110. t.seth(300)
  111. t.fillcolor('#DD4D28')
  112. t.begin_fill()
  113. a = 2.3
  114. for i in range(120):
  115. if 0 <= i < 30 or 60 <= i < 90:
  116. a -= 0.05
  117. t.lt(3)
  118. t.fd(a)
  119. else:
  120. a += 0.05
  121. t.lt(3)
  122. t.fd(a)
  123. t.end_fill()
  124. turtle.tracer(True)
  125. def rightCheek(self, x, y):
  126. t = self.t
  127. turtle.tracer(False)
  128. self.noTrace_goto(x, y)
  129. t.seth(60)
  130. t.fillcolor('#DD4D28')
  131. t.begin_fill()
  132. a = 2.3
  133. for i in range(120):
  134. if 0 <= i < 30 or 60 <= i < 90:
  135. a -= 0.05
  136. t.lt(3)
  137. t.fd(a)
  138. else:
  139. a += 0.05
  140. t.lt(3)
  141. t.fd(a)
  142. t.end_fill()
  143. turtle.tracer(True)
  144. def colorLeftEar(self, x, y):
  145. t = self.t
  146. self.noTrace_goto(x, y)
  147. t.fillcolor('#000000')
  148. t.begin_fill()
  149. t.seth(330)
  150. t.circle(100, 35)
  151. t.seth(219)
  152. t.circle(-300, 19)
  153. t.seth(110)
  154. t.circle(-30, 50)
  155. t.circle(-300, 10)
  156. t.end_fill()
  157. def colorRightEar(self, x, y):
  158. t = self.t
  159. self.noTrace_goto(x, y)
  160. t.fillcolor('#000000')
  161. t.begin_fill()
  162. t.seth(300)
  163. t.circle(-100, 30)
  164. t.seth(35)
  165. t.circle(300, 15)
  166. t.circle(30, 50)
  167. t.seth(190)
  168. t.circle(300, 17)
  169. t.end_fill()
  170. def body(self):
  171. t = self.t
  172. t.fillcolor('#F6D02F')
  173. t.begin_fill()
  174. # 右脸轮廓
  175. t.penup()
  176. t.circle(130, 40)
  177. t.pendown()
  178. t.circle(100, 105)
  179. t.left(180)
  180. t.circle(-100, 5)
  181. # 右耳朵
  182. t.seth(20)
  183. t.circle(300, 30)
  184. t.circle(30, 50)
  185. t.seth(190)
  186. t.circle(300, 36)
  187. # 上轮廓
  188. t.seth(150)
  189. t.circle(150, 70)
  190. # 左耳朵
  191. t.seth(200)
  192. t.circle(300, 40)
  193. t.circle(30, 50)
  194. t.seth(20)
  195. t.circle(300, 35)
  196. #print(t.pos())
  197. # 左脸轮廓
  198. t.seth(240)
  199. t.circle(105, 95)
  200. t.left(180)
  201. t.circle(-105, 5)
  202. # 左手
  203. t.seth(210)
  204. t.circle(500, 18)
  205. t.seth(200)
  206. t.fd(10)
  207. t.seth(280)
  208. t.fd(7)
  209. t.seth(210)
  210. t.fd(10)
  211. t.seth(300)
  212. t.circle(10, 80)
  213. t.seth(220)
  214. t.fd(10)
  215. t.seth(300)
  216. t.circle(10, 80)
  217. t.seth(240)
  218. t.fd(12)
  219. t.seth(0)
  220. t.fd(13)
  221. t.seth(240)
  222. t.circle(10, 70)
  223. t.seth(10)
  224. t.circle(10, 70)
  225. t.seth(10)
  226. t.circle(300, 18)
  227. t.seth(75)
  228. t.circle(500, 8)
  229. t.left(180)
  230. t.circle(-500, 15)
  231. t.seth(250)
  232. t.circle(100, 65)
  233. # 左脚
  234. t.seth(320)
  235. t.circle(100, 5)
  236. t.left(180)
  237. t.circle(-100, 5)
  238. t.seth(220)
  239. t.circle(200, 20)
  240. t.circle(20, 70)
  241. t.seth(60)
  242. t.circle(-100, 20)
  243. t.left(180)
  244. t.circle(100, 20)
  245. t.seth(300)
  246. t.circle(10, 70)
  247. t.seth(60)
  248. t.circle(-100, 20)
  249. t.left(180)
  250. t.circle(100, 20)
  251. t.seth(10)
  252. t.circle(100, 60)
  253. # 横向
  254. t.seth(180)
  255. t.circle(-100, 10)
  256. t.left(180)
  257. t.circle(100, 10)
  258. t.seth(5)
  259. t.circle(100, 10)
  260. t.circle(-100, 40)
  261. t.circle(100, 35)
  262. t.left(180)
  263. t.circle(-100, 10)
  264. # 右脚
  265. t.seth(290)
  266. t.circle(100, 55)
  267. t.circle(10, 50)
  268. t.seth(120)
  269. t.circle(100, 20)
  270. t.left(180)
  271. t.circle(-100, 20)
  272. t.seth(0)
  273. t.circle(10, 50)
  274. t.seth(110)
  275. t.circle(100, 20)
  276. t.left(180)
  277. t.circle(-100, 20)
  278. t.seth(30)
  279. t.circle(20, 50)
  280. t.seth(100)
  281. t.circle(100, 40)
  282. # 右侧身体轮廓
  283. t.seth(200)
  284. t.circle(-100, 5)
  285. t.left(180)
  286. t.circle(100, 5)
  287. t.left(30)
  288. t.circle(100, 75)
  289. t.right(15)
  290. t.circle(-300, 21)
  291. t.left(180)
  292. t.circle(300, 3)
  293. # 右手
  294. t.seth(43)
  295. t.circle(200, 60)
  296. t.right(10)
  297. t.fd(10)
  298. t.circle(5, 160)
  299. t.seth(90)
  300. t.circle(5, 160)
  301. t.seth(90)
  302. t.fd(10)
  303. t.seth(90)
  304. t.circle(5, 180)
  305. t.fd(10)
  306. t.left(180)
  307. t.left(20)
  308. t.fd(10)
  309. t.circle(5, 170)
  310. t.fd(10)
  311. t.seth(240)
  312. t.circle(50, 30)
  313. t.end_fill()
  314. self.noTrace_goto(130, 125)
  315. t.seth(-20)
  316. t.fd(5)
  317. t.circle(-5, 160)
  318. t.fd(5)
  319. # 手指纹
  320. self.noTrace_goto(166, 130)
  321. t.seth(-90)
  322. t.fd(3)
  323. t.circle(-4, 180)
  324. t.fd(3)
  325. t.seth(-90)
  326. t.fd(3)
  327. t.circle(-4, 180)
  328. t.fd(3)
  329. # 尾巴
  330. self.noTrace_goto(168, 134)
  331. t.fillcolor('#F6D02F')
  332. t.begin_fill()
  333. t.seth(40)
  334. t.fd(200)
  335. t.seth(-80)
  336. t.fd(150)
  337. t.seth(210)
  338. t.fd(150)
  339. t.left(90)
  340. t.fd(100)
  341. t.right(95)
  342. t.fd(100)
  343. t.left(110)
  344. t.fd(70)
  345. t.right(110)
  346. t.fd(80)
  347. t.left(110)
  348. t.fd(30)
  349. t.right(110)
  350. t.fd(32)
  351. t.right(106)
  352. t.circle(100, 25)
  353. t.right(15)
  354. t.circle(-300, 2)
  355. ##############
  356. #print(t.pos())
  357. t.seth(30)
  358. t.fd(40)
  359. t.left(100)
  360. t.fd(70)
  361. t.right(100)
  362. t.fd(80)
  363. t.left(100)
  364. t.fd(46)
  365. t.seth(66)
  366. t.circle(200, 38)
  367. t.right(10)
  368. t.fd(10)
  369. t.end_fill()
  370. # 尾巴花纹
  371. t.fillcolor('#923E24')
  372. self.noTrace_goto(126.82, -156.84)
  373. t.begin_fill()
  374. t.seth(30)
  375. t.fd(40)
  376. t.left(100)
  377. t.fd(40)
  378. t.pencolor('#923e24')
  379. t.seth(-30)
  380. t.fd(30)
  381. t.left(140)
  382. t.fd(20)
  383. t.right(150)
  384. t.fd(20)
  385. t.left(150)
  386. t.fd(20)
  387. t.right(150)
  388. t.fd(20)
  389. t.left(130)
  390. t.fd(18)
  391. t.pencolor('#000000')
  392. t.seth(-45)
  393. t.fd(67)
  394. t.right(110)
  395. t.fd(80)
  396. t.left(110)
  397. t.fd(30)
  398. t.right(110)
  399. t.fd(32)
  400. t.right(106)
  401. t.circle(100, 25)
  402. t.right(15)
  403. t.circle(-300, 2)
  404. t.end_fill()
  405. # 帽子、眼睛、嘴巴、脸颊
  406. self.cap(-134.07, 147.81)
  407. self.mouth(-5, 25)
  408. self.leftCheek(-126, 32)
  409. self.rightCheek(107, 63)
  410. self.colorLeftEar(-250, 100)
  411. self.colorRightEar(140, 270)
  412. self.leftEye(-85, 90)
  413. self.rightEye(50, 110)
  414. t.hideturtle()
  415. def cap(self, x, y):
  416. self.noTrace_goto(x, y)
  417. t = self.t
  418. t.fillcolor('#CD0000')
  419. t.begin_fill()
  420. t.seth(200)
  421. t.circle(400, 7)
  422. t.left(180)
  423. t.circle(-400, 30)
  424. t.circle(30, 60)
  425. t.fd(50)
  426. t.circle(30, 45)
  427. t.fd(60)
  428. t.left(5)
  429. t.circle(30, 70)
  430. t.right(20)
  431. t.circle(200, 70)
  432. t.circle(30, 60)
  433. t.fd(70)
  434. # print(t.pos())
  435. t.right(35)
  436. t.fd(50)
  437. t.circle(8, 100)
  438. t.end_fill()
  439. self.noTrace_goto(-168.47, 185.52)
  440. t.seth(36)
  441. t.circle(-270, 54)
  442. t.left(180)
  443. t.circle(270, 27)
  444. t.circle(-80, 98)
  445. t.fillcolor('#444444')
  446. t.begin_fill()
  447. t.left(180)
  448. t.circle(80, 197)
  449. t.left(58)
  450. t.circle(200, 45)
  451. t.end_fill()
  452. self.noTrace_goto(-58, 270)
  453. t.pencolor('#228B22')
  454. t.dot(35)
  455. self.noTrace_goto(-30, 280)
  456. t.fillcolor('#228B22')
  457. t.begin_fill()
  458. t.seth(100)
  459. t.circle(30, 180)
  460. t.seth(190)
  461. t.fd(15)
  462. t.seth(100)
  463. t.circle(-45, 180)
  464. t.right(90)
  465. t.fd(15)
  466. t.end_fill()
  467. t.pencolor('#000000')
  468. def start(self):
  469. self.body()
  470. def main():
  471. print('Painting the Pikachu... ')
  472. turtle.screensize(800, 600)
  473. turtle.title('Pikachu')
  474. pikachu = Pikachu()
  475. pikachu.start()
  476. turtle.mainloop()
  477. if __name__ == '__main__':
  478. main()

turtle绘制圣诞树

在这里插入图片描述

  1. import turtle
  2. screen = turtle.Screen()
  3. screen.setup(800, 600)
  4. circle = turtle.Turtle()
  5. circle.shape('circle')
  6. circle.color('red')
  7. circle.speed('fastest')
  8. circle.up()
  9. square = turtle.Turtle()
  10. square.shape('square')
  11. square.color('green')
  12. square.speed('fastest')
  13. square.up()
  14. circle.goto(0, 280)
  15. circle.stamp()
  16. k = 0
  17. for i in range(1, 17):
  18. y = 30 * i
  19. for j in range(i - k):
  20. x = 30 * j
  21. square.goto(x, -y + 280)
  22. square.stamp()
  23. square.goto(-x, -y + 280)
  24. square.stamp()
  25. if i % 4 == 0:
  26. x = 30 * (j + 1)
  27. circle.color('red')
  28. circle.goto(-x, -y + 280)
  29. circle.stamp()
  30. circle.goto(x, -y + 280)
  31. circle.stamp()
  32. k += 2
  33. if i % 4 == 3:
  34. x = 30 * (j + 1)
  35. circle.color('yellow')
  36. circle.goto(-x, -y + 280)
  37. circle.stamp()
  38. circle.goto(x, -y + 280)
  39. circle.stamp()
  40. square.color('brown')
  41. for i in range(17, 20):
  42. y = 30 * i
  43. for j in range(3):
  44. x = 30 * j
  45. square.goto(x, -y + 280)
  46. square.stamp()
  47. square.goto(-x, -y + 280)
  48. square.stamp()
  49. turtle.exitonclick()

到这里就结束了,如果对你有帮助,欢迎点赞关注,你的点赞对我很重要。作者:北山啦
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读

    相关 的party

    Problem Description 去年冬天,皮卡丘认识了大家,因此皮卡丘决定给大家举办一场欢快的party。 这次party最大的活动就是宝可梦对战大赛,这次比赛是