目录

  • Python学习笔记六——画小猪佩奇
    • 画布
    • 画笔
      • 属性设置
      • 操纵命令
        • 运动命令
        • 画笔控制命令
        • 全局控制命令
        • 其他命令

Python学习笔记六——画小猪佩奇

使用Python的turtle库可以绘制多种图形,先设置画布、画笔等公共参数,然后分部分绘制图形,小猪佩奇的效果图:

示例代码(摘自公众号:数据分析联盟,更多代码参见http://isteps.cn/docs/python/turtle/examples.php):

from turtle import*def nose(x,y):#鼻子 penup()#提起笔goto(x,y)#定位pendown()#落笔,开始画setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)begin_fill()#准备开始填充图形a=0.4for i in range(120):if 0<=i<30 or 60<=i<90:a=a+0.08left(3) #向左转3度forward(a) #向前走a的步长else:a=a-0.08left(3)forward(a)end_fill()#填充完成penup()setheading(90)forward(25)setheading(0)forward(10)pendown()pencolor(255,155,192)#画笔颜色setheading(10)begin_fill()circle(5)color(160,82,45)#返回或设置pencolor和fillcolorend_fill()penup()setheading(0)forward(20)pendown()pencolor(255,155,192)setheading(10)begin_fill()circle(5)color(160,82,45)end_fill()def head(x,y):#头  color((255,155,192),"pink")penup()goto(x,y)setheading(0)pendown()begin_fill()setheading(180)circle(300,-30)circle(100,-60)circle(80,-100)circle(150,-20)circle(60,-95)setheading(161)circle(-300,15)penup()goto(-100,100)pendown()setheading(-30)a=0.4for i in range(60):if 0<=i<30 or 60<=i<90:a=a+0.08lt(3) #向左转3度fd(a) #向前走a的步长else:a=a-0.08lt(3)fd(a)end_fill()def ears(x,y): #耳朵color((255,155,192),"pink")penup()goto(x,y)pendown()begin_fill()setheading(100)circle(-50,50)circle(-10,120)circle(-50,54)end_fill()penup()setheading(90)forward(-12)setheading(0)forward(30)pendown()begin_fill()setheading(100)circle(-50,50)circle(-10,120)circle(-50,56)end_fill()def eyes(x,y):#眼睛 公众号 数据分析联盟color((255,155,192),"white")penup()setheading(90)forward(-20)setheading(0)forward(-95)pendown()begin_fill()circle(15)end_fill()color("black")penup()setheading(90)forward(12)setheading(0)forward(-3)pendown()begin_fill()circle(3)end_fill()color((255,155,192),"white")penup()seth(90)forward(-25)seth(0)forward(40)pendown()begin_fill()circle(15)end_fill()color("black")penup()setheading(90)forward(12)setheading(0)forward(-3)pendown()begin_fill()circle(3)end_fill()def cheek(x,y):#腮 公众号 数据分析联盟color((255,155,192))penup()goto(x,y)pendown()setheading(0)begin_fill()circle(30)end_fill()def mouth(x,y): #嘴 公众号 数据分析联盟color(239,69,19)penup()goto(x,y)pendown()setheading(-80)circle(30,40)circle(40,80)def body(x,y):#身体 公众号 数据分析联盟color("red",(255,99,71))penup()goto(x,y)pendown()begin_fill()setheading(-130)circle(100,10)circle(300,30)setheading(0)forward(230)setheading(90)circle(300,30)circle(100,3)color((255,155,192),(255,100,100))setheading(-135)circle(-80,63)circle(-150,24)end_fill()def hands(x,y):#手 公众号 数据分析联盟color((255,155,192))penup()goto(x,y)pendown()setheading(-160)circle(300,15)penup()setheading(90)forward(15)setheading(0)forward(0)pendown()setheading(-10)circle(-20,90)penup()setheading(90)forward(30)setheading(0)forward(237)pendown()setheading(-20)circle(-300,15)penup()setheading(90)forward(20)setheading(0)forward(0)pendown()setheading(-170)circle(20,90)def foot(x,y):#脚 公众号 数据分析联盟pensize(10)color((240,128,128))penup()goto(x,y)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)fd(20)pensize(10)color((240,128,128))penup()setheading(90)forward(40)setheading(0)forward(90)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)fd(20)def tail(x,y):#尾巴 公众号 数据分析联盟pensize(4)color((255,155,192))penup()goto(x,y)pendown()seth(0)circle(70,20)circle(10,330)circle(70,30)def setting():        #参数设置pensize(4)        #设置画笔的大小hideturtle()        #使乌龟无形(隐藏)colormode(255)        #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内color((255,155,192),"pink")setup(840,500)speed(10)def main():setting()           #画布、画笔设置nose(-100,100)      #鼻子head(-69,167)       #头ears(0,160)         #耳朵eyes(0,140)         #眼睛cheek(80,10)        #腮mouth(-20,30)       #嘴body(-32,-8)        #身体hands(-56,-45)      #手foot(2,-177)        #脚tail(148,-155)      #尾巴done()if __name__ == '__main__':main()

画布

设置画布大小
(1).turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素),高,背景颜色。
(2).turtle.setup(width=0.5, height=0.75, startx=None, starty=None)width, height:输入宽和高为整数时,表示像素;为小数时,表示占据电脑屏幕的比例,(startx, starty):这一坐标表示矩形窗口左上角顶点的位置,如果为空,则窗口位于屏幕中心。

画笔

属性设置

使用坐标原点位置、x轴正方向描述画笔的状态。

设置属性 方法描述
turtle.pensize() 设置画笔的宽度
turtle.pencolor() 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组。
turtle.speed(speed) 设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。

