当前位置:   article > 正文

Python绘制玫瑰花_python画玫瑰花

python画玫瑰花

文章目录


前言

今天我们来画一朵玫瑰花。


一、第一种画法

这应该是最好看玫瑰花了。

  1. from turtle import *
  2. #设置绘制起点
  3. setup (700,700)
  4. #画笔颜色
  5. pencolor("red")
  6. hideturtle()
  7. penup()
  8. goto(0,280)
  9. pendown()
  10. left(135)
  11. speed(0)
  12. #左侧弧形绘制
  13. a=0.2
  14. for i in range (80):
  15. if i < 50:
  16. pensize(a)
  17. forward(2)
  18. a+=0.15
  19. if i<10:
  20. left(0.7)
  21. elif 10<i<24:
  22. left(1)
  23. elif 24<i<40:
  24. left(3)
  25. elif 40<i<50:
  26. left(4)
  27. elif i >=50:
  28. pensize(a)
  29. forward(2.2)
  30. a-=0.25
  31. if 50<i<65:
  32. left(6.7)
  33. elif 65<i<80:
  34. left(2)
  35. setheading(0)
  36. penup()
  37. goto(40,305)
  38. pendown()
  39. left(145)
  40. #右侧弧形绘制
  41. a=0.1
  42. for i in range (100):
  43. if i < 60:
  44. pensize(a)
  45. forward(3)
  46. a+=0.15
  47. if i<10:
  48. left(0.8)
  49. elif 10<i<24:
  50. left(0.8)
  51. elif 24<i<40:
  52. left(1.7)
  53. elif 40<i<60:
  54. left(2.2)
  55. elif i >=60:
  56. pensize(a)
  57. forward(2.4)
  58. a-=0.15
  59. if 60<i<85:
  60. left(3.2)
  61. elif 85<i<90:
  62. left(4)
  63. setheading(0)
  64. penup()
  65. goto(45,320)
  66. pendown()
  67. left(10)
  68. a=0.1
  69. for i in range (100):
  70. if i < 60:
  71. pensize(a)
  72. forward(3)
  73. a+=0.15
  74. if i<17:
  75. right(3.2)
  76. elif 17<i<20:
  77. right(3.5)
  78. elif 20<i<40:
  79. right(6.8)
  80. elif 40<i<50:
  81. right(0.6)
  82. elif 50<i<60:
  83. left(1)
  84. elif i >=60:
  85. pensize(a)
  86. forward(2.4)
  87. a-=0.16
  88. if 60<i<85:
  89. left(0.4)
  90. elif 85<i<90:
  91. left(1)
  92. setheading(0)
  93. penup()
  94. goto(-135,265)
  95. pendown()
  96. left(160)
  97. a=0.1
  98. for i in range (120):
  99. if i < 60:
  100. pensize(a)
  101. forward(3)
  102. a+=0.15
  103. if i<10:
  104. left(2)
  105. elif 10<i<15:
  106. left(8)
  107. elif 15<i<35:
  108. left(6)
  109. elif 35<i<40:
  110. left(4)
  111. elif 40<i<60:
  112. left(1)
  113. elif i >=60:
  114. pensize(a)
  115. forward(3)
  116. a-=0.15
  117. if 60<i<90:
  118. right(0.4)
  119. elif 90<i<120:
  120. right(0.8)
  121. setheading(0)
  122. penup()
  123. goto(80,250)
  124. pendown()
  125. left(245)
  126. a=0.2
  127. for i in range (80):
  128. if i < 40:
  129. pensize(a)
  130. forward(1.2)
  131. a+=0.15
  132. if i<15:
  133. left(0.6)
  134. elif 15<i<40:
  135. left(0.3)
  136. elif i >=40:
  137. pensize(a)
  138. forward(1.2)
  139. a-=0.15
  140. if 40<i<65:
  141. left(0.6)
  142. elif 65<i<80:
  143. left(0.35)
  144. setheading(0)
  145. penup()
  146. goto(-150,195)
  147. pendown()
  148. left(300)
  149. a=0.2
  150. for i in range (80):
  151. if i < 40:
  152. pensize(a)
  153. forward(1.2)
  154. a+=0.16
  155. if i<15:
  156. right(0.6)
  157. elif 15<i<40:
  158. right(0.3)
  159. elif i >=40:
  160. pensize(a)
  161. forward(1.2)
  162. a-=0.16
  163. if 40<i<65:
  164. right(0.6)
  165. elif 65<i<80:
  166. right(0.35)
  167. setheading(0)
  168. penup()
  169. goto(-205,225)
  170. pendown()
  171. left(194)
  172. a=0.1
  173. for i in range (100):
  174. if i < 50:
  175. pensize(a)
  176. forward(3)
  177. a+=0.15
  178. if i<10:
  179. left(7)
  180. elif 10<i<30:
  181. left(3)
  182. elif 30<i<50:
  183. left(0.1)
  184. elif i >=50:
  185. pensize(a)
  186. forward(3)
  187. a-=0.15
  188. if 50<i<80:
  189. right(0.3)
  190. elif 80<i<100:
  191. right(0.6)
  192. setheading(0)
  193. penup()
  194. goto(110,280)
  195. pendown()
  196. left(6)
  197. a=0.1
  198. for i in range (120):
  199. if i < 60:
  200. pensize(a)
  201. forward(3)
  202. a+=0.15
  203. if i<10:
  204. right(4.5)
  205. elif 10<i<30:
  206. right(5)
  207. elif 30<i<60:
  208. right(0.1)
  209. elif i >=60:
  210. pensize(a)
  211. forward(3)
  212. a-=0.15
  213. if 50<i<80:
  214. left(0.6)
  215. elif 80<i<100:
  216. left(0.9)
  217. setheading(0)
  218. penup()
  219. goto(-160,110)
  220. pendown()
  221. left(283)
  222. a=0.2
  223. for i in range (80):
  224. if i < 40:
  225. pensize(a)
  226. forward(2.4)
  227. a+=0.15
  228. if i<10:
  229. right(0.6)
  230. elif 15<i<40:
  231. right(0.1)
  232. elif i >=40:
  233. pensize(a)
  234. forward(2.4)
  235. a-=0.15
  236. if 40<i<65:
  237. left(2)
  238. elif 65<i<80:
  239. left(3)
  240. setheading(0)
  241. penup()
  242. goto(90,155)
  243. pendown()
  244. left(258)
  245. a=0.2
  246. for i in range (80):
  247. if i < 40:
  248. pensize(a)
  249. forward(3)
  250. a+=0.15
  251. if i<10:
  252. left(0.8)
  253. elif 15<i<40:
  254. left(0.1)
  255. elif i >=40:
  256. pensize(a)
  257. forward(3)
  258. a-=0.15
  259. if 40<i<65:
  260. right(2)
  261. elif 65<i<80:
  262. right(3)
  263. setheading(0)
  264. penup()
  265. goto(-60,-45)
  266. pendown()
  267. right(9)
  268. a=0.2
  269. for i in range (40):
  270. if i < 20:
  271. pensize(a)
  272. forward(1.3)
  273. a+=0.15
  274. if i<5:
  275. left(0.7)
  276. elif 5<i<20:
  277. left(0.4)
  278. elif i >=20:
  279. pensize(a)
  280. forward(1.3)
  281. a-=0.15
  282. if 20<i<35:
  283. left(0.7)
  284. elif 35<i<40:
  285. left(0.4)
  286. setheading(0)
  287. penup()
  288. goto(-70,-55)
  289. pencolor("green")
  290. pendown()
  291. right(170)
  292. a=0.2
  293. for i in range (80):
  294. if i <40 :
  295. pensize(a)
  296. forward(2)
  297. a+=0.15
  298. if i<25:
  299. right(0.5)
  300. elif 25<i<40:
  301. right(1)
  302. elif i >=40:
  303. pensize(a)
  304. forward(2.5)
  305. a-=0.15
  306. if 40<i<55:
  307. right(1)
  308. elif 55<i<70:
  309. right(2)
  310. elif 70<i<80:
  311. right(1)
  312. setheading(270)
  313. left(30)
  314. a=0.2
  315. for i in range (80):
  316. if i <40 :
  317. pensize(a)
  318. forward(2.5)
  319. a+=0.15
  320. if i<25:
  321. left(0.5)
  322. elif 25<i<40:
  323. left(1)
  324. elif i >=40:
  325. pensize(a)
  326. forward(3)
  327. a-=0.15
  328. if 40<i<55:
  329. left(2)
  330. elif 55<i<70:
  331. left(1.3)
  332. elif 70<i<80:
  333. left(1)
  334. setheading(270)
  335. penup()
  336. goto(20,-55)
  337. pendown()
  338. left(65)
  339. a=0.2
  340. for i in range (60):
  341. if i <30 :
  342. pensize(a)
  343. forward(2)
  344. a+=0.15
  345. if i<15:
  346. left(1)
  347. elif 15<i<30:
  348. left(2)
  349. elif i >=30:
  350. pensize(a)
  351. forward(2.5)
  352. a-=0.15
  353. if 30<i<45:
  354. left(2)
  355. elif 45<i<50:
  356. left(2.5)
  357. elif 50<i<60:
  358. left(1.5)
  359. setheading(270)
  360. right(17)
  361. a=0.2
  362. for i in range (80):
  363. if i <40 :
  364. pensize(a)
  365. forward(2.2)
  366. a+=0.15
  367. if i<25:
  368. right(1)
  369. elif 25<i<40:
  370. right(2)
  371. elif i >=40:
  372. pensize(a)
  373. forward(2.5)
  374. a-=0.15
  375. if 40<i<55:
  376. right(2)
  377. elif 55<i<70:
  378. right(1.5)
  379. elif 70<i<80:
  380. right(0.6)
  381. setheading(0)
  382. penup()
  383. pencolor("brown")
  384. goto(-49,-90)
  385. pendown()
  386. right(89)
  387. a=1
  388. for i in range (120):
  389. if i < 60:
  390. pensize(a)
  391. forward(3.5)
  392. a+=0.08
  393. elif i >=60:
  394. pensize(a)
  395. forward(3.5)
  396. a-=0.08
  397. setheading(0)
  398. penup()
  399. goto(-16,-70)
  400. pendown()
  401. right(91)
  402. a=1
  403. for i in range (120):
  404. if i < 60:
  405. pensize(a)
  406. forward(3.5)
  407. a+=0.08
  408. elif i >=60:
  409. pensize(a)
  410. forward(3.5)
  411. a-=0.08
  412. setheading(0)
  413. speed(1)
  414. penup()
  415. goto(-16,-150)
  416. pendown()
  417. pensize(3)
  418. left(35)
  419. forward(15)
  420. setheading(0)
  421. right(115)
  422. forward(34)
  423. setheading(180)
  424. penup()
  425. goto(-49,-250)
  426. pendown()
  427. pensize(3)
  428. right(35)
  429. forward(15)
  430. setheading(180)
  431. left(115)
  432. forward(34)
  433. input()
  434. done

二、第二种画法

第二种就稍逊一筹了,但也挺好看。

  1. import turtle
  2. # 设置初始位置
  3. turtle.penup()
  4. turtle.left(90)
  5. turtle.fd(200)
  6. turtle.pendown()
  7. turtle.right(90)
  8. # 花蕊
  9. turtle.fillcolor("red")
  10. turtle.begin_fill()
  11. turtle.circle(10,180)
  12. turtle.circle(25,110)
  13. turtle.left(50)
  14. turtle.circle(60,45)
  15. turtle.circle(20,170)
  16. turtle.right(24)
  17. turtle.fd(30)
  18. turtle.left(10)
  19. turtle.circle(30,110)
  20. turtle.fd(20)
  21. turtle.left(40)
  22. turtle.circle(90,70)
  23. turtle.circle(30,150)
  24. turtle.right(30)
  25. turtle.fd(15)
  26. turtle.circle(80,90)
  27. turtle.left(15)
  28. turtle.fd(45)
  29. turtle.right(165)
  30. turtle.fd(20)
  31. turtle.left(155)
  32. turtle.circle(150,80)
  33. turtle.left(50)
  34. turtle.circle(150,90)
  35. turtle.end_fill()
  36. # 花瓣1
  37. turtle.left(150)
  38. turtle.circle(-90,70)
  39. turtle.left(20)
  40. turtle.circle(75,105)
  41. turtle.setheading(60)
  42. turtle.circle(80,98)
  43. turtle.circle(-90,40)
  44. # 花瓣2
  45. turtle.left(180)
  46. turtle.circle(90,40)
  47. turtle.circle(-80,98)
  48. turtle.setheading(-83)
  49. # 叶子1
  50. turtle.fd(30)
  51. turtle.left(90)
  52. turtle.fd(25)
  53. turtle.left(45)
  54. turtle.fillcolor("green")
  55. turtle.begin_fill()
  56. turtle.circle(-80,90)
  57. turtle.right(90)
  58. turtle.circle(-80,90)
  59. turtle.end_fill()
  60. turtle.right(135)
  61. turtle.fd(60)
  62. turtle.left(180)
  63. turtle.fd(85)
  64. turtle.left(90)
  65. turtle.fd(80)
  66. # 叶子2
  67. turtle.right(90)
  68. turtle.right(45)
  69. turtle.fillcolor("green")
  70. turtle.begin_fill()
  71. turtle.circle(80,90)
  72. turtle.left(90)
  73. turtle.circle(80,90)
  74. turtle.end_fill()
  75. turtle.left(135)
  76. turtle.fd(60)
  77. turtle.left(180)
  78. turtle.fd(60)
  79. turtle.right(90)
  80. turtle.circle(200,60)

总结

所画玫瑰花的库用的只是turtle,但代码却很多。

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

闽ICP备14008679号