Python中的 turtle.clearstamp() 方法
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
海龟.clearstamp()
turtle.clearstamp() 方法用于删除所有或第一个/最后 n 个海龟的图章。此方法需要一个整数参数。因此,使用 id 制作的印章会被它清除。
Syntax : turtle.clearstamp(stampid)
Parameter:
stampid – an integer, must be return value of previous stamp() call.
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# import package
import turtle
# set turtle speed to slowest
# for better understandings
turtle.speed(1)
# motion with stamps
# and their ids
turtle.forward(50)
id1 = turtle.stamp()
turtle.forward(50)
id2 = turtle.stamp()
turtle.forward(50)
id3 = turtle.stamp()
# hide the turtle to
# clarify stamps
turtle.ht()
# clear the stamps
# of id : id1 and id3
turtle.clearstamp(id1)
turtle.clearstamp(id3)
Python3
# import package
import turtle
# list to store ids
ids = []
# loop to create motion
# with stamps
for i in range(12):
# motion
turtle.forward(50)
# stampid
id = turtle.stamp()
lst.append(id)
turtle.right(30)
# hide the turtle for
# better understandings
turtle.ht()
# loop for clear stamps with
# their ids using clearstamp
# half stamps are cleared
for i in range(len(lst)//2):
turtle.clearstamp(lst[i])
输出 :
示例 2:
Python3
# import package
import turtle
# list to store ids
ids = []
# loop to create motion
# with stamps
for i in range(12):
# motion
turtle.forward(50)
# stampid
id = turtle.stamp()
lst.append(id)
turtle.right(30)
# hide the turtle for
# better understandings
turtle.ht()
# loop for clear stamps with
# their ids using clearstamp
# half stamps are cleared
for i in range(len(lst)//2):
turtle.clearstamp(lst[i])
输出 :