turtle库

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

1.画布的大小

画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置。设置画布大小。常用的画布方法有两个:screensize()和setup()。

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

2.画笔的设置

在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。
画笔(画笔的属性,颜色、画线的宽度等)

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

操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。

(1)画笔运动命令

(2)画笔控制命令

(3)全局控制命令

(4)其他命令

3.画小猪佩奇

-- coding: utf-8 --

“”"
Created on Tue Feb 26 15:58:31 2019

@author: lhh
“”"

from turtle import*def backgrand():screensize(canvwidth=None, canvheight=None, bg='white')     #自己加的背景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():backgrand()     #自己加的背景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()

参考链接

https://blog.csdn.net/weixin_41137248/article/details/81188801
https://blog.csdn.net/zengxiantao1994/article/details/76588580
https://www.cnblogs.com/findsoon/articles/9359021.html
https://www.cnblogs.com/yudanqu/p/8683794.html

05.python学习系列——画图库turtle(啥是小猪佩奇)相关推荐

  1. python turtle画熊-Python使用turtle库绘制小猪佩奇(实例代码)

    turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制.turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言. turtle库绘制图形有一个基本框架:一个小海龟 ...

  2. python画棒棒糖程序_Python使用turtle库绘制小猪佩奇(实例代码)

    turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制.turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言. turtle库绘制图形有一个基本框架:一个小海龟 ...

  3. Python学习系列(六)(模块)

    Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...

  4. Python学习系列(五)(文件操作及其字典)

    Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件      在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...

  5. Python: 学习系列之七:模块、PIPY及Anaconda

    系列 Python: 学习系列之一:Python能做什么 Python: 学习系列之二:基础介绍(int/float/string/range/list/tuple/dict/set) Python: ...

  6. python turtle 绘图小猪佩奇,Python使用turtle库绘制小猪佩奇(实例代码)

    这篇文章主要介绍了Python使用turtle库绘制小猪佩奇,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 turtle(海龟)是Python重要的标准库之一,它 ...

  7. python画笑脸-python 学习笔记——学会用turtle画笑脸

    Life is short, you need Python 人生苦短,我用Python -- Bruce Eckel 上课听了老师说的一句话,至此难以忘怀,很是受用.老师是知识的传授者,这话果然没毛 ...

  8. pythonturtle画图库使用技巧_Python画图库turtle使用方法简介

    Python图库turtle绘制爱心 先给出代码 import turtle as x x.penup() x.seth(90) x.fd(200) x.pendown() x.pencolor(&q ...

  9. Python学习系列(一)(基础入门)

    Python入门 本系列为Python学习相关笔记整理所得,IT人,多学无害,多多探索,激发学习兴趣,开拓思维,不求高大上,只求懂点皮毛,作为知识储备,不至于落后太远.如果兴趣学习者,推荐一个基础视频 ...

最新文章

  1. CSS3透明属性opacity
  2. 学好python可以做什么兼职-自学Python能干些什么副业
  3. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
  4. Matlab C混合编程
  5. 优秀的java代码_像这样写,Java菜鸟也能写出牛逼的代码
  6. 7.6 yum更换国内源 7.7 yum下载rpm包 7.8/7.9 源码包安装
  7. Java生产力提示:社区的热门选择
  8. 在linux centos7上安装git
  9. 如果手里有20万是放银行吃利息还是投资比较合适?
  10. 微软希望通过监控开发者结束软件 bug
  11. 博文视点大讲堂35期《Google Android创赢路线与产品开发实战》读者见面会
  12. 自动驾驶 10-1: 3D 几何和参考系3D Geometry and Reference Frames
  13. linux下c语言队列,C语言队列的实现
  14. qt button clicked(bool) always false
  15. 群晖 kodi mysql,用群晖为 Kodi 注入多设备同步能力
  16. 人的思维谬误与心理学效应
  17. URP无法使用 RenderType:Overlap
  18. 工业环境中的LED照明降低成本,提高安全性
  19. [CentOS Python系列] 五.阿里云部署web环境及通过IP地址访问服务器网页
  20. vue组件强制刷新的方案

热门文章

  1. java for循环延迟_Java 锁粗化与循环问题
  2. mysql5.7.19winx64安装_mysql5.7.19winx64安装配置方法图文教程(win10)
  3. 刚入门的自媒体人,也能给视频配上好听的声音
  4. ue4学习日记4(植被,光照,光束遮挡,天空球)
  5. C语言(二级基础知识2)
  6. 小程序input的type属性 text、number、idcard、digit
  7. 劳动合同法电子版(2021年的合同书,应该是目前最新版)
  8. 如何在AD中批量创建域用户
  9. Zeromq 学习笔记1
  10. 我的学厨记——香煎鸡翅