日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:turtle—-海龜繪圖

turtle —- 海龜繪圖

源碼: Lib/turtle.py


概述

海龜繪圖很適合用來引導(dǎo)孩子學(xué)習(xí)編程。 最初來自于 Wally Feurzeig, Seymour Papert 和 Cynthia Solomon 于 1967 年所創(chuàng)造的 Logo 編程語言。

請想象繪圖區(qū)有一只機器海龜,起始位置在 x-y 平面的 (0, 0) 點。先執(zhí)行 import turtle,再執(zhí)行 turtle.forward(15),它將(在屏幕上)朝所面對的 x 軸正方向前進 15 像素,隨著它的移動畫出一條線段。再執(zhí)行 turtle.right(25),它將原地右轉(zhuǎn) 25 度。

Turtle star

使用海龜繪圖可以編寫重復(fù)執(zhí)行簡單動作的程序畫出精細(xì)復(fù)雜的形狀。

 
 
 
 
  1. from turtle import *
  2. color('red', 'yellow')
  3. begin_fill()
  4. while True:
  5. forward(200)
  6. left(170)
  7. if abs(pos()) < 1:
  8. break
  9. end_fill()
  10. done()

通過組合使用此類命令,可以輕松地繪制出精美的形狀和圖案。

turtle 模塊是基于 python 標(biāo)準(zhǔn)發(fā)行版 2.5 以來的同名模塊重新編寫并進行了功能擴展。

新模塊盡量保持了原模塊的特點,并且(幾乎)100%與其兼容。這就意味著初學(xué)編程者能夠以交互方式使用模塊的所有命令、類和方法——運行 IDLE 時注意加 -n 參數(shù)。

turtle 模塊提供面向?qū)ο蠛兔嫦蜻^程兩種形式的海龜繪圖基本組件。由于它使用 tkinter 實現(xiàn)基本圖形界面,因此需要安裝了 Tk 支持的 Python 版本。

面向?qū)ο蟮慕涌谥饕褂谩?+2”個類:

  1. TurtleScreen 類定義圖形窗口作為繪圖海龜?shù)倪\動場。它的構(gòu)造器需要一個 tkinter.Canvas 或 ScrolledCanvas 作為參數(shù)。應(yīng)在 turtle 作為某個程序的一部分的時候使用。

    Screen() 函數(shù)返回一個 TurtleScreen 子類的單例對象。此函數(shù)應(yīng)在 turtle 作為獨立繪圖工具時使用。作為一個單例對象,其所屬的類是不可被繼承的。

    TurtleScreen/Screen 的所有方法還存在對應(yīng)的函數(shù),即作為面向過程的接口組成部分。

  2. RawTurtle (別名: RawPen) 類定義海龜對象在 TurtleScreen 上繪圖。它的構(gòu)造器需要一個 Canvas, ScrolledCanvas 或 TurtleScreen 作為參數(shù),以指定 RawTurtle 對象在哪里繪圖。

    從 RawTurtle 派生出子類 Turtle (別名: Pen),該類對象在 Screen 實例上繪圖,如果實例不存在則會自動創(chuàng)建。

    RawTurtle/Turtle 的所有方法也存在對應(yīng)的函數(shù),即作為面向過程的接口組成部分。

過程式接口提供與 Screen 和 Turtle 類的方法相對應(yīng)的函數(shù)。函數(shù)名與對應(yīng)的方法名相同。當(dāng) Screen 類的方法對應(yīng)函數(shù)被調(diào)用時會自動創(chuàng)建一個 Screen 對象。當(dāng) Turtle 類的方法對應(yīng)函數(shù)被調(diào)用時會自動創(chuàng)建一個 (匿名的) Turtle 對象。

如果屏幕上需要有多個海龜,就必須使用面向?qū)ο蟮慕涌凇?/p>

備注

以下文檔給出了函數(shù)的參數(shù)列表。對于方法來說當(dāng)然還有額外的第一個參數(shù) self,這里省略了。

可用的 Turtle 和 Screen 方法概覽

Turtle 方法

