turtle库笔记

1.Python语言是通用、脚本、开源、跨平台、多模型语言。

而Python绘图:turtle库的使用

—turtle库是turtle绘图体系的python实现

—turtle绘图体系:1969诞生,主要用于程序设计入门

—turtle库是pyrhon语言的标准库之一,是入门级的图形绘制函数库

2.Python计算生态=标准库+第三方库

—标准库:随解释器直接安装到操作系统中的功能模块

—第三方库:需要经过安装才能使用的功能模块

—模块:库Library、包Package、模块Module,统称模块

3.turtle绘图窗体布局:绘制turtle图形首先需要一个绘图窗体(在操作系统上表现为一个窗口),在窗口中使用的最小单位是像素

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

(1)setup()设置窗体大小及位置

(2)setup()中四个参数中后两个可选(宽度、高度是指窗体本身的宽度、高度),把后两个参数去掉,由于没有指定左上角在屏幕中的位置,系统默认该窗口在屏幕的正中心

(3)setup()不是必须要有的

4.turtle空间坐标体系(turtle窗体内部形成的一个空间坐标体系):包含绝对坐标和海龟坐标两种

(1)绝对坐标:正中心坐标(0,0),海龟运行方向向着画布的右侧,因此窗体右方向是x轴,上方向是y轴

绝对坐标系:

 

—坐标值:海龟绘制过程中它的画布中间的空间坐标体系,与窗体在屏幕中间的位置的坐标不同,(利用空间坐标改变海龟的行进位置)

—可利用一些函数改变turtle的行进位置:turtle,goto(x,y)

(2)海龟坐标:turtle空间坐标体系:

turtle。fd:指向海龟的正前方向运行

turtle.bk:向海龟的反方向运行

turtle,circle:以海龟当前位置左侧的某一点为圆心,进行曲线运行

这些函数可以改变海龟在空间坐标的位置,进而实现绘图功能

(3)turtle角度坐标体系:

turtle.seth(angle):

—turtle.seth():改变海龟的行进方向,只改变方向但不行进,不在图像中绘制任何信息

—angle为绝对度数

(4)海龟角度:

5.RGB色彩体系:由三种颜色构成万能色

—RGB指红蓝绿三个通道的颜色组合

—覆盖视力所能感知的所有颜色

—RGB每色取值范围0~255整数或0~1小数

6.turtle库是默认采用RGB的小数值表示颜色:

—turtle.colormode(mode):改变色彩数值使用:—1.0:RGB小数值模式;—255:RGB整数值模式

 

turtle库的使用小结:

—turtle库的海龟绘图法

—turtle.setup()调整绘图窗体在电脑屏幕中的布局

—画布上以中心为原点的空间坐标系:绝对&海龟坐标

—画布上以空间x轴为0度的角度坐标系:绝对&海龟坐标(以海龟当前位置和它的朝向为准设计)

—RGB色彩体系:色彩模式切换(相关RGB值可网上查询)

 

7.turtle程序语法元素分析:库引用:扩充Python程序功能的方式

(1)使用import保留字完成,采用<a>.<b>()编码风格

—import<库名>

—<库名>.<函数名>(<函数参数>)              (注:此方法不会出现函数重名的问题)

(2)使用from和import保留字共同完成

—from<库名>import<函数名>

—from<库名>import*

    <函数名>(函数参数>)  (此方法后再调用函数过程中,不需要再加库名. 的形式,直接使用函数名加函数参数完成引用)

(注:此方法会出现函数重名的问题,会使程序运行不一致)

(3)使用import和as保留字共同完成,形成新的库引用方法:

—import<库名>as<库别名>

   <库别名>.<函数名>(<函数参数>)

8.turtle画笔控制函数:画笔操作后一直有效,一般成对出现

—turtle.penup()   别名   turtle.pu() :抬起画笔,画笔或海龟运行轨迹不在画布形成图案

—turtle.pendown()  别名  turtle.pd()  :落下画笔

画笔设置后一直有效,直至下次重新设置

—turtle.pensize(width)  别名  turtle.width(width) :设置画笔宽度

—turtle.pencolor(color) :修改画笔颜色(字符串或RGB值赋予颜色)

(1)oencolor(color)的color参数可有三种形式:1)颜色字符串:turtle.pencolor("purple")  ,字符串形式而且要小写

                                                                         2)RGB的小数值:turtle.pencolor(0.63,0.13,0.94)

                                                                         3)RGB的元组数:turtle.prncolor((0.63,0.13,0.94))

(2)pencolor(colorstring)、pencolor(r,g,b)、pencolor((r,g,b))

