Python中的 turtle.Screen().turtles()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
turtle.Screen().turtles()
此函数用于返回屏幕上的海龟列表。这不需要任何论据。
句法 :
turtle.Screen().turtles()
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# import package
import turtle
# make screen object
# and set size
sc = turtle.Screen()
sc.setup(400,300)
# make turtle object
t1=turtle.Turtle(shape='square')
# do some motion with properties
t1.color("red")
t1.circle(50)
# make another turtle object
t2=turtle.Turtle(shape='circle')
# do some motion with properties
t2.color("green")
t2.circle(40)
# get all turtle objects on screen
print(sc.turtles())
Python3
# import package
import turtle
# make screen object and set size
sc = turtle.Screen()
sc.setup(400, 300)
# make first turtle and do something
t1 = turtle.Turtle(shape='square')
t1.color("red")
t1.circle(50)
# make another turtle and do something
t2 = turtle.Turtle(shape='circle')
t2.color("green")
t2.circle(40)
# get all turtles object
turt = sc.turtles()
# use first turtle object
turt[0].circle(-40)
# use another turtle object
turt[1].circle(-50)
输出 :
[
示例 2:
Python3
# import package
import turtle
# make screen object and set size
sc = turtle.Screen()
sc.setup(400, 300)
# make first turtle and do something
t1 = turtle.Turtle(shape='square')
t1.color("red")
t1.circle(50)
# make another turtle and do something
t2 = turtle.Turtle(shape='circle')
t2.color("green")
t2.circle(40)
# get all turtles object
turt = sc.turtles()
# use first turtle object
turt[0].circle(-40)
# use another turtle object
turt[1].circle(-50)
输出 :