经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
Python+Turtle绘制蜘蛛侠的示例代码
来源:jb51  时间:2022/6/20 12:22:02  对本文有异议

蜘蛛侠(Spider-Man)即彼得·帕克(Peter Parker),是美国漫威漫画旗下超级英雄。

由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于《惊奇幻想》(Amazing Fantasy)第15期(1962年8月).

因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画。

网易云中关于蜘蛛侠主题曲热评过万的评论说到。

蜘蛛侠之所以成为最受欢迎的超级英雄之一,是因为这面具下的人,不分肤色、种族、性别。。。

他/她可能是你,是我,是和你一起生活的人。

任何人都能带上这个面具,你也可以做到,如果你以前没有想过,希望现在可以了~

本文主要介绍运用python中的turtle库控制函数绘制蜘蛛侠。

一、效果展示

在介绍代码之前,先来看下本文的实现效果。

可以参考下面步骤把Python文件转化成exe,发给未安装Python的他/她。

Pinstaller(Python打包为exe文件)

之前自己把 Python 文件打包成 exe 的时候,折腾了很久,本文将详细地讲述如何快速生成在不安装 Python 的电脑上也能执行的文件

1. 在 prompt 中运行 pip install pyinstaller , 安装 pyinstaller 库

2.  在 prompt 中运行 where pyinstaller 

3.  找到待打包文件存放的路径

把要打包的文件放到找到的路径 

C:\Users\Administrator\Anaconda3\Scripts 中 (我的路径是这个,你就按照第二步的路径)

4.  调用 cmd 窗口

把待打包文件放在

C:\Users\Administrator\Anaconda3 \Scripts 目录下,在该文件夹中按shift+鼠标右键 , 点击 在此处打开命令窗口 调用 cmd 

5.  在 cmd 中输入 pyinstaller -F  文件名

例子:打包 Python 绘制皮卡丘的视频,在cmd中输入 pyinstaller -F  pkq_1.py

即可生成普通图标的exe可执行文件。

6.  生成 exe 文件

可以在路径

C:\Users\Administrator\Anaconda3\Scripts 下的 dist 文件夹中找到打包好的exe文件(即不用安装 Python 也可以运行的文件)。

这样生成的文件图标是标准固定格式,如果想生成特定特定形状的图标需要用第7点中的语句。

7.  生成自定义形状的图标,在cmd中输入:pyinstaller -i  ico路径 -F xxxxx.py

例子: 打包  Python 绘制皮卡丘视频的py文件,在cmd中输入 (注: 我把ico图标和待打包文件放到一个文件夹下了, 所以直接输入了ico的名字)

  1. pyinstaller?-i??pikaqiu2.ico?-F?pkq_1.py

生成图标是皮卡丘形状的exe文件。

二、代码详解

Python绘制蜘蛛侠的原理是:应用turtle库绘制身体的不同部位。

1.导入库

首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。

  1. # -*- coding: UTF-8 -*-
  2. '''
  3. 代码用途 :画蜘蛛侠
  4. 作者 :阿黎逸阳
  5. 博客 : https://blog.csdn.net/qq_32532663/article/details/106176609
  6. '''
  7. import os
  8. import pygame
  9. import turtle as t

本文应用到的库较少,只应用了os、pygame和turtle三个库。

os库可以设置文件读取的位置。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

2.播放音乐

接着应用pygame库播放背景音乐,本文的音乐是《Sunflower》。

  1. os.chdir(r'F:\公众号\56.蜘蛛侠')
  2. #播放音乐
  3. print('播放音乐')
  4. pygame.mixer.init()
  5. pygame.mixer.music.load("Cope - Sunflower (Original Version).mp3")
  6. pygame.mixer.music.set_volume(0.5)
  7. pygame.mixer.music.play(1, 10)

这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。

如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。

有部分朋友对这一块有疑问,填充格式可参考如下图片:

3.定义画蜘蛛侠上半身的函数

