##初始化turtle:
t = turtle.Turtle() 或者 t = turtle.Pen()
##查詢turtle狀態
#位置
t.position()
#箭頭朝向,箭頭朝向按照順時針或者逆時針偏離的角度描述,具體是順時針或者逆時針取決於turtle.mode()的值,默認turtle.mode() = standard,表示逆時針方向;logo表示順時針
t.heading()
#初始狀態下箭頭向右轉20度,t.heading() = 340
t.right(20)
t.left(20)
#向前或向后
t.forward(10)
t.backward(10)
#設置turtle位置
t.goto(x,y) = t.setpos(x,y) = t.setposition(x,y)
t.setx(x)
t.sety(y)
#設置turtle方向,具體方向和turtle.mode相關,默認standard,設置為90度時,箭頭指向正北
t.setheading(90)
#turtle回到初始位置和初始方向
t.home()
#turtle回到初始起點,保留角度,清空畫布
t.clear()
#turtle重置,回到畫布初始狀態
t.reset()
#畫圓,第一個參數為半徑,半徑為正值,默認逆時針畫圓,半徑為負值,順時針畫圓。
#t.circle(-20,90),順時針,半徑20畫弧,弧度90
#t.circle(20,90,10),第三個參數指定總共花幾步畫完,相當於多邊形
t.circle(20)
#畫點,t.dot(),第一個參數指定圓點半徑,第二個參數指定顏色
t.dot()
t.dot(20)
t.dot(20,’blue’)
#撤銷動作
t.undo()
#設置turtle移動速度,范圍0-10,不加參數返回當前speed
t.speed(10)
#返回x,y坐標位置
t.xcor()
t.ycor()
##turtle到指定位置的距離
t.distance(0,0)
##turtle落下和抬起,抬起時移動不畫線;isdown():turtle落下返回真,否則turtle為up狀態
t.down()
t.up()
t.isdown()
#設置pen的size,參數為空時返回當前的size
t.pensize(10)
##同時設置多個pen屬性
t.pen(**dict)
• “shown”: True/False
• “pendown”: True/False
• “pencolor”: color-string or color-tuple
• “fillcolor”: color-string or color-tuple
• “pensize”: positive number
• “speed”: number in range 0..10
• “resizemode”: “auto” or “user” or “noresize”
• “stretchfactor”: (positive number, positive number)
• “outline”: positive number
• “tilt”: number
• t.pen(fillcolor="black", pencolor="red", pensize=10)
##設置填充顏色,t.color()顯示或者設置pencolor和fillcolor
t.fillcolor()
t.color(‘red’,’blue’)
#填充,顯示填充是否開啟,開始填充,結束填充
t.filling()
t.begin_fill()
t.circle()
t.end_fill()
##顯示、隱藏turtle,turtle是否可見
t.hideturtle()
t.showturtle()
t.isvisible()
#設置turtle形狀,無參時顯示當前turtle形狀
t.shape(‘turtle’)
#####################事件處理
#鼠標左鍵單擊事件,fun為綁定函數,需要2個參數,代表點擊的位置(x,y),btn指左鍵單擊的次數,只有點擊在turtle上才觸發綁定。fun=None表示解除綁定
t.onclick(fun,btn=1)
t.onrelease()
#################窗口控制
turtle.bgcolor()
turtle.bgpic() 設置或獲取背景圖片,只支持gif圖片
turtle.clear()
turtle.reset()
turtle.screensize() 窗口尺寸