海龜動作

  • 移動和繪制

    forward() | fd() 前進

    backward() | bk() | back() 后退

    right() | rt() 右轉(zhuǎn)

    left() | lt() 左轉(zhuǎn)

    goto() | setpos() | setposition() 前往/定位

    setx() 設(shè)置x坐標(biāo)

    sety() 設(shè)置y坐標(biāo)

    setheading() | seth() 設(shè)置朝向

    home() 返回原點

    circle() 畫圓

    dot() 畫點

    stamp() 印章

    clearstamp() 清除印章

    clearstamps() 清除多個印章

    undo() 撤消

    speed() 速度

    獲取海龜?shù)臓顟B(tài)

    position() | pos() 位置

    towards() 目標(biāo)方向

    xcor() x坐標(biāo)

    ycor() y坐標(biāo)

    heading() 朝向

    distance() 距離

    設(shè)置與度量單位

    degrees() 角度

    radians() 弧度

畫筆控制

  • 繪圖狀態(tài)

    pendown() | pd() | down() 畫筆落下

    penup() | pu() | up() 畫筆抬起

    pensize() | width() 畫筆粗細(xì)

    pen() 畫筆

    isdown() 畫筆是否落下

    顏色控制

    color() 顏色

    pencolor() 畫筆顏色

    fillcolor() 填充顏色

    填充

    filling() 是否填充

    begin_fill() 開始填充

    end_fill() 結(jié)束填充

    更多繪圖控制

    reset() 重置

    clear() 清空

    write() 書寫

海龜狀態(tài)

  • 可見性

    showturtle() | st() 顯示海龜

    hideturtle() | ht() 隱藏海龜

    isvisible() 是否可見

    外觀

    shape() 形狀

    resizemode() 大小調(diào)整模式

    shapesize() | turtlesize() 形狀大小

    shearfactor() 剪切因子

    settiltangle() 設(shè)置傾角

    tiltangle() 傾角

    tilt() 傾斜

    shapetransform() 變形

    get_shapepoly() 獲取形狀多邊形

使用事件

onclick() 當(dāng)鼠標(biāo)點擊

onrelease() 當(dāng)鼠標(biāo)釋放

ondrag() 當(dāng)鼠標(biāo)拖動

特殊海龜方法

begin_poly() 開始記錄多邊形

end_poly() 結(jié)束記錄多邊形

get_poly() 獲取多邊形

clone() 克隆

getturtle() | getpen() 獲取海龜畫筆

getscreen() 獲取屏幕

setundobuffer() 設(shè)置撤消緩沖區(qū)

undobufferentries() 撤消緩沖區(qū)條目數(shù)

TurtleScreen/Screen 方法

窗口控制

bgcolor() 背景顏色

bgpic() 背景圖片

clearscreen()

resetscreen()

screensize() 屏幕大小

setworldcoordinates() 設(shè)置世界坐標(biāo)系

動畫控制

delay() 延遲

tracer() 追蹤

update() 更新

使用屏幕事件

listen() 監(jiān)聽

onkey() | onkeyrelease() 當(dāng)鍵盤按下并釋放

onkeypress() 當(dāng)鍵盤按下

onclick() | onscreenclick() 當(dāng)點擊屏幕

ontimer() 當(dāng)達到定時

mainloop() | done() 主循環(huán)

設(shè)置與特殊方法

mode()

colormode() 顏色模式

getcanvas() 獲取畫布

getshapes() 獲取形狀

register_shape() | addshape() 添加形狀

turtles() 所有海龜

window_height() 窗口高度

window_width() 窗口寬度

輸入方法

textinput() 文本輸入

numinput() 數(shù)字輸入

Screen 專有方法

bye() 退出

exitonclick() 當(dāng)點擊時退出

setup() 設(shè)置

title() 標(biāo)題

RawTurtle/Turtle 方法和對應(yīng)函數(shù)

本節(jié)中的大部分示例都使用 Turtle 類的一個實例,命名為 turtle。

海龜動作

turtle.forward(distance)

turtle.fd(distance)

  • 參數(shù)

    distance — 一個數(shù)值 (整型或浮點型)

海龜前進 distance 指定的距離,方向為海龜?shù)某颉?/p>

 
 
 
 
  1. >>> turtle.position()
  2. (0.00,0.00)
  3. >>> turtle.forward(25)
  4. >>> turtle.position()
  5. (25.00,0.00)
  6. >>> turtle.forward(-75)
  7. >>> turtle.position()
  8. (-50.00,0.00)

turtle.back(distance)

turtle.bk(distance)

turtle.backward(distance)

  • 參數(shù)

    distance — 一個數(shù)值

海龜后退 distance 指定的距離,方向與海龜?shù)某蛳喾?。不改變海龜?shù)某颉?/p>

 
 
 
 
  1. >>> turtle.position()
  2. (0.00,0.00)
  3. >>> turtle.backward(30)
  4. >>> turtle.position()
  5. (-30.00,0.00)

turtle.right(angle)

turtle.rt(angle)

  • 參數(shù)

    angle — 一個數(shù)值 (整型或浮點型)

海龜右轉(zhuǎn) angle 個單位。(單位默認(rèn)為角度,但可通過 degrees() 和 radians() 函數(shù)改變設(shè)置。) 角度的正負(fù)由海龜模式確定,參見 mode()。

 
 
 
 
  1. >>> turtle.heading()
  2. 22.0
  3. >>> turtle.right(45)
  4. >>> turtle.heading()
  5. 337.0

turtle.left(angle)

turtle.lt(angle)

  • 參數(shù)

    angle — 一個數(shù)值 (整型或浮點型)

海龜左轉(zhuǎn) angle 個單位。(單位默認(rèn)為角度,但可通過 degrees() 和 radians() 函數(shù)改變設(shè)置。) 角度的正負(fù)由海龜模式確定,參見 mode()。

 
 
 
 
  1. >>> turtle.heading()
  2. 22.0
  3. >>> turtle.left(45)
  4. >>> turtle.heading()
  5. 67.0

turtle.goto(x, y=None)

turtle.setpos(x, y=None)

turtle.setposition(x, y=None)

  • 參數(shù)

    • x — 一個數(shù)值或數(shù)值對/向量

    • y — 一個數(shù)值或 None

如果 yNone,x 應(yīng)為一個表示坐標(biāo)的數(shù)值對或 Vec2D 類對象 (例如 pos() 返回的對象).

海龜移動到一個絕對坐標(biāo)。如果畫筆已落下將會畫線。不改變海龜?shù)某颉?/p>

 
 
 
 
  1. >>> tp = turtle.pos()
  2. >>> tp
  3. (0.00,0.00)
  4. >>> turtle.setpos(60,30)
  5. >>> turtle.pos()
  6. (60.00,30.00)
  7. >>> turtle.setpos((20,80))
  8. >>> turtle.pos()
  9. (20.00,80.00)
  10. >>> turtle.setpos(tp)
  11. >>> turtle.pos()
  12. (0.00,0.00)

turtle.setx(x)

  • 參數(shù)

    x — 一個數(shù)值 (整型或浮點型)

設(shè)置海龜?shù)臋M坐標(biāo)為 x,縱坐標(biāo)保持不變。

 
 
 
 
  1. >>> turtle.position()
  2. (0.00,240.00)
  3. >>> turtle.setx(10)
  4. >>> turtle.position()
  5. (10.00,240.00)

turtle.sety(y)

  • 參數(shù)

    y — 一個數(shù)值 (整型或浮點型)

設(shè)置海龜?shù)目v坐標(biāo)為 y,橫坐標(biāo)保持不變。

 
 
 
 
  1. >>> turtle.position()
  2. (0.00,40.00)
  3. >>> turtle.sety(-10)
  4. >>> turtle.position()
  5. (0.00,-10.00)

turtle.setheading(to_angle)

turtle.seth(to_angle)

  • 參數(shù)

    to_angle — 一個數(shù)值 (整型或浮點型)

設(shè)置海龜?shù)某驗?to_angle。以下是以角度表示的幾個常用方向:

標(biāo)準(zhǔn)模式

logo 模式

0 - 東

0 - 北

90 - 北

90 - 東

180 - 西

180 - 南

270 - 南

270 - 西

 
 
 
 
  1. >>> turtle.setheading(90)
  2. >>> turtle.heading()
  3. 90.0

turtle.home()

海龜移至初始坐標(biāo) (0,0),并設(shè)置朝向為初始方向 (由海龜模式確定,參見 mode())。

 
 
 
 
  1. >>> turtle.heading()
  2. 90.0
  3. >>> turtle.position()
  4. (0.00,-10.00)
  5. >>> turtle.home()
  6. >>> turtle.position()
  7. (0.00,0.00)
  8. >>> turtle.heading()
  9. 0.0

turtle.circle(radius, extent=None, steps=None)

  • 參數(shù)

    • radius — 一個數(shù)值

    • extent — 一個數(shù)值 (或 None)

    • steps — 一個整型數(shù) (或 None)

繪制一個 radius 指定半徑的圓。圓心在海龜左邊 radius 個單位;extent 為一個夾角,用來決定繪制圓的一部分。如未指定 extent\則繪制整個圓。如果 *extent 不是完整圓周,則以當(dāng)前畫筆位置為一個端點繪制圓弧。如果 radius 為正值則朝逆時針方向繪制圓弧,否則朝順時針方向。最終海龜?shù)某驎罁?jù) extent* 的值而改變。

圓實際是以其內(nèi)切正多邊形來近似表示的,其邊的數(shù)量由 steps 指定。如果未指定邊數(shù)則會自動確定。此方法也可用來繪制正多邊形。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.position()
  3. (0.00,0.00)
  4. >>> turtle.heading()
  5. 0.0
  6. >>> turtle.circle(50)
  7. >>> turtle.position()
  8. (-0.00,0.00)
  9. >>> turtle.heading()
  10. 0.0
  11. >>> turtle.circle(120, 180) # draw a semicircle
  12. >>> turtle.position()
  13. (0.00,240.00)
  14. >>> turtle.heading()
  15. 180.0

turtle.dot(size=None, \color*)

  • 參數(shù)

    • size — 一個整型數(shù) >= 1 (如果指定)

    • color — 一個顏色字符串或顏色數(shù)值元組

繪制一個直徑為 size,顏色為 color 的圓點。如果 size 未指定,則直徑取 pensize+4 和 2*pensize 中的較大值。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.dot()
  3. >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
  4. >>> turtle.position()
  5. (100.00,-0.00)
  6. >>> turtle.heading()
  7. 0.0

turtle.stamp()

在海龜當(dāng)前位置印制一個海龜形狀。返回該印章的 stamp_id,印章可以通過調(diào)用 clearstamp(stamp_id) 來刪除。

 
 
 
 
  1. >>> turtle.color("blue")
  2. >>> turtle.stamp()
  3. 11
  4. >>> turtle.fd(50)

turtle.clearstamp(stampid)

  • 參數(shù)

    stampid — 一個整型數(shù),必須是之前 stamp() 調(diào)用的返回值

刪除 stampid 指定的印章。

 
 
 
 
  1. >>> turtle.position()
  2. (150.00,-0.00)
  3. >>> turtle.color("blue")
  4. >>> astamp = turtle.stamp()
  5. >>> turtle.fd(50)
  6. >>> turtle.position()
  7. (200.00,-0.00)
  8. >>> turtle.clearstamp(astamp)
  9. >>> turtle.position()
  10. (200.00,-0.00)

turtle.clearstamps(n=None)

  • 參數(shù)

    n — 一個整型數(shù) (或 None)

刪除全部或前/后 n 個海龜印章。如果 nNone 則刪除全部印章,如果 n > 0 則刪除前 n 個印章,否則如果 n < 0 則刪除后 n 個印章。

 
 
 
 
  1. >>> for i in range(8):
  2. ... turtle.stamp(); turtle.fd(30)
  3. 13
  4. 14
  5. 15
  6. 16
  7. 17
  8. 18
  9. 19
  10. 20
  11. >>> turtle.clearstamps(2)
  12. >>> turtle.clearstamps(-2)
  13. >>> turtle.clearstamps()

turtle.undo()

撤消 (或連續(xù)撤消) 最近的一個 (或多個) 海龜動作。可撤消的次數(shù)由撤消緩沖區(qū)的大小決定。

 
 
 
 
  1. >>> for i in range(4):
  2. ... turtle.fd(50); turtle.lt(80)
  3. ...
  4. >>> for i in range(8):
  5. ... turtle.undo()

turtle.speed(speed=None)

  • 參數(shù)

    speed — 一個 0..10 范圍內(nèi)的整型數(shù)或速度字符串 (見下)

設(shè)置海龜移動的速度為 0..10 表示的整型數(shù)值。如未指定參數(shù)則返回當(dāng)前速度。

如果輸入數(shù)值大于 10 或小于 0.5 則速度設(shè)為 0。速度字符串與速度值的對應(yīng)關(guān)系如下:

  • “fastest”: 0 最快

  • “fast”: 10 快

  • “normal”: 6 正常

  • “slow”: 3 慢

  • “slowest”: 1 最慢

速度值從 1 到 10,畫線和海龜轉(zhuǎn)向的動畫效果逐級加快。