然后设置画板的大小,并定义绘制蜘蛛侠上半身的函数。

  1. t.title('阿黎逸阳的代码公众号')
  2. t.speed(10)
  3. #t.screensize(1000, 800)
  4. t.setup(startx=0, starty = 0, width=800, height = 600)
  5. def up_body():
  6. #画头
  7. t.penup()
  8. t.goto(60, 200)
  9. t.pendown()
  10. t.pensize(1)
  11. t.color('black', 'red')
  12. t.begin_fill()
  13. t.setheading(60)
  14. t.circle(60, 30)
  15. t.left(4)
  16. t.circle(40, 173)
  17. t.left(4)
  18. t.circle(60, 30)
  19. #画脖子
  20. t.setheading(260)
  21. t.circle(30, 29)
  22. #画肩膀
  23. t.setheading(220)
  24. t.forward(30)
  25. #画手上肌肉
  26. t.setheading(150)
  27. t.circle(30, 130)
  28. #画胸部的内部线
  29. t.setheading(30)
  30. t.circle(-100, 13)
  31. t.setheading(270)
  32. t.circle(50, 40)
  33. t.setheading(255)
  34. t.circle(55, 40)
  35. t.circle(-40, 50)
  36. #画腰部的外横线
  37. t.setheading(0)
  38. t.forward(-7)
  39. t.setheading(270)
  40. t.forward(18)
  41. #画腰线
  42. t.setheading(-30)
  43. t.forward(50)
  44. t.setheading(15)
  45. t.forward(80)
  46. t.setheading(90)
  47. t.forward(22)
  48. #重复的地方
  49. #画衣服内轮廓
  50. t.setheading(190)
  51. t.forward(20)
  52. t.setheading(103)
  53. t.circle(-160, 41)
  54. #画手内轮廓
  55. t.setheading(5)
  56. t.circle(-80, 30)
  57. t.setheading(20)
  58. t.circle(30, 30)
  59. #重复的地方
  60. #手臂上肌肉
  61. t.setheading(70)
  62. t.circle(22, 150)
  63. t.setheading(150)
  64. t.forward(30)
  65. t.setheading(120)
  66. t.forward(15)
  67. t.end_fill()

关键代码详解:

t.pensize(width):设置画笔的尺寸。

t.color(color):设置画笔的颜色。

t.penup():抬起画笔,一般用于另起一个地方绘图使用。

t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。

t.pendown():放下画笔,一般和penup组合使用。

t.left(degree):画笔向左转多少度,括号里表示度数。

t.right(degree):画笔向右转多少度,括号里表示度数。

t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。

画外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得蜘蛛侠的轮廓比较流畅。

4.定义画左手和右手的函数

接着定义画左手和右手的函数。

  1. def left_hand():
  2. #画左手臂
  3. #画胸部的内部线
  4. t.penup()
  5. t.goto(-69, 134)
  6. t.color('black', 'blue')
  7. t.pendown()
  8. t.begin_fill()
  9. t.setheading(30)
  10. t.circle(-100, 13)
  11. t.setheading(270)
  12. t.circle(50, 40)
  13. t.setheading(255)
  14. t.circle(55, 40)
  15. t.circle(-40, 50)
  16. #画腰部的外横线
  17. t.setheading(0)
  18. t.forward(-8)
  19. t.setheading(90)
  20. t.circle(220, 18)
  21. t.setheading(-90)
  22. t.circle(-40, 50)
  23. t.setheading(-85)
  24. t.circle(-50, 50)
  25. t.setheading(135)
  26. t.circle(30, 40)
  27. t.setheading(95)
  28. t.circle(-50, 50)
  29. t.setheading(98)
  30. t.circle(-60, 51)
  31. t.end_fill()
  32. def right_hand():
  33. #画右手臂
  34. #画衣服内轮廓
  35. t.penup()
  36. t.goto(80, 39)
  37. t.color('black', 'blue')
  38. t.pendown()
  39. t.begin_fill()
  40. t.setheading(190)
  41. t.forward(20)
  42. t.setheading(103)
  43. t.circle(-160, 41)
  44. #画手内轮廓
  45. t.setheading(5)
  46. t.circle(-80, 30)
  47. t.setheading(20)
  48. t.circle(30, 30)
  49. t.setheading(-20)
  50. t.circle(-55, 65)
  51. t.setheading(-30)
  52. t.circle(-50, 60)
  53. t.setheading(180)
  54. t.circle(30, 40)
  55. t.setheading(154)
  56. t.circle(-48, 60)
  57. t.setheading(164)
  58. t.circle(-50, 60)
  59. t.setheading(-90)
  60. t.circle(-40, 60)
  61. t.left(40)
  62. t.circle(150, 23)
  63. t.end_fill()
  64. def left_wrist():
  65. #画左手腕
  66. t.penup()
  67. t.goto(-81, 37)
  68. t.color('black', 'red')
  69. t.pendown()
  70. t.begin_fill()
  71. t.setheading(135)
  72. t.circle(30, 40)
  73. t.setheading(-90)
  74. t.circle(-60, 30)
  75. t.setheading(-90)
  76. t.forward(20)
  77. t.setheading(-45)
  78. t.forward(12)
  79. t.circle(6, 180)
  80. t.setheading(-50)
  81. t.circle(5, 160)
  82. t.setheading(95)
  83. t.forward(10)
  84. t.setheading(135)
  85. t.forward(8)
  86. t.setheading(95)
  87. t.forward(6)
  88. t.setheading(35)
  89. t.circle(30, 10)
  90. t.left(10)
  91. t.circle(30, 27)
  92. t.end_fill()
  93. #画手腕上的线
  94. #横线
  95. #第一条横线
  96. t.penup()
  97. t.goto(-84, 30)
  98. t.color('black')
  99. t.pendown()
  100. t.setheading(145)
  101. t.circle(30, 36)
  102. #第二条横线
  103. t.penup()
  104. t.goto(-90, 22)
  105. t.color('black')
  106. t.pendown()
  107. t.setheading(185)
  108. t.circle(-30, 31)
  109. #第三条横线
  110. t.penup()
  111. t.goto(-83, 10)
  112. t.color('black')
  113. t.pendown()
  114. t.setheading(210)
  115. t.circle(-50, 31)
  116. #第四条横线
  117. t.penup()
  118. t.goto(-102, -10)
  119. t.color('black')
  120. t.pendown()
  121. t.setheading(50)
  122. t.circle(-20, 41)
  123. t.setheading(55)
  124. t.circle(-90, 8)
  125. #第一条竖线
  126. t.penup()
  127. t.goto(-105, 24)
  128. t.color('black')
  129. t.pendown()
  130. t.setheading(-95)
  131. t.circle(100, 20)
  132. #第二条竖线
  133. t.penup()
  134. t.goto(-87, 42)
  135. t.color('black')
  136. t.pendown()
  137. t.setheading(-110)
  138. t.forward(22)
  139. t.setheading(-63)
  140. t.circle(-50, 40)
  141. def right_wrist():
  142. #画右手腕
  143. t.penup()
  144. t.goto(189, 57)
  145. t.color('black', 'red')
  146. t.pendown()
  147. t.begin_fill()
  148. t.setheading(180)
  149. t.circle(30, 40)
  150. t.setheading(-55)
  151. t.circle(-100, 10)
  152. t.circle(-20, 70)
  153. t.setheading(-90)
  154. t.forward(10)
  155. t.setheading(-0)
  156. t.forward(5)
  157. t.setheading(-85)
  158. t.forward(8)
  159. t.setheading(-20)
  160. t.circle(8, 60)
  161. t.setheading(-35)
  162. t.circle(8, 70)
  163. t.setheading(-15)
  164. t.circle(6, 70)
  165. t.setheading(60)
  166. t.circle(20, 80)
  167. t.setheading(115)
  168. t.circle(-100, 20)
  169. t.end_fill()
  170. #画第一条横线
  171. t.goto(191, 45)
  172. t.color('black')
  173. t.pendown()
  174. t.setheading(215)
  175. t.circle(-30, 34)
  176. #画第二条横线
  177. t.penup()
  178. t.goto(197, 29)
  179. t.color('black')
  180. t.pendown()
  181. t.setheading(215)
  182. t.circle(-30, 37)
  183. #画第三条横线
  184. t.penup()
  185. t.goto(174, 11)
  186. t.color('black')
  187. t.pendown()
  188. t.setheading(-0)
  189. t.circle(-30, 27)
  190. t.setheading(20)
  191. t.circle(-20, 27)
  192. t.setheading(40)
  193. t.circle(-30, 23)
  194. #画第一条竖线
  195. t.penup()
  196. t.goto(178, 55)
  197. t.color('black')
  198. t.pendown()
  199. t.setheading(-70)
  200. t.circle(-200, 9)
  201. t.setheading(-82)
  202. t.circle(-100, 18)
  203. #画第二条竖线
  204. t.penup()
  205. t.goto(185, 55)
  206. t.color('black')
  207. t.pendown()
  208. t.setheading(-70)
  209. t.circle(-200, 8)
  210. t.setheading(-68)
  211. t.circle(-80, 25)

