python---turtle库(详解)

https://blog.csdn.net/zengxiantao1994/article/details/76588580(部分引用来源)

初识turtle库(python内置库)

  • turtle库的使用:from turtle import * 或者 import turtle、前者使用方法不需要加 turtle,后者需要加turtle
例如:
'''
from turtle import *

circle(60)
done()
'''
import turtle

turtle.circle(60)
turtle.done()#绘图完,不自动结束

turtle库基本方法

画布(canvas)

画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置。

  1. 设置画布大小

第一种方法

turtle.screensize(canvwidth=None,canvheight=None,bg=None)
canvwidth -- 正整型数,以像素表示画布的新宽度值

canvheight -- 正整型数,以像素表示画面的新高度值

bg -- 颜色字符串或颜色元组,新的背景颜色

例如:turtle.screensize(800,600, "green")
turtle.screensize() #返回默认大小(400, 300)

第二种方法

turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
width -- 如为一个整型数值,表示大小为多少像素,如为一个浮点数值,则表示屏幕的占比;默认为屏幕的 50%

height -- 如为一个整型数值,表示高度为多少像素,如为一个浮点数值,则表示屏幕的占比;默认为屏幕的 75%

startx -- 如为正值,表示初始位置距离屏幕左边缘多少像素,负值表示距离右边缘,None 表示窗口水平居中

starty -- 如为正值,表示初始位置距离屏幕上边缘多少像素,负值表示距离下边缘,None 表示窗口垂直居中

例如:
import turtle

turtle.setup(500,500)
done()

画笔属性

  • 画笔
    在画布上,默认有一个坐标原点为画布中心的坐标轴,坐标原点上有一只面朝x轴正方向小乌龟。这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中,就是使用位置方向描述小乌龟(画笔)的状态。
turtle.pensize(width=None)表示小乌龟运动轨迹的宽度(线条粗细) 
turtle.pencolor(*args)函数表示小乌龟运动轨迹的颜色

<<<允许以下四种输入格式:
pencolor()
返回以颜色描述字符串或元组 (见示例) 表示的当前画笔颜色
pencolor(colorstring)
设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串,例如 "red""yellow""#33cc8c"。
pencolor((r, g, b))
设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围
pencolor(r, g, b)
设置画笔颜色为以 r, g, b 表示的 RGB 颜色。

turtle.speed(speed=None)速度值从 110,画线和海龟转向的动画效果逐级加快。
"fastest": 0 最快
"fast": 10"normal": 6 正常
"slow": 3"slowest": 1 最慢
1.移动和绘制
命令说明
turtle.forward(distance) 或 turtle.fd(distance)向当前画笔方向移动distance像素长度
turtle.backward(distance) 或 turtle.bk(distance) 或 turtle.back(distance)向当前画笔相反方向移动distance像素长度
turtle.right(degree) 或 turtle.rt(degree)顺时针移动degree°
turtle.left(degree) 或 turtle.lt(degree)逆时针移动degree°
turtle.goto(x,y) 或 turtle.setpos(x,y) 或 turtle.setposition(x,y)将画笔移动到坐标为x,y的位置
turtle.setx()将当前x轴移动到指定位置
turtle.sety()将当前y轴移动到指定位置
turtle.setheading(angle) 或 turtle.seth(angle)设置当前朝向为angle角度
turtle.home()返回原点,朝向东。
turtle.circle()画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
turtle.undo()撤销
turtle.speed()速度
turtle.dot(size=None, *color)绘制一个指定直径和颜色的圆点

注意:
turtle.left(-90) 相当于 turtle.right(90)
turtle.seth(angle)表示小乌龟启动时运动的方向。 是角度值。其中,0表示向东,90度向北,180度向西,270度向南;负值表示相反方向。
turtle.circle(radius, extent=None, steps=None)
radius – 一个数值:半径为正(负),表示圆心在画笔的左边(右边)画圆;
extent – 一个数值 (或 None):extent 为一个夹角,用来决定绘制圆的一部分。
steps – 一个整型数 (或 None):做半径为radius的圆的内切正多边形,多边形边数为steps

2.画笔控制
命令说明
turtle.pendown() 或 turtle.pd() 或 turtle.down()画笔落下
turtle.penup() 或 turtle.pu() 或 turtle.up()画笔抬起
turtle.pensize() 或 turtle.width()画笔粗细
turtle.pen(pen=None, **pendict)画笔
turtle.isdown()画笔是否落下(返回True或False)
turtle.color(color1, color2)返回或设置画笔颜色color1和填充颜色color2。
turtle.pencolor()画笔颜色
turtle.fillcolor()填充颜色
turtle.filling()返回当前是否在填充状态
turtle.begin_fill()准备开始填充图形
turtle.end_fill()结束填充
turtle.write()书写