9.turtle运动控制函数:在绘图上让海龟行动,控制海龟行进:走直线&走曲线

—turtle.forward(d)  别名  turtle.fd(d) :向前进,走直线

—d:行进距离,可为负数(倒退行进),具体行进距离单位是像素

—turtle.circle(r,extent=None) (r若为负数,圆心在右侧半径绝对值):根据半径r绘制extent角度的弧形,走曲线

—r:默认圆心在海龟左侧r距离的位置

—extent:绘制角度,默认为360度整圆

10.turtle方向控制函数:并不在绘图上让海龟行动,控制海龟面对方向:绝对&海龟角度

(1)绝对角度:turtle.setheading(angle)  别名  turtle.seth(angle) :改变行进方向

                —angle :改变行进方向

(2)海龟角度: turtle.left(angle)  、turtle.right(angle)

                    —angel:在海龟当前行进方向上旋转角度

11.循环语句:按一定次数循环:for <变量> in range(<参数>)

                                                        <被循环执行的语句>

—<变量>:表每次循环计数,从0到次数-1

—range()函数:产生循环计数序列

—range(N):产生0到N-1整数序列,共N个

—range(M,N):产生M到N-1......,共N-M个

 

import turtle as t
'''
t.pu()  提起画笔
t.pd()  移动时绘制图形,缺省时也为绘制
t.seth  设置当前朝向为angle角度
t.begin_fill()  准备开始填充图形
t.color      同时设置pencolor=color1, fillcolor=color2
t.goto       设置笔的坐标
t.circle(70,20) 半径 度数
15,124,215  乔治裤子颜色外面
66,163,242  乔治裤子颜色里面

134  196  247  天空的颜色
123,245,95 草地的颜色
253,6,6  鞋子外面
253,70,70  鞋子里面
130,119,100 泥坑


'''
r_a=0.8
wight = 1100
height = 700
#t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color("green")
t.setup(wight,height)
t.speed(10)
def move_pen(x,y):
    t.pu()
    t.goto(x-wight/2+50,y-height/2+50)
    t.pd()
def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):
    t.pensize(size)
    t.color((r1,g1,b1),(r2,g2,b2))
def draw_grid():
    pen_set(1,0,0,0,0,0,0)
    for i in range(20):
        move_pen(0+i*50,0)
        t.seth(90)
        t.fd(600)
    for i in range(12):
        move_pen(0,0+i*50)
        t.seth(0)
        t.fd(1000)

def draw_bg():
    #画草地
    
    move_pen(0,350)
    pen_set(4,123,245,95, 123,245,95)
    t.begin_fill()
    t.seth(-90)
    t.fd(350)
    t.seth(0)
    t.fd(1000)
    t.seth(90)
    t.fd(350)
    t.end_fill()
    #画天空
    move_pen(0,350)
    pen_set(4,134,196,247, 134,196,247)
    t.begin_fill()
    t.seth(90)
    t.fd(250)
    t.seth(0)
    t.fd(1000)
    t.seth(-90)
    t.fd(250)
    a=-180 + r_a
    for i in range(50):
        a = a - r_a/50
        t.seth(a)
        t.fd(500/50)
    a =180
    for i in range(50):
        a = a - r_a/50
        t.seth(a)
        t.fd(500/50)
    t.end_fill()
    
def draw_mud_pit():
    #画泥坑
    pen_set(5,130,119,100, 130,119,100)
    move_pen(350,150)
    t.begin_fill()
    t.seth(-180)
    t.circle(50,125)
    t.seth(-20)
    t.circle(350,60)
    t.seth(20)
    t.circle(50,30)
    t.seth(10)
    t.circle(50,30)
    t.seth(0)
    t.circle(50,30)
    t.seth(40)
    t.circle(50,90)
    t.seth(170)
    t.circle(500,45)
    t.end_fill()
    
def draw_shoes():
    pen_set(3,253,6,6, 253,70,70)
    move_pen(400,100)
    t.begin_fill()
    t.seth(0)
    t.fd(50)
    t.seth(87)
    t.fd(50)
    t.seth(180)
    t.fd(25)
    t.seth(-93)
    t.fd(20)
    t.seth(-180)
    t.fd(25)
    t.seth(-120)
    t.circle(45,38)
    t.end_fill()
    move_pen(470,100)
    t.begin_fill()
    t.seth(0)
    t.fd(50)
    t.seth(87)
    t.fd(50)
    t.seth(180)
    t.fd(25)
    t.seth(-93)
    t.fd(20)
    t.seth(-180)
    t.fd(25)
    t.seth(-120)
    t.circle(45,38)
    t.end_fill()
