Turtle method using the basic module (canvas)

 
method
Logogram
description
example
Move and draw
forward()
fd()
go ahead
turtle.forward (10): Forward 10 units
backward()
bk()、back()
Retreat
turtle.backward (): 10 units reverse
right()
rt()
Turn right
turtle.rignt (30): Rotate right 30 °
left
lt()
Turn left
turtle.left (30): 30 ° leftward rotation
goto()、setpos()、setposition()
 
Positioning / Go
turtle.goto (0,0): the origin is moved to the small turtles
setx ()
 
Sets the x coordinate
turtle.setx (10): sets the x coordinate is 10 units
sets ()
 
Sets the y coordinate
turtle.sety (10): sets the y coordinate is 10 units
setheading()
seth()
Disposed toward
 turtle.seth (90): Little Turtle set to 90 degrees
home()
 
Return home
turtle.home (): small turtles return to the origin, and sets an initial direction toward the
circle()
 
Circle
turtle.circle (100,360,6): draw a circle with a radius of 100 hexagon inscribed
turtle.circle (100,180): Draw a radius of 100, an arc angle of 180 degrees
dot()
 
Image spots
turtle.dot (10, 'red'): to draw red dot size of 10
stamp()
 
Seal (cursor pointer shape)
turtle.stamp (): Little Turtle printing shape (the shape of the pointer) at the current position, the seal returns id
clearstamp()
 
Clear stamp
turtle.clearstamp (designated stamp id): Clears Seal
clearstamps()
 
Clear number of stamps
turtle.clearstamps (2): Clear the first two stamps
undo()
 
Revocation of a small turtle action
turtle.undo (): a small turtle undo operation, a plurality of actions for loop revocation
speed()
 
Set the pen speed
turtle.speed (10): 0-10 [] Set speed level, the larger the number, the faster
Acquired the status of a small turtle
position()
pos()
Gets Little Turtle current coordinate position
turtle.position (): Gets Little Turtle current coordinates
towards()
 
Target direction (angle)
turtle.towards (0,0): the angle connection, if the direction of the small turtles initial position (10, 10), then the value from the point (0,0) and between the rotary accordance little turtle mode decision (standard, logo)
xcor()
 
Gets the x-coordinate
turtle.xcor (): Returns the current x coordinate of the small turtle
ycor ()
 
Gets the y-coordinate
turtle.ycor (): Return the current y coordinate little turtle
heading()
 
Get toward
turtle.heading (): returns the current orientation of small turtles
distance()
 
Get away
turtle.distance (0,0): If small turtles initial position (10, 10), then the value from the distance between the point (0,0)
Setting the unit of measure
degrees()
 
Setting angle
turtle.degrees (400): setting a total angle of 400 degrees, if the total angle of 360 degrees before, and the current angle is 90 degrees, then 400 after setting, the current position at this time in degrees 100 degrees, is calculated: 90/360 = 100/400
radians()
 
Set in radians
turtle.radians (): Set the angle in radians, such as the current angle is 90 degrees, the setting of 1.57 radians, i.e., 90 ° = π / 2≈1.57
Drawing State
pendown ()
pd()、down()
Brush down
turtle.down():画笔落下
penup()
pu()、up()
画笔抬起
turtle.up():画笔抬起
pensize()
width()
设置画笔粗细(大小)
turtle.pensize():设置画笔大小
pen()
 
设置画笔属性
turtle.pen(fillcolor='yellow',pencolor='red',pensize='5'):设置多个画笔属性
isdown()
 
画笔是否落下
turtle.isdown():判断画笔是否落下,返回布尔值
颜色控制
color()
 
设置颜色
turtle.color('red','yellow'):设置和返回画笔颜色为红色,填充颜色为黄色
pencolor()
 
画笔颜色
turtle.pencolor('red'):设置和返回画笔颜色为红色
fillcolor()
 
填充颜色
turtle.fillcolor('red'):设置和返回填充颜色为红色
填充
filling()
 
是否填充
turtle.filling():判断是否填充,返回布尔值
begin_fill()
 
开始填充
turtle.begin_fill():在绘制要填充的图形之前调用
end_fill()
 
结束填充
turtle.end_fill():从开始填充到画完要填充的图形后调用,结束填充
更多绘图控制
reset()
 
重置指定Turtle对象的小乌龟和绘图
turtle.reset():删除小乌龟绘制的图形,并回到原点,所有设置回复默认值
clear()
 
清空指定Turtle对象所绘的图形
turtle.clear():删除指定小乌龟的绘图,turtle为一个Turtle的对象,若有多个Turtle对象时,指定一个进行删除
write()
 
书写文本,turtle.write(arg,move='False',align='center',font=('Arial',8,'normal')):arg为写的内容,align为对齐方式,
turtle.write('你好',True,align='center',font=('Arial',10,'normal')):在画布中写入文本为“你好”,以小乌龟位置居中,字体为Arial,大小为10,字体类型为正常,bold为加粗,写完后小乌龟移动到文本的右下角(move为True)
可见性
showturtle()
st()
显示小乌龟
turtle.st():显示小乌龟
hideturtle()
ht()
隐藏小乌龟
turtle.ht():隐藏小乌龟
isvisible()
 
是否可见
turtle.isvisible():判断小乌龟是否可见,返回布尔值
外观
shape()
 
设置小乌龟形状
turtle.shape('turtle'):设置小乌龟的形状为乌龟, arrow:箭头,turtle:龟,circle:圆,square:正方形,triangle:三角形,classic:经典
resizemode()
 
设置大小调整模式
turtle.resizemode('auto'):根据画笔出席值调整小乌龟的外观大小,“user”:根据拉伸因子和轮廓宽度值调整小乌龟的外观,两者是由shapesize()设置,“noresize”:默认不调整小乌龟的外观大小
shapesize()
turtlesize()
设置小乌龟形状大小,当且仅当大小调整模式设为“user”时起作用
先设置turtle.resizemode('user')再使用turtle.shapesize(3,5,4):设置小乌龟图形拉伸值,第一个值:3为垂直拉伸值,第二个值:5位水平拉伸值,第三个值:4为轮廓线的粗细值 加轮廓线后:
shearfactor()
 
设置或返回当前的剪切因子,不改变小乌龟的朝向
turtle.shearfactor(1):设置小乌龟与x轴的正切值为1,拉伸小乌龟 正切值为1,小乌龟与x轴夹角45°
settiltangle()
 
设置倾角,不改变小乌龟的朝向
turtle.settiltangle(45):旋转小乌龟形状指向与x轴成45°夹角
tiltangle()
 
设置或返回当前的倾角,不改变小乌龟的朝向
turtle.tiltangle():返回当前小乌龟的倾角
tilt()
 
设置小乌龟形状自其当前的倾角转动角度倾斜,不改变小乌龟的朝向
turtle.tilt(45):在当前倾角下转动45°(当前倾角为45°)
shapetransform()
 
设置或返回小乌龟形状的当前变形矩阵
turtle.shapetransform(4,-1,0,2):(没搞懂)
get_shapepoly()
 
返回以坐标值对元组表示的当前形状多边形
turtle.shapetransform(4,-1,0,2)
turtle.get_shapepoly():(没搞懂)
使用事件(还没理解透)
onclick()
 
onclick(fun,btn=1,add=None):当鼠标点击小乌龟时,执行函数(fun为传入的函数),btn值(1为鼠标左键,2为鼠标中间键,3为鼠标右键),add为True则将添加一个新绑定
def trun(x,y):
     right(90)
onclick(turn)
当鼠标点击小乌龟时,执行函数trun,向右旋转90°
onrelease()
 
onrelease(fun,btn=1,add=None):当鼠标点击小乌龟释放鼠标时,执行函数(fun为传入的函数),btn值(1为鼠标左键,2为鼠标中间键,3为鼠标右键),add为True则将添加一个新绑定
def trun(x,y):
     right(90)
onrelease(turn)
当鼠标点击小乌龟释放鼠标时,执行函数trun,向右旋转90°
ondrag()
 
ondrag(fun,btn=1,add=None):当鼠标点击小乌龟释放鼠标时,执行函数(fun为传入的函数),btn值(1为鼠标左键,2为鼠标中间键,3为鼠标右键),add为True则将添加一个新绑定
turtle.ondrag(turtle.goto):点击并拖动小乌龟在画布上移动手绘线条
特殊小乌龟方法
begin_poly()
 
开始记录多边形
turtle.home()
turtle.begin_poly()
turtle.fd(100)
turtle.left(20)
turtle.fd(30)
turtle.left(60)
turtle.fd(50)
turtle.end_poly()
p = turtle.get_poly()
screen.register_shape("myFavouriteShape", p)
end_poly()
 
结束记录多边形
get_poly()
 
获取多边形
clone()
 
克隆一个Turtle对象
a = turtle.Turtle()
b = a.clone():克隆一个Turtle对象
getturtle()
getpen()
获取小乌龟画笔
a = turtle.getturtle():获取一个Turtle对象
getscreen()
 
获取屏幕
a = turtle.getscreen():获取一个Screen对象
setundobuffer()
 
设置撤销缓冲区
turtle.setundobuffer(50):设置撤销缓冲区次数上限,最多只能撤销50次小乌龟的动作
undobufferentries()
 
返回撤销缓冲区条目数
turtle.undobufferentries():返回撤销缓冲区里剩下的条目数
窗口控制
bgcolor()
 
设置背景颜色
screen.bgcolor('white'):设置画布背景颜色
bgpic()
 
设置背景图片
screen.bgpic('C:\\Users\\Archer\\Pictures\\Camera Roll\\图片.jpg'):设置背景图片
clearscreen()
clear()
从中删除所有小乌龟的全部绘图。将已清空的TurtleScreen重置为初始状态:白色背景,无背景图片,无事件绑定并启用追踪
screen.clearscreen():清除所有小乌龟绘图,并重置状态
resetscreen()
reset()
重置
screen.resetscreen():重置所有小乌龟为初始状态,并清除所绘制的图形,但使用clone()克隆的Turtle对象所画的图形灭有
screensize()
 
设置屏幕大小
screen.screensize(800,1000,'white'):设置宽为800,高为1000,背景颜色为白色的窗口,有滚动条
setworldcoordinates()
 
screen.setworldcoordinates(llx,lly,urx,ury):llx-为画布左下角的x坐标,lly-为画布左下角的y坐标,urx-为画布右上角的x坐标, ury-为画布右上角的y坐标
screen.setworldcoordinates(-50,-7.5,50,7.5):(没搞懂怎么用)
动画控制
delay()
 
延迟(毫秒):连续两次画布刷新的间隔时间
screen.delay(5):设置延迟值为5毫秒
tracer()
 
tracer(n,delay):追踪小乌龟的绘图,当n为0或为False时,禁用追踪,默认为1;delay为延迟(毫秒)
screen.tracer(8,25):没第8次屏幕刷新会执行,延迟25毫秒(还不太懂怎么用)
update()
 
更新
screen.update():执行一次TurtleScreen刷新。在禁用追踪时使用
使用屏幕事件
listen()
 
监听
screen.listen():监听键盘的点击操作,与键盘点击事件一起用,没有监听,键盘操作无效果
onkeyrelease()
onkey()
当键盘按下并释放
def up():
     turtle.fd(10)
screen.listen()
screen.onkey(up,'Up'):按下键盘并释放上方向键,执行up函数,前进10个单位
onkeypress()
 
当键盘按下
def up():
     turtle.fd(10)
screen.listen()
screen.onkeypress(up,'Up'):按住键盘上方向键,一直执行up函数,就一直前进10个单位,松开键盘停止执行
onscreenclick()
onclick()
onclick(fun,btn=1,add=None):当鼠标点击画布屏幕时,执行函数(fun为传入的函数),btn值(1为鼠标左键,2为鼠标中间键,3为鼠标右键),add为True则将添加一个新绑定
def turn(x,y):
     turtle.right(90)
screen.onclick(turn):当鼠标点击画布屏幕时执行turn函数
ontimer()
 
screen.ontimer(fun,t=0):定时器,在达到t毫秒后,执行fun函数
def up():
     turtle.fd(10)
screen.ontimer(up,1000):程序执行1秒后,执行up函数,前进10个单位
mainloop()
done()
主循环
screen.mainloop():开始事件循环,必须作为一个小乌龟绘图程序的结束语句,否则程序会被关闭
设置与特殊方法
mode()
 
设置小乌龟的模式
"standard"模式:初始方向(朝右),逆时针,默认模式
"logo"模式:初始方向(朝上),顺时针
screen.mode('logo'):设置小乌龟模式为logo模式
colormode()
 
screen.colormode(cmode):设置颜色模式,cmode值为1.0或255,构成颜色的RGB三元组数字必须在0-cmode范围之间,默认数值为1.0
screen.colormode(255):设置颜色模式为0-255之间,则可使用screen.bgcolor(255,255,255)      :白色
getcanvas()
 
获取画布对象
cv = screen.getcanvas():获取画布对象,没搞懂怎么用
getshapes()
 
获取所有小乌龟的形状信息
screen.getshapes():返回当前所有小乌龟的形状的列表
register_shape()
addshape()
添加一个gif格式的图片到图形列表中
screen.register_shape('C:\\Users\\Archer\\Pictures\\Camera Roll\\太极.gif')
print(screen.getshapes())
再通过turtle.shape('C:\\Users\\Archer\\Pictures\\Camera Roll\\太极.gif')设置小乌龟形状
turtles()
 
获取所有小乌龟的对象列表
screen.turtles():返回屏幕上所有小乌龟的对象列表
window_height()
 
获取窗口高度
screen.window_height():返回窗口高度
window_width()
 
获取窗口宽度
screen.window_width():返回窗口宽度
输入方法
textinput()
 
screen.textinput(title,prompt):文本输入,title为窗口标题,prompt为提示输入信息
screen.textinput('新年问候','你好'):弹出一个对话框窗口用来输入一个字符串,输入后返回这个字符串,取消返回None
numinput()
 
screen.numinput(title,prompt,default,minval,maxval):数字输入,title为窗口标题,prompt为提示输入信息,default为默认值,minval为最小值,maxval为最大值
screen.numinput('调查问卷','请问你多少岁了',18,0,150):弹出一个对话框窗口用来输入一个数字,输入后返回这个数值,取消返回None
Screen专有方法
bye()
 
退出
screen.bye():关闭绘图窗口
exitonclick()
 
当鼠标左键点击时退出窗口
screen.exitonclick():鼠标左键点击后关闭窗口
setup()
 
screen.setup(w,h,x,y):设置主窗口大小和位置,w为宽度(像素),如为浮点数,则表示屏幕占比,h为高度,x为初始位置距离屏幕左边多少像素,y为初始位置距离屏幕上边多少像素,没有设置xy默认居中
screen.setup(500,500,0,0):窗口大小为宽500像素,高为500像素,在屏幕左上角打开窗口
title()
 
设置窗口标题
screen.title('Welcome to the turtle zoo!'):设置窗口标题
 
 
 

Guess you like

Origin www.cnblogs.com/Archer-Xin/p/12215490.html