注意:
turtle.pen(pen=None, **pendict):返回或设置画笔的属性,以一个包含以下键值对的 “画笔字典” 表示:“shown”: True/False、“pendown”: True/False、“pencolor”: 颜色字符串或颜色元组、“fillcolor”: 颜色字符串或颜色元组、“pensize”: 正数值、“speed”: 0…10 范围内的数值、“resizemode”: “auto” 或 “user” 或 “noresize”、“stretchfactor”: (正数值, 正数值)、“outline”: 正数值、“tilt”: 数值

此字典可作为后续调用 pen() 时的参数,以恢复之前的画笔状态。另外还可将这些属性作为关键词参数提交。使用此方式可以用一条语句设置画笔的多个属性。

turtle.write(arg, move=False, align=“left”, font=(“Arial”, 8, “normal”))

arg --: 要书写的文本内容

move --: True/False

align --: 字符串 “left”, “center” 或 “right”

font --: 一个三元组 (fontname, fontsize, fonttype)

3.其他命令
命令说明
turtle.showturtle() 或 st()显示画笔的turtle形状
turtle.hideturtle() 或 ht()隐藏画笔的turtle形状
turtle.isvisible()是否隐藏画笔的海龟形状
turtle.shape(name=None)设置海龟形状为 name 指定的形状名,如未指定形状名则返回当前的形状名
turtle.done()运行结束但不退出,必须是乌龟图形程序中的最后一个语句。
turtle.delay(delay=None)设置或返回以毫秒为单位的绘图延迟。

应用实例

彩色python(蟒蛇)

在这里插入图片描述

import turtle
turtle.setup(650, 350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
colors=['green','blue','yellow','orange','pink','purple']
turtle.seth(-40)
for i in range(4):
    turtle.color(colors[i])#选择索引从0~3的颜色
    turtle.circle(40, 80)#上半弧度
    turtle.circle(-40, 80)#下半弧度
turtle.color(colors[5])
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)

turtle.done()
# 改写 加入函数   画python
import turtle
def drawSnake(radius, angle, length):
    turtle.seth(-40)
    for i in range(length):
        turtle.circle(radius, angle)
        turtle.circle(-radius, angle)
    turtle.circle(radius, angle/2)
    turtle.fd(40)
    turtle.circle(16, 180)
    turtle.fd(40* 2/3)
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
drawSnake(40, 80, 4)
turtle.done()

多等边三角形

在这里插入图片描述

from turtle import *

pensize(3)
color('black')
fd(200)
seth(-120)  #根据二维  就是x,y轴坐标系(第三象限-120°)来显示
fd(200)
seth(120)   #第二象限,120°
fd(200)
color('red')
seth(60)    #第一象限,60°
fd(200)
seth(-60)   #第四象限,-60°
fd(400)
seth(180)  #转一个平角
color('blue')
fd(400)
seth(60)    #第一象限,60°
fd(200)
done()

无角正方形

在这里插入图片描述

from turtle import *
pensize(2)
pencolor('black')
for i in range(4):
    seth(90*i)
    penup()
    fd(20)
    pendown()
    fd(200)
    penup()
    fd(20)
    pendown()
hideturtle()#隐藏画笔
done()

六角星

在这里插入图片描述

from turtle import *
def triangle():
    pensize(2)
    pencolor('black')
    for i in range(3):
        fd(60)
        right(120)

def main():
    colors=['green','red','yellow','pink','purple','orange']
    speed(7)
    for i in range(6):
        begin_fill()
        fillcolor(colors[i])
        triangle()
        fd(60)  #以坐标系为基准,左转60°
        left(60)
        end_fill()
    #填充中心颜色
    fillcolor("blue")
    begin_fill()
    for i in range(6):
        fd(60)
        left(60)
    end_fill()
    ht()#隐藏画笔
main()
done()

你猜

在这里插入图片描述

from turtle import *
def triangle():
    pensize(2)
    pencolor('black')
    for i in range(3):
        fd(160)
        right(120)

def main():
    penup()
    setpos(-100,140)#画笔移动到一个绝对坐标。(开始默认画笔居中)
    pendown()
    speed(5)
    colors=['green','red','pink','purple','blue','yellow','orange']
    for i in range(6):
        fillcolor(colors[i])
        begin_fill()
        triangle()
        fd(160)
        right(60)
        end_fill()
    ht()
main()
done()

风车(为什么不会转呢?)

在这里插入图片描述

