欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Python turtle.addshape 示例说明

最编程 2024-03-17 18:50:38
...

turtle 模块以面向对象和面向过程的方式提供 turtle 图形基元。由于它使用Tkinter作为基础图形,因此需要安装有Tk支持的Python版本。

turtle .addshape()

此函数用于将海龟形状添加到TurtleScreen的形状列表中。

用法:

turtle.addshape(name, shape=None)

参数:

参数 描述
name gif-file的名称
shape 形状是一对坐标的元组

下面是上述方法的实现和一些示例:



范例1:

Python3

# import package 
import turtle 
  
# print list of the all 
# shapes available 
print(turtle.getshapes()) 
  
# add shape to the shape list 
turtle.addshape(name="gfg.gif",shape=None) 
  
# check the updated shape list 
print(turtle.getshapes())

输出:

[‘arrow’, ‘blank’, ‘circle’, ‘classic’, ‘square’, ‘triangle’, ‘turtle’]
[‘arrow’, ‘blank’, ‘circle’, ‘classic’, ‘gfg.gif’, ‘square’, ‘triangle’, ‘turtle’]

范例2:

Python3

# import package 
import turtle 
  
# add shape to the shape list as above 
turtle.addshape(name="gfg.gif",shape=None) 
  
# set turtle with new shape 
# and new position 
turtle.shape("gfg.gif") 
turtle.up() 
turtle.setpos(-10,10) 
turtle.down() 
  
# loop for motion 
for i in range(22):
    turtle.fd(40+5*i) 
    turtle.right(90)

输出: