来画一只你的小蛇吧!

1.

2.

3.了解turtle库

Turtle,也叫海龟渲染器,使用Turtle库画图也叫海龟作图。Turtle库是Python语言中一个很流行的绘制图像的函数库。海龟渲染器,和各种三维软件都有着良好的结合。功能强大,使用方便。该渲染器的特色在于其渲染速度可以优海龟渲染器,

和各种三维软件都有着良好的结合。功能强大,使用方便。化得非常快,相比起mental ray来说,这是他的一大优点。尤其是在全局光与final gather联用的时候效果更是明显。海龟渲染器在渲染大场景时非常有效,其对于光线的处理和色彩的鲜艳程度都要更胜三维软件自带的渲染器。其缺点在于对于三维软件的程序纹理贴图的支持不够,很多情况下并不能对它的材质球使用程序纹理贴图,这不能不说是一个遗憾。

代码:

  1. #pythondraw.py
  2. import turtle #引用 绘制(海龟)库
  3. turtle.setup(650,350,200,200) #启动窗体,宽650,高350
  4. turtle.penup() #抬起画笔
  5. turtle.fd(-250) #倒退250像素
  6. turtle.pendown() #落下画笔
  7. turtle.pensize(25) #画笔宽度是25像素
  8. turtle.pencolor('purple') #画笔颜色是紫色
  9. turtle.seth(-40) #调整方向为绝对40度
  10. for i in range(4): #循环4次,走曲线,r为40像素,角度为80
  11. turtle.circle(40,80)
  12. turtle.circle(-40,80)
  13. turtle.circle(40,80/2) #继续走曲线
  14. turtle.fd(40) #向前走40像素
  15. turtle.circle(16,180)
  16. turtle.fd(40 * 2/3)
  17. turtle.done() #结束绘制,不会主动退出;如果想绘图结束就关闭窗口,就去掉这一行

结果:

推荐使用:

画笔控制函数:

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

turtle.color(color1, color2):同时设置pencolor=color1, fillcolor=color2

turtle.filling():返回当前是否在填充状态

turtle.begin_fill():准备开始填充图形

turtle.end_fill():填充完成

turtle.hideturtle():隐藏画笔的turtle形状

turtle.showturtle():显示画笔的turtle形状

运动控制函数:

turtle.forward(distance):向当前画笔方向移动distance像素长度

turtle.backward(distance):向当前画笔相反方向移动distance像素长度

turtle.right(degree):顺时针移动degree°

turtle.left(degree):逆时针移动degree°

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

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

turtle.penup():提起笔移动,不绘制图形,用于另起一个地方绘制

turtle.circle():画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

setx( ):将当前x轴移动到指定位置

sety( ):将当前y轴移动到指定位置

setheading(angle):设置当前朝向为angle角度

home():设置当前画笔位置为原点,朝向东。

dot(r):绘制一个指定直径和颜色的圆点

方向控制函数:

循环与range函数:

print里加了逗号,输出时文字与参数会加一个空格

全局控制命令:

turtle.clear():清空turtle窗口,但是turtle的位置和状态不会改变

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

turtle.undo():撤销上一个turtle动作

turtle.isvisible():返回当前turtle是否可见

stamp():复制当前图形

turtle.write(s [,font=("font-name",font_size,"font_type")]):写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项

 

其他命令:

实例:

画个时钟:

代码:

  1. # coding=utf-8
  2.  
  3. import turtle
  4. from datetime import *
  5.  
  6. # 抬起画笔,向前运动一段距离放下
  7. def Skip(step):
  8. turtle.penup()
  9. turtle.forward(step)
  10. turtle.pendown()
  11.  
  12. def mkHand(name, length):
  13. # 注册Turtle形状,建立表针Turtle
  14. turtle.reset()
  15. Skip(-length * 0.1)
  16. # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
  17. turtle.begin_poly()
  18. turtle.forward(length * 1.1)
  19. # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
  20. turtle.end_poly()
  21. # 返回最后记录的多边形。
  22. handForm = turtle.get_poly()
  23. turtle.register_shape(name, handForm)
  24.  
  25. def Init():
  26. global secHand, minHand, hurHand, printer
  27. # 重置Turtle指向北
  28. turtle.mode("logo")
  29. # 建立三个表针Turtle并初始化
  30. mkHand("secHand", 135)
  31. mkHand("minHand", 125)
  32. mkHand("hurHand", 90)
  33. secHand = turtle.Turtle()
  34. secHand.shape("secHand")
  35. minHand = turtle.Turtle()
  36. minHand.shape("minHand")
  37. hurHand = turtle.Turtle()
  38. hurHand.shape("hurHand")
  39.  
  40. for hand in secHand, minHand, hurHand:
  41. hand.shapesize(1, 1, 3)
  42. hand.speed(0)
  43.  
  44. # 建立输出文字Turtle
  45. printer = turtle.Turtle()
  46.  
  47. # 隐藏画笔的turtle形状
  48. printer.hideturtle()
  49. printer.penup()
  50.  
  51. def SetupClock(radius):
  52. # 建立表的外框
  53. turtle.reset()
  54. turtle.pensize(7)
  55. turtle.pencolor("#ff5500")
  56. turtle.fillcolor("green")
  57.  
  58. for i in range(60):
  59. Skip(radius)
  60. if i % 5 == 0:
  61. turtle.forward(20)
  62. Skip(-radius - 20)
  63.  
  64. Skip(radius + 20)
  65. if i == 0:
  66. turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
  67. elif i == 30:
  68. Skip(25)
  69. turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
  70. Skip(-25)
  71. elif (i == 25 or i == 35):
  72. Skip(20)
  73. turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
  74. Skip(-20)
  75. else:
  76. turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
  77. Skip(-radius - 20)
  78. else:
  79. turtle.dot(5)
  80. Skip(-radius)
  81. turtle.right(6)
  82.  
  83. def Week(t):
  84. week = ["星期一", "星期二", "星期三",
  85. "星期四", "星期五", "星期六", "星期日"]
  86. return week[t.weekday()]
  87.  
  88. def Date(t):
  89. y = t.year
  90. m = t.month
  91. d = t.day
  92. return "%s-%d-%d" % (y, m, d)
  93.  
  94. def Tick():
  95. # 绘制表针的动态显示
  96. t = datetime.today()
  97. second = t.second + t.microsecond * 0.000001
  98. minute = t.minute + second / 60.0
  99. hour = t.hour + minute / 60.0
  100. secHand.setheading(6 * second)
  101. minHand.setheading(6 * minute)
  102. hurHand.setheading(30 * hour)
  103.  
  104. turtle.tracer(False)
  105.  
  106. printer.forward(65)
  107. printer.write(Week(t), align="center",
  108. font=("Courier", 14, "bold"))
  109. printer.back(130)
  110. printer.write(Date(t), align="center",
  111. font=("Courier", 14, "bold"))
  112. printer.home()
  113. turtle.tracer(True)
  114.  
  115. # 100ms后继续调用tick
  116. turtle.ontimer(Tick, 100)
  117.  
  118. def main():
  119. # 打开/关闭龟动画,并为更新图纸设置延迟。
  120. turtle.tracer(False)
  121. Init()
  122. SetupClock(160)
  123. turtle.tracer(True)
  124. Tick()
  125. turtle.mainloop()
  126.  
  127. if __name__ == "__main__":
  128. main()

结果:

绘制小猪佩奇:

 代码:

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

