目录
一、turtle库基本介绍
turtle库:
(1)1969年诞生
(2)Python语言的标准库之一
(3)入门级的图形绘制函数库
turtle绘图原理:turtle海龟走过的轨迹绘制成了图形
二、turtle绘图窗体布局
turtle.setup(width,height,startx,starty):
(1)最小单位是像素,左上角是(0,0)
(2)startx与starty:绘图窗体出现在屏幕的哪里,后两个参数可选,默认正中心
三、turtle空间坐标体系
1.绝对坐标
(1)绝对坐标:就是标准的xOy坐标系,上y右x,中央点是(0,0)
(2)turtle.goto(x,y)函数:从当前点到(x,y)点的一条直线
(3)例:
import turtle
turtle.goto(100,100)
#海龟默认出现在中央(0,0),表示从(0,0)点到(100,100)的一条直线
turtle.goto(100,-100)
#表示从(100,100)点到(100,-100)的一条直线
2.海龟坐标
(1)头朝的方向就是前方:如当龟头朝右时,右侧就为前方。开始默认头朝右
(2)turtle.forword(d) 别名turtle.fd(d):前进,d负数就倒退
(3)turtle.backward(d) 别名trutle.bk(d):后退
(4)turtle.circle(r,angle):根据半径r画多少角度的弧形。
-圆心根据r确定:默认圆心在海龟左侧(头朝的左侧,要是加了负号表示右侧)r距离的位置,默认绘制圆形。
-顺逆时针方向也根据r:r正时,画逆时针。r负时,画顺时针。
四、turtle角度坐标体系
角度坐标体系:就是sinx的坐标轴角度,绕x轴逆时针
(1)turtle.setheading(angle) 别名turtle.seth(angle):只改变方向不行进。如turtle.seth(90):海龟的朝向为90度
注意:与当前海龟头的朝向没有关系,90度就一定向上
(2)turtle.left(angle)与turtle.right(angle):在海龟当前头的方向上再向左/右转多少度
五、RGB色彩模式:
(1)取值范围:0~255之间的整数或0~1之间的小数
(2)常用:
白色:255,255,2555 1,1,1
黄色:255,215,0 1,1,0
黑色:0,0,0 0,0,0
蓝色:0,0,255 0,0,1
(3)turtle.colormode(mode)
mode的两个值:1.0和255
turtle.colormode(1.0) :RGB小数模式(默认)
turtle.colormode(255) :RGB整数模式(如果不打这个,输入turtle.pencolor(0,0,255)会报错)
(4)一致性:使用小数就一直用
(5)设置色彩
turtle.fillcolor(colorstring)
#绘制图形的填充颜色
turtle.color(color1, color2)
#同时设置pencolor=color1, fillcolor=color2
六、turtle画笔控制函数
画笔操作后一直有效,一般成对出现:
(1)turtle.penup() 别名turtle.pu():抬起画笔
(2)turtle.pendown() 别名turtle.pd():落下画笔
(3)turtle.pensize(width) 别名turtle.width(width):画笔宽度
(4)turtle.pencolor(color):
颜色字符串:turtle.pencolor("purple")
RGB的小数值:turtle.pencolor(0.63,0.13,0.94)
RGB的元组值:turtle.pencolor((0.63,0.13,0.94))
七、其他命令
turtle.write(s [,font=("font-name",font_size,"font_type")]) |
写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项 |
turtle.write('月',font=("Arial",18,"normal"))
turtle.begin_fill() |
准备开始填充图形 |
turtle.end_fill() |
填充完成 |
八、例子
1.太阳花
import turtle
import time
turtle.speed(10)
#同时设置pencolor=color1, fillcolor=color2
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(50):
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
2.七段数码管
import turtle,time
def drawLine(draw):
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
turtle.right(90)
def drawDight(dight): #根据数字绘制七段数码管
drawLine(True) if dight in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if dight in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if dight in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if dight in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if dight in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if dight in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if dight in [0,1,2,3,4,7,8,9] else drawLine(False)
turtle.left(180)
turtle.penup() #为绘制后续数字确定位置
turtle.fd(20) #为绘制后续数字确定位置
def drawDate(date):
turtle.pencolor("red")
for i in date:
if i=='-':
turtle.write('年',font=("Arial",18,"normal"))
turtle.pencolor("green")
turtle.fd(40)
elif i=='=':
turtle.write('月',font=("Arial",18,"normal"))
turtle.pencolor("blue")
turtle.fd(40)
elif i=='+':
turtle.write('日',font=("Arial",18,"normal"))
else:
drawDight(eval(i))
def main():
turtle.speed(10)
turtle.setup(800,350,200,200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
参考:
https://blog.csdn.net/zengxiantao1994/article/details/76588580