欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 用turtle實(shí)現(xiàn)用正方形畫(huà)圓的例子

 更新時(shí)間:2019年11月21日 10:15:32   作者:春江暮客  
今天小編就為大家分享一篇Python 用turtle實(shí)現(xiàn)用正方形畫(huà)圓的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

最近發(fā)現(xiàn)一個(gè)很有意思的畫(huà)圖的python庫(kù),叫做turtle,這里先說(shuō)下用turtle這個(gè)庫(kù)來(lái)實(shí)現(xiàn)用正方形畫(huà)圓的思路。

每次都用烏龜(turtle)來(lái)畫(huà)出一個(gè)正方形,然后通過(guò)旋轉(zhuǎn)3°后,繼續(xù)畫(huà)一樣的正方形,在通過(guò)120次循環(huán)后就實(shí)現(xiàn)了完整的圓,這里當(dāng)然也可以用其他的角度和次數(shù),只要能完成360度就可以了。

先看完成的圖形和代碼。

代碼如下:

import turtle

window = turtle.Screen() #設(shè)置好畫(huà)圖的基本參數(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)角度,也就決定了需要畫(huà)正方形的次數(shù)。

window.exitonclick()

代碼應(yīng)該很簡(jiǎn)單易懂,就不再說(shuō)了。turtle真的是非常強(qiáng)大的一個(gè)繪圖工具,可以繪制各種各樣有趣的圖形,詳情請(qǐng)看 turtle官方文檔,這里說(shuō)點(diǎn)基本的參數(shù)與用法吧。主要包括兩部分,烏龜與畫(huà)布。

烏龜方法

烏龜運(yùn)動(dòng)

烏龜移動(dòng)與繪畫(huà)

forward() | fd() 向前移動(dòng)指定的距離。參數(shù):(integer or float))一個(gè)數(shù)字

backward() | bk() | back() 向后移動(dòng)指定的距離。參數(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() 移動(dòng)到原點(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() 用顏色畫(huà)出一個(gè)直徑大小的圓點(diǎn)。參數(shù):(size,color)(一個(gè)大于1的整數(shù)_可None,顏色值)

stamp() 將當(dāng)前位置上的形狀復(fù)制到畫(huà)布上,返回stamp_id.可通過(guò)下方的clearstamp刪除

clearstamp() 刪除stamp()返回來(lái)的值,參數(shù):(stamp_id)stamp函數(shù)返回值

clearstamps() 刪除所有的stamp,默認(rèn)無(wú)參數(shù),刪除所有

undo() 撤銷(xiāo)上一步動(dòng)作

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è)置與測(cè)量

degrees() 設(shè)置整個(gè)圓的角度,最好不要?jiǎng)?。參?shù):(integer or float)一個(gè)整數(shù)
radians() 將角度測(cè)量單位設(shè)置為弧度。360度就是2π

畫(huà)筆控制

繪畫(huà)狀態(tài)

pendown() | pd() | down() 將筆落下放在圖上,移動(dòng)的時(shí)候?qū)?huì)繪圖
penup() | pu() | up() 將筆提起來(lái),移動(dòng)的時(shí)候?qū)⒉粫?huì)繪圖
pensize() | width() 設(shè)置線條的粗細(xì)。參數(shù):(width)一個(gè)正數(shù)

pen() 使用鍵值對(duì)設(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    繪畫(huà)速度(范圍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é)束填充

更多繪畫(huà)控制

reset() 重置所有參數(shù)
clear() 刪除繪畫(huà),與reset不同之處僅僅是刪除圖形,參數(shù)保留
write() 寫(xiě)文字

arg – object to be written to the TurtleScreen 寫(xiě)到TurtleScreen的參數(shù)
move – True/False  移動(dòng)
align – one of the strings “l(fā)eft”, “center” or right” 對(duì)齊參數(shù)3選1(left,right,center)
font – a triple (fontname, fontsize, fonttype) 字體

烏龜狀態(tài)

可視性

showturtle() | st() 顯示烏龜?shù)男螤?br /> hideturtle() | ht() 隱藏烏龜?shù)男螤?br /> isvisible() 是否可見(jiàn),返回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.   由畫(huà)筆大小決定(自動(dòng))
“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)聽(tīng)動(dòng)作

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)移動(dòng)事件,同上

烏龜一些特殊方法

begin_poly() 開(kāi)始記錄多邊形的頂點(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對(duì)象本身
getscreen() 獲取畫(huà)布對(duì)象
setundobuffer() 設(shè)置或禁用中斷器
undobufferentries() 返回undobuffer中的條目數(shù)

畫(huà)布的方法

窗口控制

bgcolor() 設(shè)置或返回當(dāng)前畫(huà)布的背景顏色
bgpic() 設(shè)置或返回當(dāng)前畫(huà)布的背景圖片名稱(chēng)
clear() | clearscreen() 清除圖形
reset() | resetscreen() 重置畫(huà)布

screensize() 畫(huà)布大小

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)

動(dòng)畫(huà)控制

delay() 動(dòng)畫(huà)延遲(毫秒)參數(shù):(integer )一個(gè)數(shù)字
tracer() 開(kāi)啟動(dòng)畫(huà),設(shè)置延遲

n – nonnegative integer    n個(gè)動(dòng)作執(zhí)行一次
delay – nonnegative integer  延遲,毫秒

update() 更新畫(huà)布,當(dāng)tracer關(guān)閉時(shí)使用

畫(huà)布監(jiān)聽(tīng)

listen() 開(kāi)啟監(jiān)聽(tīng),將鼠標(biāo)定位到畫(huà)布
onkey() | onkeyrelease() 鍵盤(pán)彈起(需要位于焦點(diǎn)上,使用上面listen后)

fun – a function with no arguments or None 動(dòng)作函數(shù)
key – a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按鍵
onkeypress() 鍵盤(pán)按下事件,同上

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 無(wú)需函數(shù)
t – a number >= 0 事件間隔
mainloop() | done() 開(kāi)始事件循環(huán),必須是烏龜繪畫(huà)中的最后一個(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 左邊開(kāi)始位置
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 右邊開(kāi)始位置
title() 設(shè)置繪畫(huà)窗口標(biāo)題

以上這篇Python 用turtle實(shí)現(xiàn)用正方形畫(huà)圓的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論