【Python】蟒蛇绘制的更多相关文章

  1. python蟒蛇绘制

    使用IDLE的文件方式编写代码并保存为PythonDraw.py文件 python蟒蛇绘制 import turtle引入了海龟绘图体系 使用setup函数,设定了一个宽650像素和高350像素的窗体 ...

  2. python蟒蛇绘制的代码以及目前还不知道怎么用的RGB颜色对照表

    #PythonDraw.py import turtle#引入海龟库 turtle.setup(650,350,200,200)#确定窗口大小,长650,高350,确定窗口位置,距离电脑左上角200, ...

  3. 011 实例2-Python蟒蛇绘制

    目录 一."Python蟒蛇绘制"问题分析 1.1 Python蟒蛇绘制 二."Python蟒蛇绘制"实例编写 三.运行效果 3.1 程序关键 四." ...

  4. Python学习之turtle库和蟒蛇绘制程序

    Python的函数库 Python语言与C语言Java类似,可以大量使用外部函数库包含在安装包中的函数库:. 比如math, random, turtle等其他函数库,其他函数库用户根据代码需求自行安 ...

  5. Python turtle绘制阴阳太极图代码解析

    本文详细分析如何使用Python turtle绘制阴阳太极图,先来分解这个图形,图片中有四种颜色,每条曲线上的箭头表示乌龟移动的方向,首先从中心画一个半圆(红线),以红线所示圆的直径作半径画一个校园, ...

  6. python+matplotlib 绘制等高线

    python+matplotlib 绘制等高线 步骤有七: 有一个m*n维的矩阵(data),其元素的值代表高度 构造两个向量:x(1*n)和y(1*m).这两个向量用来构造网格坐标矩阵(网格坐标矩阵 ...

  7. python学习2:turtle的使用蟒蛇绘制的学习以及自己摸索的等边三角形绘制(跟随mooc学习)

    首先先放上蟒蛇的绘制程序 import turtle#引入外部库#def保留字用于 定义函数 def drawSnake(rad,angle,len,neckrad): for i in range( ...

  8. Python入门习题2.蟒蛇绘制(turtle库)

    例2.调用turtle库中的若干函数来绘制蟒蛇,要求:(1)主体窗口宽650像素,高度350像素,窗口左侧与屏幕左侧像素距离200,窗口顶部与屏幕顶部像素距离200:(2)画笔落点在原点反向前进250 ...

  9. Python语言程序设计(3)--实例2-python蟒蛇绘制-turtle库

    1. 2. 3.了解turtle库 Turtle,也叫海龟渲染器,使用Turtle库画图也叫海龟作图.Turtle库是Python语言中一个很流行的绘制图像的函数库.海龟渲染器,和各种三维软件都有着良 ...

随机推荐

  1. ubuntu---禁止更新内核

    系统内核 4.15.0-29  更新成了 4.15.0-88,降级内核并禁止更新内核 查看已安装内核:dpkg --get-selections |grep linux-image 查看正在使用的内核 ...

  2. LIS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  3. 自己的系统重装之后,怎么去重新的装官方的office办公软件,详细教程

    1  访问官网地址--微软,并通过自己的微软账号进行登录,转到下面的界面 2   点击上图的菜单栏的offce菜单项,跳转到下图 3  点击  菜单栏的产品  之后选择  查看office的全部的历史 ...

  4. 02:QT的第一个程序

    新建项目,有这么几个文件: main.cpp                      //一个main函数,作为应用程序的入口函数 mainwindow.cpp mainwindow.h untit ...

  5. 如何使用Mbp模块构建应用.

    上一篇文章https://www.cnblogs.com/mbpframework/p/12073102.html,介绍了一下Mbp的框架.其实这个框架写出来主要是为了学习,当然也可以经过优化运用到实 ...

  6. base(根URL)

    指定用于一个文档中包含的所有相对 URL 的根 URL.一份中只能有一个 <base> 元素. 可以通过使用 document.baseURI 的 JS 脚本查询 属性 包含全局属性 hr ...

  7. spring框架中用到了哪些设计模式

    1.代理模式:在AOP和remoting中被用的比较多 2.单例模式:在spring配置文件中定义的bean默认为单例模式 3.模板方法模式:解决代码重复问题 4.前端控制器模式:spring提供了D ...

  8. spring整合websocket,如何在服务端依赖注入service

    1.在pom.xml文件中添加jar包: <properties> <spring.version>4.0.5.RELEASE</spring.version> & ...

  9. linux中Jenkins启动/重启/停止命令

    简要记录一下Linux 中Jenkins启动/重启/停止命令 启动service jenkins start1重启service jenkins restart1停止service jenkins s ...

  10. Spring-session+Redis解决Session共享

    1. 保证Redis启动           2. 导入依赖                SpringBoot+Spring-Session+Redis <!--spring boot 与re ...