5.定义画蜘蛛的函数

接着定义画蜘蛛的函数。

  1. def spider():
  2. #画蜘蛛
  3. t.penup()
  4. t.goto(8, 146)
  5. t.color('black')
  6. t.pendown()
  7. t.begin_fill()
  8. t.setheading(-120)
  9. t.circle(40, 60)
  10. t.setheading(60)
  11. t.circle(40,60)
  12. t.end_fill()
  13. #画蜘蛛的脚
  14. #右边的脚1
  15. t.penup()
  16. t.goto(13, 129)
  17. t.color('black')
  18. t.pendown()
  19. t.setheading(30)
  20. t.forward(10)
  21. t.setheading(90)
  22. t.forward(15)
  23. #右边的脚2
  24. t.penup()
  25. t.goto(14, 125)
  26. t.color('black')
  27. t.pendown()
  28. t.setheading(30)
  29. t.forward(16)
  30. t.setheading(90)
  31. t.forward(17)
  32. #右边的脚3
  33. t.penup()
  34. t.goto(14, 124)
  35. t.color('black')
  36. t.pendown()
  37. t.setheading(-20)
  38. t.forward(16)
  39. t.setheading(-90)
  40. t.forward(17)
  41. #右边的脚4
  42. t.penup()
  43. t.goto(14, 120)
  44. t.color('black')
  45. t.pendown()
  46. t.setheading(-20)
  47. t.forward(10)
  48. t.setheading(-90)
  49. t.forward(15)
  50. #画蜘蛛的脚
  51. #左边的脚1
  52. t.penup()
  53. t.goto(3, 129)
  54. t.color('black')
  55. t.pendown()
  56. t.setheading(150)
  57. t.forward(10)
  58. t.setheading(90)
  59. t.forward(15)
  60. #右边的脚2
  61. t.penup()
  62. t.goto(2, 125)
  63. t.color('black')
  64. t.pendown()
  65. t.setheading(150)
  66. t.forward(16)
  67. t.setheading(90)
  68. t.forward(17)
  69. #右边的脚3
  70. t.penup()
  71. t.goto(2, 124)
  72. t.color('black')
  73. t.pendown()
  74. t.setheading(-170)
  75. t.forward(16)
  76. t.setheading(-99)
  77. t.forward(17)
  78. #右边的脚4
  79. t.penup()
  80. t.goto(3, 120)
  81. t.color('black')
  82. t.pendown()
  83. t.setheading(-170)
  84. t.forward(10)
  85. t.setheading(-90)
  86. t.forward(15)

6.调用函数绘制图形

最后调用函数绘制图形。

  1. print('绘制上半身外轮廓')
  2. up_body()
  3. print('绘制右手')
  4. right_hand()
  5. print('绘制左手')
  6. left_hand()
  7. print('绘制左拳头')
  8. left_wrist()
  9. print('绘制右拳头')
  10. right_wrist()
  11. print('绘制蜘蛛')
  12. spider()

至此,在Python中实现蜘蛛侠的绘制逻辑已大致讲解完毕。

以上就是Python+Turtle绘制蜘蛛侠的示例代码的详细内容,更多关于Python Turtle绘制蜘蛛侠的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号