from turtle import *
def triangle(x):
    pensize(2)
    pencolor('black')
    for i in range(3):
        fd(x)
        right(120)

def main():
    speed(8)
    colors=['green','red','yellow','blue']
    for j in range(1,7):
        for i in range(4):
            if j >= 2:
                triangle(160 + j * 10)
                left(90)
            else:
                fillcolor(colors[i])
                begin_fill()
                triangle(160)
                left(90)
                end_fill()
    ht()
main()
done()

龙卷风

在这里插入图片描述

from turtle import *
# 龙卷风
setup(500,700)
pensize(1)
speed(6)
colors=['green','red','yellow','grey','orange','blue','pink','purple']
bgcolor('black')
for i in range(1,10):
    pencolor(colors[i%8])
    penup()
    goto(5*i,0)
    pendown()
    circle(10*i)
done()

同心圆(箭靶)

在这里插入图片描述

# 同心圆
from turtle import *
setup(600, 500, 450, 50)
pensize(5)
bgcolor('white')
speed('fastest')
colors = ['blue', 'yellow', 'red', 'green', 'white', 'black', 'orange', 'grey']
for i in range(10, 1, -1):
    penup()
    goto(0, -20 * i)
    pendown()
    begin_fill()
    fillcolor(colors[i % 4])
    circle(20 * i)
    end_fill()
hideturtle()
done()

五环

在这里插入图片描述

from turtle import *

setup(500, 500, 0, 0)
penup()
backward(50)
pendown()
# 先往后移动75像素,不显示直线
penup()
setx(-75)
pendown()

pensize(5)
pencolor('red')
circle(50)

penup()
fd(110)
pendown()

pencolor('black')
circle(50)

penup()
fd(110)
pendown()

pencolor('blue')
circle(50)

penup()
goto(-20, -50)
pendown()

pencolor('yellow')
circle(50)
penup()
fd(110)
pendown()

pencolor('green')
circle(50)
hideturtle()  # 隐藏画笔

done()

海绵宝宝

在这里插入图片描述

from turtle import *

#大致外观
setup(600,600)
pensize(3)
speed(20)
color('black','yellow')
penup()
goto(-120,120)
pendown()
begin_fill()
seth(-45) #Turtle中的turtle.seth(angle)函数表示小乌龟启动时运动的方向
for i in range(0,4):
    for i in range(0,5):
        circle(15,90)
        circle(-15,90)
    right(90)
end_fill()

#眼睛
pensize(1)
color('black','white')
penup()
goto(-70,30)
pendown()
begin_fill()
circle(30)
end_fill()

#1眼睛
color('white','blue')
penup()
goto(-40,43)
pendown()
begin_fill()
circle(10)
end_fill()

#2眼睛
color('white','black')
penup()
goto(-35,46)
pendown()
begin_fill()
circle(5)
end_fill()

#左眉毛1
pensize(3)
color('black')
penup()
goto(-68,82)
pendown()
fd(6)

#左眉毛2
pensize(3)
color('black')
penup()
goto(-50,88)
pendown()
right(45)
fd(6)

#左眉毛3
pensize(3)
color('black')
penup()
goto(-30,82)
pendown()
right(30)
fd(6)

#右眼睛
pensize(1)
color('black','white')
penup()
goto(3,67)
pendown()
begin_fill()
circle(30)
end_fill()

#右1眼睛
color('white','blue')
penup()
goto(3,56)
pendown()
begin_fill()
circle(10)
end_fill()

#右2眼睛
color('white','black')
penup()
goto(7,54)
pendown()
begin_fill()
circle(5)
end_fill()

#右眉毛1
pensize(3)
color('black')
penup()
goto(10,82)
pendown()
left(50)
fd(6)

#右眉毛2
pensize(3)
color('black')
penup()
goto(30,88)
pendown()
right(20)
fd(6)

#右眉毛3
pensize(3)
color('black')
penup()
goto(50,82)
pendown()
right(15)
fd(6)

#嘴巴
pensize(1)
color('black','IndianRed3')
penup()
goto(-86,-20)
pendown()
left(65)
begin_fill()
right(33)
circle(70,150)
# circle(120,80)
fd(10)
left(130)
circle(-184,45)
end_fill()

#舌头
color('black', 'orange red')
penup()
goto(-66,-50)
pendown()
right(130)
circle(-140,46)

#鼻子
penup()
goto(-15,10)
pendown()
fd(40)
circle(5,180)
fd(40)

#裤子
color('black','white')
penup()
goto(-123,-85)
pendown()
begin_fill()
left(100)
fd(20)

left(93)
fd(225)

left(95)
fd(20)

left(85)
fd(225)
end_fill()

#二层裤子
color('black','orange4')
penup()
goto(-123,-106)
pendown()
begin_fill()
left(90)
fd(30)
left(90)
fd(225)
left(90)
fd(30)
left(90)
fd(225)
end_fill()

#裤缝
color('black')
pensize(8)
for i in range(0,4):
    penup()
    goto(-83+(i*60),-120)
    pendown()
    fd(30)

#裤腿1
color('black','orange3')
pensize(1)
penup()
goto(-63,-137)
pendown()
begin_fill()
left(90)
fd(10)
left(30)
circle(15,120)
left(30)
fd(10)
left(90)
fd(30)
end_fill()


#裤腿2
color('black','orange3')
pensize(1)
penup()
goto(23,-137)
pendown()
begin_fill()
left(90)
fd(10)
left(30)
circle(15,120)
left(30)
fd(10)
left(90)
fd(30)
end_fill()

#腿1
color('black')
penup()
goto(-53,-155)
pendown()
left(90)
fd(65)
fd(30)
left(90)
fd(20)
penup()
goto(-43,-155)
pendown()
right(90)
fd(60)
circle(20,90)
fillcolor('black')
begin_fill()
fd(5)
circle(-7,180)
fd(35)
right(90)
fd(24)
end_fill()

#腿2
color('black')
penup()
goto(33,-152)
pendown()
right(180)
fd(55)
fd(30)
left(90)
fd(20)
penup()
goto(43,-152)
pendown()
right(90)
fd(65)
circle(20,90)
fillcolor('black')
begin_fill()
circle(-7,180)
fd(32)
right(90)
fd(22)
end_fill()

#左手
color('black','yellow')
penup()
goto(-120,-10)
pendown()
begin_fill()
left(45)
fd(100)
circle(-15,80)
fd(5)
circle(5,180)
fd(20)
#第2个手指
right(150)
fd(35)
circle(5,180)
fd(40)

#第3个手指
right(150)
fd(30)
circle(5,180)
fd(30)

#第4个手指
right(140)
fd(30)
circle(5,180)
fd(45)
circle(15,90)

right(40)
right(70)
fd(110)
end_fill()

#右手
color('black','yellow')
penup()
goto(90,-10)
pendown()
begin_fill()
left(90)
fd(100)
circle(-15,80)
fd(5)
circle(5,180)
fd(20)
#第2个手指
right(150)
fd(35)
circle(5,180)
fd(40)

# #第3个手指
right(150)
fd(30)
circle(5,180)
fd(30)

# #第4个手指
right(140)
fd(30)
circle(5,180)
fd(45)
circle(15,90)

right(40)
right(70)
fd(98)
end_fill()

penup()
goto(-120,160)
pendown()
color("red")
write("你好呀!", font=('Arial', 40, 'normal'))
hideturtle()
done()
#import ... 使用中函数需要 .函数 不会出现函数重名问题 #from .... import* 则可以直接使用函数名 #import .... as ....使代码量更少且不容易重名 import turtle #设置窗体大小和位置,4个参数中后两个可选为左上角坐标,setup函数不是必须的 turtle.setup(650,350,200,200) #绝对坐标 turtle.goto(x,y)海龟一开始在画面的中心(0,0),到达(x,y) #相对坐标 海龟当前运行的方向是前进方向 turtle.fd(d) 或turtle.forward(d) #后方是后退方向 turtle.bk(d) #左侧是左侧方向 turtle.circle(r,angle)以左侧的某个点为圆心向左侧 #右侧是右侧方向 # 画笔控制函数pen... # 一般成对出现: turtle.penup() 别名 turtle.pu() 不画 # turtle.pendown() turtle.pd() 画 # #画笔宽度设置后一直有效 turtle.pensize(width) 或 turtle.width(width) #画笔颜色 turtle.pencolor("purple")或 turtle.pencolor(0.63,0.13,0.94) 或 turtle.pencolor((0.63,0.13,0.94)) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("purple") #绝对角度 turtle角度坐标体系,类似数学平面直角坐标系,turtle.seth(angle)改变海龟行进角度,但不行进 或turtle.setheading() #相对角度 turtle.left(angle)向左改变角度 turtle.right(angle)向右改变角度 turtle.seth(-40) #turtle.circle(r,extent=None)绘制弧 默认圆心是左侧r距离的位置,弧度为360 -r右侧 for i in range(4): turtle.circle(40,80) turtle.circle(-40,80) turtle.circle(40,80/2) turtle.fd(40) turtle.circle(16,180) turtle.fd(40*2/3) turtle.done()

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值