画布:

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

1)turtle.screensize(canvwidth, canvheight, bg):参数分别为画布的宽(单位像素), 高, 背景颜色

2)turtle.setup(width, height, startx, starty):width, height:

输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例。(startx, starty): 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。

画笔:

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

画笔属性:

1) turtle.pensize():设置画笔的宽度;

2)turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组。

3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。

绘制命令:

1)turtle.forward(distance)(别名:turtle.fd):向当前画笔方向移动distance像素长度。

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

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

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

5)turtle.pendown()(别名:turtle.pd(),turtle.down()):移动时绘制图形,缺省时也为绘制。

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

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

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

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

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

11)turtle.setheading(angle):设置当前朝向为angle角度。

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

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

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

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

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

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

18)turtle.end_fill():填充完成。

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

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

21)turtle.seth(to_angle)(别名:turtle.setheading(to_angle)):设置小海龟当前前进方向为to_angle,该角度是绝对方向的角度值。

实例

绘制小猪佩奇

# -*- coding:utf-8 -*-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()

python的turtle画图常用函数相关推荐

  1. Python中turtle库常用函数

    记录turtle库中经常用到的函数. turtle.forward(distance)  画笔向前移动distance距离 turtle.backforward(distance)  画笔向后移动di ...

  2. python :turtle画笔设置函数

    python :turtle画笔设置函数 turtle英⽂是"乌⻳的意思",当我们使⽤turtle时,想象⼀只⼩乌⻳在⼀个横轴为x.纵轴为y的坐标系原点(0,0),它爬过的路线形成 ...

  3. (numpy)python中Array的常用函数

    python中Array的常用函数 1.unique 2.sum 3.max 1.unique a = np.random.randint(10, size=20).reshape(4,5) a &g ...

  4. Python中random模块常用函数/方法(2)——random.random(),random.randint()和random.uniform()

    1.random.random():生成一个0到1的随机符点数: 0 <= n < 1.0 语法:random.random() #生成一个0~1之间的随机浮点数 print(" ...

  5. 1. python 字符串简介与常用函数

    1. python中的字符串简介与常用函数 在python中,字符串变成了一个强大的处理工具集,他是不可变的,也就是说字符串包含字符与字符的顺序,他不可以原处修改 字符串是我们后面需要学习的稍大一点的 ...

  6. 菜鸟教程python正则表达式_Python正则表达式常用函数菜鸟教程

    这篇文章主要为大家详细介绍了Python正则表达式常用函数菜鸟教程,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! Py ...

  7. python的应用领域和常用函数模块有哪些_Python模块导入区别与常用函数案例

    作者:Nature 出品:AI机器思维 Python中包含很多模块,每个领域的应用有关专家开发了相应的模块,必须将其导入到python中,然后才能使用.每个模块安装导入后才能引用,下面通过math模块 ...

  8. Python基础-字符串(字符串常用函数/操作/字符串遍历)

    字符串就是一串字符, 表示文本类型的数据, 可以用"一对双引号"或者'一对单引号'定义一个字符串, 字符串定义格式为 字符串变量名 = '字符串的文本内容' 常用函数/操作 获取字 ...

  9. Python基础-字典(字典常用函数/操作/字典遍历)

    字典是Python中除列表外,最灵活的数据类型,使用键值对存储数据,其中键必须是唯一的,值可以是任意类型 字典类型格式 student = {'键名1': 值1, '键名2': 值2, ...} # ...

最新文章

  1. java nifty_Java NiftyDialogBuilder類代碼示例
  2. mybatis返回map键值对_mybatis返回map结果集怎么配置
  3. python中变量作用域
  4. 第二次Soring冲刺计划第二天(个人)
  5. mysql 5.7 insert_MySQL5.7 支持一个表有多个INSERT/DELETE/UPDATE触发器
  6. 使用汉文博士检索汉字
  7. css3中transform-style的用法
  8. 【数据、软件共享】年鉴,夜间灯光,土地数据(永久更新)
  9. 抄答案就是了,两套详细的设计方案,解决头疼的支付掉单问题
  10. Gitlab的使用(内含Git命令大全)
  11. 国产宽带电力载波驱动芯片GS6212应用原理图(PIN TO PIN THS6212)
  12. DVD-R、DVD+R以及DVD-RW和DVD+RW的功能区别
  13. Circuit Design 三极管驱动蜂鸣器电路 及 蜂鸣器两端电压正确但是不响的解决方案
  14. vue中使用svg图片
  15. 深度学习笔记——情感分析
  16. 如何用好消息推送为app拉新、留存、促活
  17. 神经网络模型的工作原理,人脑神经网络模型
  18. 智旗:向小罐茶杜国楹学习如何做产品
  19. RHCSA 文件编辑 nano vi vim
  20. 为什么有些人已经那么接近成功,但是,最终却没有能够取得成功?

热门文章

  1. java之十四 网络连接
  2. Qt小程序——运用 open cv 进行视频人脸检测
  3. andriod 监听系统截屏操作
  4. 字符、字节、位等之间的转换(自我收集,欢迎指教)
  5. 【Unity3D】利用Touch类编写手游触控操作演示
  6. 低功耗验证 (一) CMOS基础,功耗分类,低功耗方法
  7. Linux之设置防火墙、开放端口
  8. web手势库AlloyFinger运用( 控制CANVAS中图片移动、缩放、旋转) - 可编辑图片指定区域位置
  9. 互联网产品——没有个性
  10. java go多线程:两个线程交替打印 0~100 的奇偶数