def draw_leg(): 
    pen_set(6,255,155,192, 255,155,192)
    move_pen(440,140)
    t.seth(90)
    t.fd(20)
    move_pen(510,140)
    t.seth(90)
    t.fd(20)
def draw_trousers():
    move_pen(400,300)
    pen_set(6,15,124,215, 66,163,242)
    t.begin_fill()
    a=-130 
    for i in range(60):
        a = a + 2
        t.seth(a)
        t.fd(3)
    for i in range(14):
        a = a + 0.02
        t.seth(a)
        t.fd(2)
    a = 0-a
    for i in range(14):
        a = a + 0.02
        t.seth(a)
        t.fd(2)
    for i in range(60):
        a = a + 2.2
        t.seth(a)
        t.fd(3)
    t.end_fill()
def draw_tile():
    move_pen(550,177)
    pen_set(6,255,155,192, 255,155,192)
    a=-60 
    for i in range(25):
        a = a + 4
        t.seth(a)
        t.fd(1)
    t.circle(5)
    a = -a
    for i in range(30):
        a = a + 4
        t.seth(a)
        t.fd(1)
def draw_hands():
    move_pen(550,250)
    pen_set(6,255,155,192, 255,155,192)
    t.seth(20)
    t.fd(70)
    move_pen(600,270)
    t.seth(60)
    t.fd(20)
    move_pen(600,270)
    t.seth(-20)
    t.fd(20)
    
    move_pen(380,250)
    t.seth(160)
    t.fd(50)
    move_pen(350,260)
    t.seth(100)
    t.fd(20)
    move_pen(350,260)
    t.seth(-140)
    t.fd(20)
    
def draw_face():
    move_pen(400,360)
    pen_set(4,255,155,192, 255,196,218)
    t.begin_fill()
    a=-120 
    for i in range(20):
        a = a + 2.5
        t.seth(a)
        t.fd(2.2)
    for i in range(130):
        a = a + 1.3
        t.seth(a)
        t.fd(1.8)
    for i in range(35):
        a = a + 1.4
        t.seth(a)
        t.fd(2)
    for i in range(50):
        a = a + 0.35
        t.seth(a)
        t.fd(2)
    for i in range(50):
        a = a + 0.2
        t.seth(a)
        t.fd(2)
    n=0.4
    for i in range(180):
        if 0<=i<30 or 60<=i<90 or 120<=i<150 :
            n=n+0.08
            t.lt(3) #向左转3度
            t.fd(n) #向前走a的步长
        else:
            n=n-0.08
            t.lt(3)
            t.fd(n)
    a=-50
    for i in range(20):
        a = a + 2.8
        t.seth(a)
        t.fd(5)
    t.end_fill()
def draw_other():
    move_pen(310,440)
    pen_set(6,255,145,192, 255,145,192)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    move_pen(330,430)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    
    pen_set(6,255,145,192, 255,255,255)
    move_pen(410,425)
    t.begin_fill()
    t.circle(10)
    t.end_fill()
    move_pen(460,395)
    t.begin_fill()
    t.circle(10)
    t.end_fill()
    
    pen_set(6,0,0,0,0,0,0)
    move_pen(405,429)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    move_pen(455,399)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    
    move_pen(510,310)
    pen_set(6,255,155,192, 255,155,192)
    t.begin_fill()
    t.circle(25)
    t.end_fill()
    
    move_pen(410,340)
    pen_set(6,255,145,192, 255,145,192)
    a=-80 
    for i in range(20):
        a = a + 6
        t.seth(a)
        t.fd(3)
    move_pen(430,445)
    pen_set(4,255,155,192, 255,196,218)
    t.begin_fill()
    a=120 
    for i in range(40):
        a = a - 2
        t.seth(a)
        t.fd(1.2)
    a=-a
    for i in range(45):
        a = a - 2
        t.seth(a)
        t.fd(1.2)
    t.end_fill()
    move_pen(480,430)
    t.begin_fill()
    a=70 
    for i in range(40):
        a = a -1.5
        t.seth(a)
        t.fd(1.5)
    a=-80
    for i in range(45):
        a = a -1.5
        t.seth(a)
        t.fd(1.5)
    t.end_fill()
draw_bg()
draw_mud_pit()
#draw_grid()
draw_leg()
draw_shoes()
draw_trousers()
draw_tile()
draw_hands()
draw_face()
draw_other()
t.mainloop()

  

 

posted @ 2020-03-14 16:05  可乐配牛奶  阅读(521)  评论(0编辑  收藏  举报