注意: speed = 0 表示 沒有 動畫效果。forward/back 將使海龜向前/向后跳躍,同樣的 left/right 將使海龜立即改變朝向。

 
 
 
 
  1. >>> turtle.speed()
  2. 3
  3. >>> turtle.speed('normal')
  4. >>> turtle.speed()
  5. 6
  6. >>> turtle.speed(9)
  7. >>> turtle.speed()
  8. 9

獲取海龜?shù)臓顟B(tài)

turtle.position()

turtle.pos()

返回海龜當(dāng)前的坐標(biāo) (x,y) (為 Vec2D 矢量類對象)。

 
 
 
 
  1. >>> turtle.pos()
  2. (440.00,-0.00)

turtle.towards(x, y=None)

  • 參數(shù)

    • x — 一個數(shù)值或數(shù)值對/矢量,或一個海龜實例

    • y — 一個數(shù)值——如果 x 是一個數(shù)值,否則為 None

返回從海龜位置到由 (x,y)、矢量或另一海龜所確定位置的連線的夾角。 此數(shù)值依賴于海龜?shù)某跏汲颍@又取決于 “standard”/“world” 或 “l(fā)ogo” 模式設(shè)置。

 
 
 
 
  1. >>> turtle.goto(10, 10)
  2. >>> turtle.towards(0,0)
  3. 225.0

turtle.xcor()

返回海龜?shù)?x 坐標(biāo)。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.left(50)
  3. >>> turtle.forward(100)
  4. >>> turtle.pos()
  5. (64.28,76.60)
  6. >>> print(round(turtle.xcor(), 5))
  7. 64.27876

turtle.ycor()

返回海龜?shù)?y 坐標(biāo)。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.left(60)
  3. >>> turtle.forward(100)
  4. >>> print(turtle.pos())
  5. (50.00,86.60)
  6. >>> print(round(turtle.ycor(), 5))
  7. 86.60254

turtle.heading()

返回海龜當(dāng)前的朝向 (數(shù)值依賴于海龜模式參見 mode())。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.left(67)
  3. >>> turtle.heading()
  4. 67.0

turtle.distance(x, y=None)

  • 參數(shù)

    • x — 一個數(shù)值或數(shù)值對/矢量,或一個海龜實例

    • y — 一個數(shù)值——如果 x 是一個數(shù)值,否則為 None

返回從海龜位置到由 (x,y),適量或另一海龜對應(yīng)位置的單位距離。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.distance(30,40)
  3. 50.0
  4. >>> turtle.distance((30,40))
  5. 50.0
  6. >>> joe = Turtle()
  7. >>> joe.forward(77)
  8. >>> turtle.distance(joe)
  9. 77.0

度量單位設(shè)置

turtle.degrees(fullcircle=360.0)

  • 參數(shù)

    fullcircle — 一個數(shù)值

設(shè)置角度的度量單位,即設(shè)置一個圓周為多少 “度”。默認(rèn)值為 360 度。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.left(90)
  3. >>> turtle.heading()
  4. 90.0
  5. Change angle measurement unit to grad (also known as gon,
  6. grade, or gradian and equals 1/100-th of the right angle.)
  7. >>> turtle.degrees(400.0)
  8. >>> turtle.heading()
  9. 100.0
  10. >>> turtle.degrees(360)
  11. >>> turtle.heading()
  12. 90.0

turtle.radians()

設(shè)置角度的度量單位為弧度。其值等于 degrees(2*math.pi)。

 
 
 
 
  1. >>> turtle.home()
  2. >>> turtle.left(90)
  3. >>> turtle.heading()
  4. 90.0
  5. >>> turtle.radians()
  6. >>> turtle.heading()
  7. 1.5707963267948966

畫筆控制

繪圖狀態(tài)

turtle.pendown()

turtle.pd()

turtle.down()

畫筆落下 — 移動時將畫線。

turtle.penup()

turtle.pu()

turtle.up()

畫筆抬起 — 移動時不畫線。

turtle.pensize(width=None)

turtle.width(width=None)

  • 參數(shù)

    width — 一個正數(shù)值

設(shè)置線條的粗細(xì)為 width 或返回該值。如果 resizemode 設(shè)為 “auto” 并且 turtleshape 為多邊形,該多邊形也以同樣組細(xì)的線條繪制。如未指定參數(shù),則返回當(dāng)前的 pensize。

 
 
 
 
  1. >>> turtle.pensize()
  2. 1
  3. >>> turtle.pensize(10) # from here on lines of width 10 are drawn