操纵命令

运动命令

命令 说明
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(radius, extent=None, steps=None) 做半径为radius的圆的内切正多边形,多边形边数为stepsradius半径为正(负),表示圆心在画笔的左边(右边)画圆;extent为弧度
setx( ) 将当前x轴移动到指定位置
sety( ) 将当前y轴移动到指定位置
setheading(angle) 设置当前朝向为angle角度
home() 设置当前画笔位置为原点,朝向东
dot(r) 绘制一个指定直径和颜色的圆点

画笔控制命令

命令 说明
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1, color2) 同时设置pencolor=color1fillcolor=color2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏画笔的turtle形状
turtle.showturtle() 显示画笔的turtle形状

全局控制命令

命令 说明
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参数也是可选项

其他命令

命令 说明
turtle.mainloop()或turtle.done() 启动事件循环 -调用Tkintermainloop函数。必须是乌龟图形程序中的最后一个语句。
turtle.mode(mode=None) 设置乌龟模式(“standard”,“logo”或“world”)并执行重置。如果没有给出模式,则返回当前模式。standard模式:初始龟标题为“向右(东)”,正角度为“逆时针”;logo模式:初始龟标题为“向上(北)”,正角度为“顺时针”。
turtle.delay(delay=None) 设置或返回以毫秒为单位的绘图延迟。
turtle.begin_poly() 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
turtle.end_poly() 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
turtle.get_poly() 返回最后记录的多边形。

Python学习笔记六——画小猪佩奇相关推荐

  1. Python学习笔记(六)

    1. IO编程 1.1 文件读写 1.2 StringIO和BytesIO 1.3 操作文件和目录 1.4 序列化 2. 进程和线程 2.1 多进程 2.2 多线程 2.3 ThreadLocal 2 ...

  2. python学习笔记六 初识面向对象上(基础篇)

    python面向对象 面向对象编程(Object-Oriented Programming )介绍 对于编程语言的初学者来讲,OOP不是一个很容易理解的编程方式,虽然大家都知道OOP的三大特性是继承. ...

  3. 【懒懒的Python学习笔记六】

    大多数程序旨在最终解决用户问题,为此通常需要从用户那里输入信息,在本章学习中,我们将学习Python的raw_input()函数接受用户输入以及学习while循环来控制程序的运行. 函数raw_inp ...

  4. Python学习笔记六(Python time模块)

    通过python time模块提供的函数和方法可以获取与时间相关的操作,例如:获取系统时间,统计程序执行时间,WEB程序的响应时间等. 1)time.time(),该函数返回从1970年1月1日0点0 ...

  5. Python学习笔记(六)Python基础_数据类型——字符串

    文章目录 字符串 字符串输入 字符串拼接 字符串操作 字符串格式化 访问字符串中的值 字符串切片 字符串遍历 常用的字符串方法 字符串 字符串是 Python 中最常用的数据类型:一般以使用引号' ' ...

  6. python学习笔记六:Series

    Series是一种类似于一维数组的对象,由以下两部分组成: values:一组数据(ndarray或list类型) index:相关的数据索引标签 一维数组默认索引为0,1,2,3...,而Serie ...

  7. Python学习笔记(六)—几个标准类型内建函数

    比如说cmp(),str(),type(),repr()等等 cmp(a,b)比较a和b,根据比较的结果返回一个整数i. 当a<b时返回i<0;(即-1) 当a>b时返回i>0 ...

  8. 用Python的turtle模块画小猪佩奇

    # coding:utf-8 from turtle import*def nose(x,y):#鼻子pu()goto(x,y)pd()seth(-30)begin_fill()a=0.4for i ...

  9. Python学习笔记总结

    了解了python语言后,决定以后使用python来进行各项工作,因此一直想要深入学习python.千里之行始于足下,万事开头难. 由于最近在准备写毕业论文,陆陆续续学习了Python的语法知识. P ...

最新文章

  1. python 内部函数,以及lambda,filter,map等内置函数
  2. dubbo学习之dubbo管理控制台装配及集成zookeeper集群部署(1)【转】
  3. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
  4. hdu-1195--Open the Lock(BFS)
  5. Cachefiled
  6. 服务器oracle优化,oracle服务器配置及优化
  7. 编译Nginx提示gzip module requires the zlib library
  8. html演示 用鼠标画记号,html怎么用鼠标画出一条直线,鼠标移动时候要能看到线条...
  9. C程序100例 第二题
  10. 通过Android上的意图启动Google地图路线
  11. 数理统计基本原理复习
  12. qt控制程序打开记事本_Qt 记事本程序
  13. 全文专利 PDF 免费下载
  14. 人工智能和机器视觉技术学习培训设备
  15. 新浪微博正在审核的开发者平台应用添加测试账号
  16. python批量注释快捷键wingide_WingIDE 常用快捷键
  17. 23是android版本几,关于sdk=23的android版本权限的问题
  18. 私钥记录-支付宝接入
  19. 做跨境电商,用信用卡通道还是Paypal收款比较便宜?
  20. 【hadoop生态之Hive】Hive的DDL数据定义语言【笔记+代码】

热门文章

  1. mysql 5.7.24-winx64_mysql-5.7.24-winx64下载与安装
  2. 多变量微积分笔记1——偏导数
  3. 手机epub阅读软件
  4. Got a packet bigger than 'max_allowed_packet' bytes 问题的解决方法
  5. 《男人装》2006.05
  6. 【转】本人常用资源整理
  7. 如何用AVS Video Editor给视频添加文字?
  8. 【技术邻】基于有限元方法的整车风噪仿真分析
  9. tplmap-模板注入工具
  10. python 实现的 成语接龙