Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子
最近發(fā)現(xiàn)一個(gè)很有意思的畫圖的python庫,叫做turtle,這里先說下用turtle這個(gè)庫來實(shí)現(xiàn)用正方形畫圓的思路。
每次都用烏龜(turtle)來畫出一個(gè)正方形,然后通過旋轉(zhuǎn)3°后,繼續(xù)畫一樣的正方形,在通過120次循環(huán)后就實(shí)現(xiàn)了完整的圓,這里當(dāng)然也可以用其他的角度和次數(shù),只要能完成360度就可以了。
先看完成的圖形和代碼。
代碼如下:
import turtle window = turtle.Screen() #設(shè)置好畫圖的基本參數(shù) window.bgcolor(“blue”) wugui= turtle.Turtle() wugui.shape(“turtle”) wugui.color(“red”) wugui.speed(5) for i in range(120): #這里設(shè)定正方形的個(gè)數(shù) wugui.forward(100) wuguiright(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(93)#這里決定每次旋轉(zhuǎn)角度,也就決定了需要畫正方形的次數(shù)。 window.exitonclick()
代碼應(yīng)該很簡單易懂,就不再說了。turtle真的是非常強(qiáng)大的一個(gè)繪圖工具,可以繪制各種各樣有趣的圖形,詳情請看 turtle官方文檔,這里說點(diǎn)基本的參數(shù)與用法吧。主要包括兩部分,烏龜與畫布。
烏龜方法
烏龜運(yùn)動
烏龜移動與繪畫
forward() | fd() 向前移動指定的距離。參數(shù):(integer or float))一個(gè)數(shù)字
backward() | bk() | back() 向后移動指定的距離。參數(shù):(integer or float))一個(gè)數(shù)字
right() | rt() left() | lt() 向右 旋轉(zhuǎn)指定的角度。參數(shù):(integer or float))一個(gè)數(shù)字
goto() | setpos() | setposition() 去到位置(x,y)。參數(shù):(x, y=None))一個(gè)數(shù)字
setx() 設(shè)置X位置。參數(shù):(integer or float)一個(gè)數(shù)字
sety() 設(shè)置Y位置。參數(shù):(integer or float)一個(gè)數(shù)字
setheading() | seth() 方向設(shè)置為to_angle.就是東西南北方向,上北下南左西右東
home() 移動到原點(diǎn) – 坐標(biāo)(0,0):并將其標(biāo)題設(shè)置為其起始方向
circle() 繪制一個(gè)給定半徑的圓。參數(shù):(radius,extent,steps)(一個(gè)數(shù)字__半徑,如果值為正則逆時(shí)針,負(fù)數(shù)為順時(shí)針__,一個(gè)數(shù)字, 執(zhí)行的步數(shù))
dot() 用顏色畫出一個(gè)直徑大小的圓點(diǎn)。參數(shù):(size,color)(一個(gè)大于1的整數(shù)_可None,顏色值)
stamp() 將當(dāng)前位置上的形狀復(fù)制到畫布上,返回stamp_id.可通過下方的clearstamp刪除
clearstamp() 刪除stamp()返回來的值,參數(shù):(stamp_id)stamp函數(shù)返回值
clearstamps() 刪除所有的stamp,默認(rèn)無參數(shù),刪除所有
undo() 撤銷上一步動作
speed() 烏龜爬行速度,我們這設(shè)置的是5,不設(shè)置為最快,直接生成
烏龜當(dāng)前狀態(tài)
position() | pos() 當(dāng)前位置
towards() 返回與指定點(diǎn)之間的角度 參數(shù):(X,Y)一個(gè)位置
xcor() 返回烏龜X坐標(biāo)
ycor() 返回烏龜Y坐標(biāo)
heading() 返回當(dāng)前烏龜?shù)姆较蛑?br />
distance() 返回烏龜與坐標(biāo)點(diǎn)之間的距離。參數(shù):(X,Y)一個(gè)位置
設(shè)置與測量
degrees() 設(shè)置整個(gè)圓的角度,最好不要動。參數(shù):(integer or float)一個(gè)整數(shù)
radians() 將角度測量單位設(shè)置為弧度。360度就是2π
畫筆控制
繪畫狀態(tài)
pendown() | pd() | down() 將筆落下放在圖上,移動的時(shí)候?qū)L圖
penup() | pu() | up() 將筆提起來,移動的時(shí)候?qū)⒉粫L圖
pensize() | width() 設(shè)置線條的粗細(xì)。參數(shù):(width)一個(gè)正數(shù)
pen() 使用鍵值對設(shè)置筆的屬性
“shown”: True/False 顯示 “pendown”: True/False 筆落下 “pencolor”: color-string or color-tuple 筆的顏色 “fillcolor”: color-string or color-tuple 填充顏色 “pensize”: positive number 筆大?。ㄕ麛?shù)) “speed”: number in range 0..10 繪畫速度(范圍0-10) “resizemode”: “auto” or “user” or “noresize” 大小調(diào)整模式 “stretchfactor”: (positive number, positive number) 拉伸參數(shù) “outline”: positive number 外部 “tilt”: number 傾斜
isdown() 如果筆停止返回True,反之返回False
顏色控制
color() 顏色,直接使用返回當(dāng)前筆顏色與填充顏色
pencolor() 設(shè)置筆的顏色
fillcolor() 設(shè)置筆的填充顏色
填充
filling() 返回填充狀態(tài),
begin_fill() 在填充之前使用
end_fill() 結(jié)束填充
更多繪畫控制
reset() 重置所有參數(shù)
clear() 刪除繪畫,與reset不同之處僅僅是刪除圖形,參數(shù)保留
write() 寫文字
arg – object to be written to the TurtleScreen 寫到TurtleScreen的參數(shù) move – True/False 移動 align – one of the strings “l(fā)eft”, “center” or right” 對齊參數(shù)3選1(left,right,center) font – a triple (fontname, fontsize, fonttype) 字體
烏龜狀態(tài)
可視性
showturtle() | st() 顯示烏龜?shù)男螤?br /> hideturtle() | ht() 隱藏烏龜?shù)男螤?br /> isvisible() 是否可見,返回True or False
外表
shape() 設(shè)置烏龜?shù)膱D形形狀,可選( “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”)
resizemode() 大小調(diào)整模式
“auto”: adapts the appearance of the turtle corresponding to the value of pensize. 由畫筆大小決定(自動) “user”: adapts the appearance of the turtle according to the values of stretchfactor and outlinewidth (outline), 由拉伸參數(shù)決定 “noresize”: no adaption of the turtle's appearance takes place. 不調(diào)整
shapesize() | turtlesize() 返回筆的屬性。
shearfactor() 設(shè)置或者返回當(dāng)前剪切因子
settiltangle() 與tilt() 一樣,只是可以為空,則返回當(dāng)前旋轉(zhuǎn)角度
tiltangle() 棄用
tilt() 設(shè)置當(dāng)前烏龜角度,不調(diào)整烏龜前進(jìn)方向(僅僅改變?yōu)觚敇幼樱?br />
shapetransform() 設(shè)置或返回烏龜?shù)男螤畹漠?dāng)前轉(zhuǎn)換矩陣
get_shapepoly() 返回當(dāng)前形狀的坐標(biāo)
監(jiān)聽動作
onclick() 鼠標(biāo)點(diǎn)擊事件
fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas函數(shù)需要有兩個(gè)參數(shù) num – number of the mouse-button, defaults to 1 (left mouse button) 單擊次數(shù),默認(rèn)1 add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 添加新的綁定函數(shù),否則替代之前函數(shù) 例子:def turn(x, y): 。。。 left(180) onclick(turn)
onrelease() 鼠標(biāo)釋放事件,同上
ondrag() 鼠標(biāo)移動事件,同上
烏龜一些特殊方法
begin_poly() 開始記錄多邊形的頂點(diǎn),當(dāng)前點(diǎn)為起始點(diǎn)
end_poly() 結(jié)束記錄多邊形的頂點(diǎn),當(dāng)前點(diǎn)為起始點(diǎn)
get_poly() 返回最后記錄的多邊形
clone() 復(fù)制一個(gè)一模一樣的烏龜
getturtle() | getpen() 獲取trutle對象本身
getscreen() 獲取畫布對象
setundobuffer() 設(shè)置或禁用中斷器
undobufferentries() 返回undobuffer中的條目數(shù)
畫布的方法
窗口控制
bgcolor() 設(shè)置或返回當(dāng)前畫布的背景顏色
bgpic() 設(shè)置或返回當(dāng)前畫布的背景圖片名稱
clear() | clearscreen() 清除圖形
reset() | resetscreen() 重置畫布
screensize() 畫布大小
canvwidth – positive integer, new width of canvas in pixels 寬度 canvheight – positive integer, new height of canvas in pixels 高度 bg – colorstring or color-tuple, new background color 顏色
setworldcoordinates() 全局坐標(biāo)
llx – a number, x-coordinate of lower left corner of canvas 左下X坐標(biāo) lly – a number, y-coordinate of lower left corner of canvas 左下X坐標(biāo) urx – a number, x-coordinate of upper right corner of canvas 右下X坐標(biāo) ury – a number, y-coordinate of upper right corner of canvas 右下X坐標(biāo)
動畫控制
delay() 動畫延遲(毫秒)參數(shù):(integer )一個(gè)數(shù)字
tracer() 開啟動畫,設(shè)置延遲
n – nonnegative integer n個(gè)動作執(zhí)行一次 delay – nonnegative integer 延遲,毫秒
update() 更新畫布,當(dāng)tracer關(guān)閉時(shí)使用
畫布監(jiān)聽
listen() 開啟監(jiān)聽,將鼠標(biāo)定位到畫布
onkey() | onkeyrelease() 鍵盤彈起(需要位于焦點(diǎn)上,使用上面listen后)
fun – a function with no arguments or None 動作函數(shù)
key – a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按鍵
onkeypress() 鍵盤按下事件,同上
onclick() | onscreenclick() 鼠標(biāo)點(diǎn)擊事件
fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas 函數(shù)需要兩個(gè)參數(shù)
num – number of the mouse-button, defaults to 1 (left mouse button) 點(diǎn)擊次數(shù)
add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 是否是添加,還是替換
ontimer() 計(jì)時(shí)器
fun – a function with no arguments 無需函數(shù)
t – a number >= 0 事件間隔
mainloop() | done() 開始事件循環(huán),必須是烏龜繪畫中的最后一個(gè)函數(shù)
設(shè)置與特殊方法
mode() 繪圖模式,3選1 “standard”, “l(fā)ogo” or “world”
colormode() 顏色模式, 1.0 或者 255
getcanvas() 返回當(dāng)前TurtleScreen.的Canvas
getshapes() 返回當(dāng)前可用形狀
register_shape() | addshape() 3種調(diào)用方式。
1.直接調(diào)用圖片。screen.register_shape(“turtle.gif”)
2.調(diào)用形狀,制定點(diǎn)位置。
screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
3,調(diào)用形狀,名字隨便取
turtles() 返回烏龜list數(shù)組
window_height() 返回窗口高度
window_width() 返回窗口寬度
輸入方法
textinput() 文字輸入
title – string 輸入名字
prompt – string 輸入的文本
numinput() 數(shù)字輸入
title – string 輸入名字
prompt – string 輸入文本
default – number (optional) 默認(rèn)
minval – number (optional) 最小
maxval – number (optional) 最大
屏幕特有方法
bye() 關(guān)閉turtle窗口
exitonclick() 鼠標(biāo)點(diǎn)擊關(guān)閉窗口
setup() 設(shè)置主窗口參數(shù)
width – if an integer, a size in pixels, if a float, a fraction of the screen; default is 50% of screen 寬 度
height – if an integer, the height in pixels, if a float, a fraction of the screen; default is 75% of screen 高度
startx – if positive, starting position in pixels from the left edge of the screen, if negative from the right edge, if None, center window horizontally 左邊開始位置
startx – if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge, if None, center window vertically 右邊開始位置
title() 設(shè)置繪畫窗口標(biāo)題
以上這篇Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python+pygame實(shí)現(xiàn)簡易五子棋小游戲的三種方式
這篇文章主要介紹了使用python實(shí)現(xiàn)簡易五子棋小游戲,文中提供了三種實(shí)現(xiàn)方式,解決思路和部分實(shí)現(xiàn)代碼,感興趣的朋友可以參考下2023-03-03Python圖像處理之直線和曲線的擬合與繪制【curve_fit()應(yīng)用】
這篇文章主要介紹了Python圖像處理之直線和曲線的擬合與繪制,結(jié)合實(shí)例形式分析了Python曲線擬合相關(guān)函數(shù)curve_fit()的使用技巧,需要的朋友可以參考下2018-12-12python讀寫文件write和flush的實(shí)現(xiàn)方式
今天小編就為大家分享一篇python讀寫文件write和flush的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02基于Python3制作一個(gè)帶GUI界面的小說爬蟲工具
這篇文章主要為大家介紹了一個(gè)通過Python3制作的帶GUI界面的小說爬蟲工具,用來從筆趣閣爬取小說。感興趣的小伙伴可以跟隨小編一起動手嘗試一下2022-02-02Python實(shí)現(xiàn)的對本地host127.0.0.1主機(jī)進(jìn)行掃描端口功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的對本地host127.0.0.1主機(jī)進(jìn)行掃描端口功能,可實(shí)現(xiàn)掃描本機(jī)開放端口的功能,涉及Python socket模塊與Thread多線程模塊相關(guān)使用技巧,需要的朋友可以參考下2019-02-02