1.画布(canvas)
画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置
1.1设置画布大小
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
参数分别为画布的宽(单位像素), 高, 背景颜色
turtle.screensize(800, 600, "green")
turtle.screensize(800, 600) #背景色默认白色
turtle.screensize() #返回默认大小(400, 300)
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(800, 800, 100, 100)#与参数位置一一对应
2.画笔
2.1 画笔属性
1.海龟(画笔)角度:初始方向朝右(东),位置在画布中心,left,right是以海龟角度转向。
2.绝对角度:x轴正方向为0°/360°,负方向为±180°,y轴正方向为90°/-270°,负方向为-90°/270°
3.turtle.pensize():设置画笔的宽度;
4.turtle.pencolor(); 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组,
5.turtle.speed(speed): 设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快,0最为丝滑
6.turtle.hideturtle():隐藏画笔
7.turtle.showturtle():显示画笔
2.2绘图命令(画图动作)
操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令
(1)画笔运动命令
命令 | 说明 |
---|---|
turtle.fd(distance) | 向当前画笔方向移动distance像素长,若distance为负数相当于backward向反方向移动 |
turtle.backward(distance) | 向当前画笔相反方向移动distance像素长度 |
turtle.right(degree) | 顺时针移动degree°,海龟头当前方向右转degree° |
turtle.left(degree) | 逆时针移动degree°,海龟头当前方向左转degree° |
turtle.seth(angle) | 根据绝对角度设置当前画笔方向,海龟头的朝向 |
turtle.pendown() | 放下画笔,即做画画动作 |
turtle.penup() | 抬起画笔,移动不在图上留下痕迹,用于移动画笔,从另一位置着笔 |
turtle.goto(x,y) | 将画笔移动到坐标为x,y的位置(画布中心为(0,0)) |
turtle.circle(r,degree) | 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆,画多少度(弧度),degree=180°即半圆 |
setx( ) | 将当前x轴移动到指定位置 |
sety( ) | 将当前y轴移动到指定位置 |
home() | 设置当前画笔位置为原点,朝向东。 |
dot(d) | 以d为直径画一个圆点 |
(2)画笔控制命令
命令 | 说明 |
---|---|
turtle.fillcolor(colorstring) | 绘制图形的填充颜色 |
turtle.color(color1, color2) | 同时设置pencolor=color1, fillcolor=color2 |
turtle.filling() | 返回当前是否在填充状态 |
turtle.begin_fill() | 准备开始填充图形 |
turtle.end_fill() | 填充完成 |
turtle.hideturtle() | 隐藏画笔的turtle形状 |
turtle.showturtle() | 显示画笔的turtle形状 |
(3)全局控制命令
命令 | 说明 |
---|---|
turtle.clear() | 清空turtle窗口,但是turtle的位置和状态不会改变 |
turtle.reset() | 清空窗口,重置turtle状态为起始状态 |
turtle.undo() | 撤销上一个turtle动作 |
turtle.isvisible() | 返回当前turtle是否可见 |
stamp() | 复制当前图形 |
turtle.write(s [,font=(“font-name”,font_size,“font_type”)]) | 写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项 |
(4)其他命令
命令 | 说明 |
---|---|
turtle.mainloop()或turtle.done() | 启动事件循环 -调用Tkinter的mainloop函数。必须是乌龟图形程序中的最后一个语句。 |
turtle.mode(mode=None) | 设置乌龟模式(“standard”—(初始方向:右/东,正角度:逆时针),“logo”—(初始方向:上/北,正角度:顺时针)或“world”)并执行重置。如果没有给出模式,则返回当前模式 |
turtle.delay(delay=None) | 设置或返回以毫秒为单位的绘图延迟。 |
turtle.begin_poly() | 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 |
turtle.end_poly() | 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 |
turtle.get_poly() | 返回最后记录的多边形。 |
命令详解:
turtle.circle(radius, extent=None, steps=None)
描述:以给定半径画圆
参数:
radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆
牛刀小试:绘制多啦A梦
(绘制想画的形象有助于提高对turtle库基础方法使用的熟练度)
import turtle as t
t.setup(650,350)
t.penup()
t.fd(-50)
t.hideturtle()
#内圈
t.pendown()
t.pensize(2)
t.pencolor("black")
t.fd(100)
t.left(45)
t.circle(70,270)
t.penup()
t.right(45)
t.fd(5)
t.left(90)
#外圈
t.pendown()
t.fd(100)
t.left(42)
t.circle(75,275)
t.seth(0)
t.fd(50)
#铃铛
t.circle(-5,450)
t.right(90)
t.fd(10)
t.penup()
t.goto(0,65)
#鼻子
t.pendown()
t.circle(5,540)
t.goto(0,30)
#嘴巴
t.fd(30)
t.fd(-60)
t.right(45)
t.circle(42,90)
t.penup()
t.goto(0,70)
t.pendown()
#眼睛
t.seth(90)
t.circle(-10)
t.circle(10)
t.penup()
t.goto(5,70)
t.pendown()
t.dot(4,"black")
t.penup()
t.goto(-5,70)
t.pendown()
t.dot(4,"black")
t.penup()
#右胡须
t.goto(40,55)
t.seth(15)
t.pendown()
t.fd(45)
t.penup()
t.goto(40,45)
t.seth(0)
t.pendown()
t.fd(45)
t.penup()
t.goto(40,35)
t.seth(-15)
t.pendown()
t.fd(45)
t.penup()
#左胡须
t.goto(-40,55)
t.seth(165)
t.pendown()
t.fd(45)
t.penup()
t.goto(-40,45)
t.seth(180)
t.pendown()
t.fd(45)
t.penup()
t.goto(-40,35)
t.seth(-165)
t.pendown()
t.fd(45)
t.penup()
#左半身
t.goto(50,0)
t.pendown()
t.seth(-60)
t.fd(50)
t.circle(-10,540)
t.seth(-100)
t.fd(25)
t.right(120)
t.circle(15)
t.penup()
#右半身
t.goto(-50,0)
t.pendown()
t.seth(-120)
t.fd(50)
t.circle(10,540)
t.seth(-80)
t.fd(25)
t.left(120)
t.circle(-15,450)
t.seth(15)
t.circle(-130,30)
#口袋
t.penup()
t.goto(-45,-5)
t.seth(-90)
t.pendown()
t.fd(20)
t.circle(45,180)
t.fd(20)
t.penup()
t.goto(35,-35)
t.pendown()
t.seth(180)
t.fd(70)
t.left(135)
t.circle(50,90)
t.done()
结果展示: