1. 程式人生 > >用turtle實現用正方形畫圓

用turtle實現用正方形畫圓

最近發現一個很有意思的畫圖的python庫,叫做turtle,這裡先說下用turtle這個庫來實現用正方形畫圓的思路。

每次都用烏龜(turtle)烏龜來畫出一個正方形,然後通過旋轉3°後,繼續畫一樣的正方形,在通過120次迴圈後就實現了完整的圓,這裡當然也可以用其他的角度和次數,只要能完成360度就可以了。

先看完成的圖形和程式碼。

正方形畫圓
程式碼如下:

import turtle

window = turtle.Screen() #設定好畫圖的基本引數
window.bgcolor(“blue”)
wugui= turtle.Turtle()
wugui.shape(“turtle”)
wugui.color(“red”) wugui.speed(5) for i in range(120): #這裡設定正方形的個數 wugui.forward(100) wuguiright(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(93)#這裡決定每次旋轉角度,也就決定了需要畫正方形的次數。 window.exitonclick()

程式碼應該很簡單易懂,就不再說了。turtle真的是非常強大的一個繪圖工具,可以繪製各種各樣有趣的圖形,詳情請看 turtle官方文件,這裡說點基本的引數與用法吧。主要包括兩部分,烏龜與畫布。

烏龜方法

烏龜運動

烏龜移動與繪畫

forward() | fd() 向前移動指定的距離。引數:(integer or float))一個數字

backward() | bk() | back() 向後移動指定的距離。引數:(integer or float))一個數字

right() | rt() left() | lt() 向右 旋轉指定的角度。引數:(integer or float))一個數字

goto() | setpos() | setposition() 去到位置(x,y)。引數:(x, y=None))一個數字

setx() 設定X位置。引數:(integer or float)一個數字

sety() 設定Y位置。引數:(integer or float)一個數字

setheading() | seth() 方向設定為to_angle.就是東西南北方向,上北下南左西右東

home() 移動到原點 – 座標(0,0):並將其標題設定為其起始方向

circle() 繪製一個給定半徑的圓。引數:(radius,extent,steps)(一個數字__半徑,如果值為正則逆時針,負數為順時針__,一個數字, 執行的步數)

dot() 用顏色畫出一個直徑大小的圓點。引數:(size,color)(一個大於1的整數_可None,顏色值)

stamp() 將當前位置上的形狀複製到畫布上,返回stamp_id.可通過下方的clearstamp刪除

clearstamp() 刪除stamp()返回來的值,引數:(stamp_id)stamp函式返回值

clearstamps() 刪除所有的stamp,預設無引數,刪除所有

undo() 撤銷上一步動作

speed() 烏龜爬行速度,我們這設定的是5,不設定為最快,直接生成

烏龜當前狀態

position() | pos() 當前位置
towards() 返回與指定點之間的角度 引數:(X,Y)一個位置
xcor() 返回烏龜X座標
ycor() 返回烏龜Y座標
heading() 返回當前烏龜的方向值
distance() 返回烏龜與座標點之間的距離。引數:(X,Y)一個位置

設定與測量

degrees() 設定整個圓的角度,最好不要動。引數:(integer or float)一個整數
radians() 將角度測量單位設定為弧度。360度就是2π

畫筆控制

繪畫狀態

pendown() | pd() | down() 將筆落下放在圖上,移動的時候將會繪圖
penup() | pu() | up() 將筆提起來,移動的時候將不會繪圖
pensize() | width() 設定線條的粗細。引數:(width)一個正數
pen() 使用鍵值對設定筆的屬性

“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              繪畫速度(範圍0-10)
“resizemode”: “auto” or “user” or “noresize”  大小調整模式
“stretchfactor”: (positive number, positive number)    拉伸引數
“outline”: positive number   外部
“tilt”: number   傾斜

isdown() 如果筆停止返回True,反之返回False

顏色控制

color() 顏色,直接使用返回當前筆顏色與填充顏色
pencolor() 設定筆的顏色
fillcolor() 設定筆的填充顏色

填充

filling() 返回填充狀態,
begin_fill() 在填充之前使用
end_fill() 結束填充

更多繪畫控制

reset() 重置所有引數
clear() 刪除繪畫,與reset不同之處僅僅是刪除圖形,引數保留
write() 寫文字

arg – object to be written to the TurtleScreen   寫到TurtleScreen的引數
move – True/False     移動
align – one of the strings “left”, “center” or right”    對齊引數3選1(left,right,center)
font – a triple (fontname, fontsize, fonttype)  字型

烏龜狀態

可視性

showturtle() | st() 顯示烏龜的形狀
hideturtle() | ht() 隱藏烏龜的形狀
isvisible() 是否可見,返回True or False

外表

shape() 設定烏龜的圖形形狀,可選( “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”)
resizemode() 大小調整模式

“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), 由拉伸引數決定
“noresize”: no adaption of the turtle’s appearance takes place.                                              不調整

shapesize() | turtlesize() 返回筆的屬性。
shearfactor() 設定或者返回當前剪下因子
settiltangle() 與tilt() 一樣,只是可以為空,則返回當前旋轉角度
tiltangle() 棄用
tilt() 設定當前烏龜角度,不調整烏龜前進方向(僅僅改變烏龜樣子)
shapetransform() 設定或返回烏龜的形狀的當前轉換矩陣
get_shapepoly() 返回當前形狀的座標

監聽動作

onclick() 滑鼠點選事件

fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas函式需要有兩個引數
num – number of the mouse-button, defaults to 1 (left mouse button)    單擊次數,預設1
add – True or False – if True, a new binding will be added, otherwise it will replace a former binding  新增新的繫結函式,否則替代之前函式
例子:def turn(x, y):
     。。。 left(180)
onclick(turn)

onrelease() 滑鼠釋放事件,同上

ondrag() 滑鼠移動事件,同上

烏龜一些特殊方法
begin_poly() 開始記錄多邊形的頂點,當前點為起始點
end_poly() 結束記錄多邊形的頂點,當前點為起始點
get_poly() 返回最後記錄的多邊形
clone() 複製一個一模一樣的烏龜
getturtle() | getpen() 獲取trutle物件本身
getscreen() 獲取畫布物件
setundobuffer() 設定或禁用中斷器
undobufferentries() 返回undobuffer中的條目數

畫布的方法

視窗控制

bgcolor() 設定或返回當前畫布的背景顏色
bgpic() 設定或返回當前畫布的背景圖片名稱
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() 全域性座標

llx – a number, x-coordinate of lower left corner of canvas          左下X座標
lly – a number, y-coordinate of lower left corner of canvas           左下X座標
urx – a number, x-coordinate of upper right corner of canvas      右下X座標
ury – a number, y-coordinate of upper right corner of canvas       右下X座標

動畫控制

delay() 動畫延遲(毫秒)引數:(integer )一個數字
tracer() 開啟動畫,設定延遲

n – nonnegative integer              n個動作執行一次
delay – nonnegative integer      延遲,毫秒

update() 更新畫布,當tracer關閉時使用

畫布監聽

listen() 開啟監聽,將滑鼠定位到畫布
onkey() | onkeyrelease() 鍵盤彈起(需要位於焦點上,使用上面listen後)

fun – a function with no arguments or None 動作函式
key – a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按鍵
onkeypress() 鍵盤按下事件,同上

onclick() | onscreenclick() 滑鼠點選事件

fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas 函式需要兩個引數
num – number of the mouse-button, defaults to 1 (left mouse button) 點選次數
add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 是否是新增,還是替換
ontimer() 計時器

fun – a function with no arguments 無需函式
t – a number >= 0 事件間隔
mainloop() | done() 開始事件迴圈,必須是烏龜繪畫中的最後一個函式

設定與特殊方法

mode() 繪圖模式,3選1 “standard”, “logo” or “world”
colormode() 顏色模式, 1.0 或者 255
getcanvas() 返回當前TurtleScreen.的Canvas
getshapes() 返回當前可用形狀
register_shape() | addshape() 3種呼叫方式。

1.直接呼叫圖片。screen.register_shape(“turtle.gif”)

2.呼叫形狀,制定點位置。

     screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))

3,呼叫形狀,名字隨便取
turtles() 返回烏龜list陣列
window_height() 返回視窗高度
window_width() 返回視窗寬度

輸入方法

textinput() 文字輸入

title – string 輸入名字
prompt – string 輸入的文字
numinput() 數字輸入

title – string 輸入名字
prompt – string 輸入文字
default – number (optional) 預設
minval – number (optional) 最小
maxval – number (optional) 最大

螢幕特有方法

bye() 關閉turtle視窗
exitonclick() 滑鼠點選關閉視窗
setup() 設定主視窗引數

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() 設定繪畫視窗標題

個人部落格 https://www.bobobk.com/39.html