turtle 模塊以麵向對象和麵向過程的方式提供 turtle 圖形基元。由於它使用Tkinter作為基礎圖形,因此需要安裝有Tk支持的Python版本。
turtle .clone()
turtle.clone()方法用於創建和返回具有相同位置,標題和 turtle 屬性的 turtle 克隆。此方法不需要任何參數。
用法:
turtle.clone()
下麵是上述方法的實現,並以兩種方式提供示例,以使您更好地理解:
範例1:沒有克隆
Python3
# import package
import turtle
# make first turtle object
tur1=turtle.Turtle()
# set turtle properties
tur1.width(5)
tur1.color("red")
# move it
tur1.circle(50)
# make another turtle object
tur2=turtle.Turtle()
# move it
tur2.circle(-50)
輸出:
範例2:與克隆
Python3
# import package
import turtle
# make first turtle object
tur1=turtle.Turtle()
# set turtle properties
tur1.width(5)
tur1.color("red")
# move it
tur1.circle(50)
# make another turtle object
tur2=turtle.Turtle()
# cloning the properties of
# first turtle object
tur2 = tur1.clone()
# move it
tur2.circle(-50)
輸出:
在這裏我們可以發現,在第一個輸出中,第二個 turtle (tur2)與第一個 turtle (tur1)沒有任何屬性。為此,我們必須再次設置這些屬性。但是turtle.clone()方法具有將一個 turtle 對象的屬性克隆到另一個 turtle 對象的優勢,正如我們在第二個輸出中看到的那樣。
相關用法
- Python Wand function()用法及代碼示例
- Python ord()用法及代碼示例
- Python int()用法及代碼示例
- Python dir()用法及代碼示例
- Python map()用法及代碼示例
- Python sum()用法及代碼示例
- Python cmp()用法及代碼示例
- Python str()用法及代碼示例
- Python tell()用法及代碼示例
- Python now()用法及代碼示例
- Python oct()用法及代碼示例
- Python hex()用法及代碼示例
- Python id()用法及代碼示例
- Python numpy.ma.where()用法及代碼示例
- Python property()用法及代碼示例
- Python type()用法及代碼示例
- Python range()用法及代碼示例
注:本文由純淨天空篩選整理自deepanshu_rustagi大神的英文原創作品 turtle.clone() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。