turtle.pen(pen=None, \*pendict*)

  • 參數(shù)

    • pen — 一個包含部分或全部下列鍵的字典

    • pendict — 一個或多個以下列鍵為關(guān)鍵字的關(guān)鍵字參數(shù)

返回或設(shè)置畫筆的屬性,以一個包含以下鍵值對的 “畫筆字典” 表示:

  • “shown”: True/False

  • “pendown”: True/False

  • “pencolor”: 顏色字符串或顏色元組

  • “fillcolor”: 顏色字符串或顏色元組

  • “pensize”: 正數(shù)值

  • “speed”: 0..10 范圍內(nèi)的數(shù)值

  • “resizemode”: “auto” 或 “user” 或 “noresize”

  • “stretchfactor”: (正數(shù)值, 正數(shù)值)

  • “outline”: 正數(shù)值

  • “tilt”: 數(shù)值

此字典可作為后續(xù)調(diào)用 pen() 時的參數(shù),以恢復(fù)之前的畫筆狀態(tài)。另外還可將這些屬性作為關(guān)鍵詞參數(shù)提交。使用此方式可以用一條語句設(shè)置畫筆的多個屬性。

 
 
 
 
  1. >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
  2. >>> sorted(turtle.pen().items())
  3. [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
  4. ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
  5. ('shearfactor', 0.0), ('shown', True), ('speed', 9),
  6. ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
  7. >>> penstate=turtle.pen()
  8. >>> turtle.color("yellow", "")
  9. >>> turtle.penup()
  10. >>> sorted(turtle.pen().items())[:3]
  11. [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
  12. >>> turtle.pen(penstate, fillcolor="green")
  13. >>> sorted(turtle.pen().items())[:3]
  14. [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]

turtle.isdown()

如果畫筆落下返回 True,如果畫筆抬起返回 False

 
 
 
 
  1. >>> turtle.penup()
  2. >>> turtle.isdown()
  3. False
  4. >>> turtle.pendown()
  5. >>> turtle.isdown()
  6. True

顏色控制

turtle.pencolor(\args*)

返回或設(shè)置畫筆顏色。

允許以下四種輸入格式:

  • pencolor()

    返回以顏色描述字符串或元組 (見示例) 表示的當(dāng)前畫筆顏色??捎米髌渌?color/pencolor/fillcolor 調(diào)用的輸入。

    pencolor(colorstring)

    設(shè)置畫筆顏色為 colorstring 指定的 Tk 顏色描述字符串,例如 "red"、"yellow""#33cc8c"。

    pencolor((r, g, b))

    設(shè)置畫筆顏色為以 r, g, b 元組表示的 RGB 顏色。r, g, b 的取值范圍應(yīng)為 0..colormode,colormode 的值為 1.0 或 255 (參見 colormode())。

    pencolor(r, g, b)

    設(shè)置畫筆顏色為以 r, g, b 表示的 RGB 顏色。r, g, b 的取值范圍應(yīng)為 0..colormode。

如果 turtleshape 為多邊形,該多邊形輪廓也以新設(shè)置的畫筆顏色繪制。

 
 
 
 
  1. >>> colormode()
  2. 1.0
  3. >>> turtle.pencolor()
  4. 'red'
  5. >>> turtle.pencolor("brown")
  6. >>> turtle.pencolor()
  7. 'brown'
  8. >>> tup = (0.2, 0.8, 0.55)
  9. >>> turtle.pencolor(tup)
  10. >>> turtle.pencolor()
  11. (0.2, 0.8, 0.5490196078431373)
  12. >>> colormode(255)
  13. >>> turtle.pencolor()
  14. (51.0, 204.0, 140.0)
  15. >>> turtle.pencolor('#32c18f')
  16. >>> turtle.pencolor()
  17. (50.0, 193.0, 143.0)

turtle.fillcolor(\args*)

返回或設(shè)置填充顏色。

允許以下四種輸入格式:

  • fillcolor()

    返回以顏色描述字符串或元組 (見示例) 表示的當(dāng)前填充顏色。可用作其他 color/pencolor/fillcolor 調(diào)用的輸入。

    fillcolor(colorstring)

    設(shè)置填充顏色為 colorstring 指定的 Tk 顏色描述字符串,例如 "red"、"yellow""#33cc8c"。

    fillcolor((r, g, b))

    設(shè)置填充顏色為以 r, g, b 元組表示的 RGB 顏色。r, g, b 的取值范圍應(yīng)為 0..colormode,colormode 的值為 1.0 或 255 (參見 colormode())。

    fillcolor(r, g, b)

    設(shè)置填充顏色為 r, g, b 表示的 RGB 顏色。r, g, b 的取值范圍應(yīng)為 0..colormode。

如果 turtleshape 為多邊形,該多邊形內(nèi)部也以新設(shè)置的填充顏色填充。

 
 
 
 
  1. >>> turtle.fillcolor("violet")
  2. >>> turtle.fillcolor()
  3. 'violet'
  4. >>> turtle.pencolor()
  5. (50.0, 193.0, 143.0)
  6. >>> turtle.fillcolor((50, 193, 143)) # Integers, not floats
  7. >>> turtle.fillcolor()
  8. (50.0, 193.0, 143.0)
  9. >>> turtle.fillcolor('#ffffff')
  10. >>> turtle.fillcolor()
  11. (255.0, 255.0, 255.0)

turtle.color(\args*)

返回或設(shè)置畫筆顏色和填充顏色。

允許多種輸入格式。使用如下 0 至 3 個參數(shù):

  • color()

    返回以一對顏色描述字符串或元組表示的當(dāng)前畫筆顏色和填充顏色,兩者可分別由 pencolor() 和 fillcolor() 返回。

    color(colorstring), color((r,g,b)), color(r,g,b)

    輸入格式與 pencolor() 相同,同時設(shè)置填充顏色和畫筆顏色為指定的值。

    color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))

    相當(dāng)于 pencolor(colorstring1)fillcolor(colorstring2),使用其他輸入格式的方法也與之類似。

如果 turtleshape 為多邊形,該多邊形輪廓與填充也使用新設(shè)置的顏色。

 
 
 
 
  1. >>> turtle.color("red", "green")
  2. >>> turtle.color()
  3. ('red', 'green')
  4. >>> color("#285078", "#a0c8f0")
  5. >>> color()
  6. ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))

另參見: Screen 方法 colormode()。

填充

turtle.filling()

返回填充狀態(tài) (填充為 True,否則為 False)。

 
 
 
 
  1. >>> turtle.begin_fill()
  2. >>> if turtle.filling():
  3. ... turtle.pensize(5)
  4. ... else:
  5. ... turtle.pensize(3)

turtle.begin_fill()

在繪制要填充的形狀之前調(diào)用。

turtle.end_fill()

填充上次調(diào)用 begin_fill() 之后繪制的形狀。

自相交多邊形或多個形狀間的重疊區(qū)域是否填充取決于操作系統(tǒng)的圖形引擎、重疊的類型以及重疊的層數(shù)。 例如上面的 Turtle 多芒星可能會全部填充為黃色,也可能會有一些白色區(qū)域。

 
 
 
 
  1. >>> turtle.color("black", "red")
  2. >>> turtle.begin_fill()
  3. >>> turtle.circle(80)
  4. >>> turtle.end_fill()

更多繪圖控制

turtle.reset()

從屏幕中刪除海龜?shù)睦L圖,海龜回到原點并設(shè)置所有變量為默認(rèn)值。

 
 
 
 
  1. >>> turtle.goto(0,-22)
  2. >>> turtle.left(100)
  3. >>> turtle.position()
  4. (0.00,-22.00)
  5. >>> turtle.heading()
  6. 100.0
  7. >>> turtle.reset()
  8. >>> turtle.position()
  9. (0.00,0.00)
  10. >>> turtle.heading()
  11. 0.0

turtle.clear()

從屏幕中刪除指定海龜?shù)睦L圖。不移動海龜。海龜?shù)臓顟B(tài)和位置以及其他海龜?shù)睦L圖不受影響。

turtle.write(arg, move=False, align=’left’, font=(‘Arial’, 8, ‘normal’))

  • 參數(shù)

    • arg — 要書寫到 TurtleScreen 的對象

    • move — True/False

    • align — 字符串 “l(fā)eft”, “center” 或 “right”

    • font — 一個三元組 (fontname, fontsize, fonttype)

基于 align (“l(fā)eft”, “center” 或 “right”) 并使用給定的字體將文本 —— arg 的字符串表示形式 —— 寫到當(dāng)前海龜位置。 如果 move 為真值,畫筆會移至文本的右下角。 默認(rèn)情況下 moveFalse。

 
 
 
 
  1. >>> turtle.write("Home = ", True, align="center")
  2. >>> turtle.write((0,0), True)

海龜狀態(tài)

可見性

turtle.hideturtle()

turtle.ht()

使海龜不可見。當(dāng)你繪制復(fù)雜圖形時這是個好主意,因為隱藏海龜可顯著加快繪制速度。

 
 
 
 
  1. >>> turtle.hideturtle()

turtle.showturtle()

turtle.st()

使海龜可見。

 
 
 
 
  1. >>> turtle.showturtle()

turtle.isvisible()

如果海龜顯示返回 True,如果海龜隱藏返回 False。

 
 
 
 
  1. >>> turtle.hideturtle()
  2. >>> turtle.isvisible()
  3. False
  4. >>> turtle.showturtle()
  5. >>> turtle.isvisible()
  6. True

外觀

turtle.shape(name=None)

  • 參數(shù)

    name — 一個有效的形狀名字符串

設(shè)置海龜形狀為 name 指定的形狀名,如未指定形狀名則返回當(dāng)前的形狀名。name 指定的形狀名應(yīng)存在于 TurtleScreen 的 shape 字典中。多邊形的形狀初始時有以下幾種: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”。要了解如何處理形狀請參看 Screen 方法 register_shape()。

 
 
 
 
  1. >>> turtle.shape()
  2. 'classic'
  3. >>> turtle.shape("turtle")
  4. >>> turtle.shape()
  5. 'turtle'

turtle.resizemode(rmode=None)

  • 參數(shù)

    rmode — 字符串 “auto”, “user”, “noresize” 其中之一

設(shè)置大小調(diào)整模式為以下值之一: “auto”, “user”, “noresize”。如未指定 rmode 則返回當(dāng)前的大小調(diào)整模式。不同的大小調(diào)整模式的效果如下:

  • “auto”: 根據(jù)畫筆粗細(xì)值調(diào)整海龜?shù)耐庥^。

  • “user”: 根據(jù)拉伸因子和輪廓寬度 (outline) 值調(diào)整海龜?shù)耐庥^,兩者是由 shapesize() 設(shè)置的。

  • “noresize”: 不調(diào)整海龜?shù)耐庥^大小。

resizemode("user") 會由 shapesize() 帶參數(shù)使用時被調(diào)用。

 
 
 
 
  1. >>> turtle.resizemode()
  2. 'noresize'
  3. >>> turtle.resizemode("auto")
  4. >>> turtle.resizemode()
  5. 'auto'

turtle.shapesize(stretch_wid=None, stretch_len=None, outline=None)

turtle.turtlesize(stretch_wid=None, stretch_len=None, outline=None)

  • 參數(shù)

    • stretch_wid — 正數(shù)值

    • stretch_len — 正數(shù)值

    • outline — 正數(shù)值

返回或設(shè)置畫筆的屬性 x/y-拉伸因子和/或輪廓。設(shè)置大小調(diào)整模式為 “user”。當(dāng)且僅當(dāng)大小調(diào)整模式設(shè)為 “user” 時海龜會基于其拉伸因子調(diào)整外觀: stretch_wid 為垂直于其朝向的寬度拉伸因子,stretch_len 為平等于其朝向的長度拉伸因子,決定形狀輪廓線的粗細(xì)。

 
 
 
 
  1. >>> turtle.shapesize()
  2. (1.0, 1.0, 1)
  3. >>> turtle.resizemode("user")
  4. >>> turtle.shapesize(5, 5, 12)
  5. >>> turtle.shapesize()
  6. (5, 5, 12)
  7. >>> turtle.shapesize(outline=8)
  8. >>> turtle.shapesize()
  9. (5, 5, 8)

turtle.shearfactor(shear=None)

  • 參數(shù)

    shear — 數(shù)值 (可選)

設(shè)置或返回當(dāng)前的剪切因子。根據(jù) share 指定的剪切因子即剪切角度的切線來剪切海龜形狀。 改變海龜?shù)某?(移動方向)。如未指定 shear 參數(shù): 返回當(dāng)前的剪切因子即剪切角度的切線,與海龜朝向平行的線條將被剪切。

 
 
 
 
  1. 標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:turtle—-海龜繪圖
    標(biāo)題來源:http://m.5511xx.com/article/cohogsg.html