如何在Python中特别是在turtle中为不同的元素指定值?

huangapple go评论59阅读模式
英文:

How I can specify a value for various elements on Python specially in turtle

问题

以下是翻译好的代码部分:

我不知道如何在Python中为不同的操作设置相同的值

import turtle
screen = turtle.Screen()
screen.title("...")
screen.setup(width=450, height=450)

t1 = turtle.Turtle()
t2 = turtle.Turtle()
# 你可以看到我为各种事物写了相同的值

t1.hideturtle()
t2.hideturtle()
t3.hideturtle()

t1.speed(0)
t2.speed(0)
t3.speed(0)

t1.pu()
t2.pu()
t3.pu()

t1.showturtle()
t2.showturtle()
t3.showturtle()

turtle.mainloop()

我想知道是否有更清晰的编写方式
英文:

I don't know how I can set the same value for different actions in Python.

import turtle
screen = turtle.Screen()
screen.title("...")
screen.setup(width=450, height=450)

t1 = turtle.Turtle()
t2 = turtle.Turtle()
# You can see I wrote the same value for various things

t1.hideturtle()
t2.hideturtle()
t3.hideturtle()

t1.speed(0)
t2.speed(0)
t3.speed(0)

t1.pu()
t2.pu()
t3.pu()

t1.showturtle()
t2.showturtle()
t3.showturtle()

turtle.mainloop()

I would like to know if there is a cleaner way to write this.

答案1

得分: 1

一个方法是将乌龟存储在一个列表中,然后通过列表进行循环。类似这样的内容。

import turtle
screen = turtle.Screen()
screen.title("...")
screen.setup(width=450, height=450)

# 你也可以循环这部分,但由于你刚开始学习...
turtles = []
turtles.append(turtle.Turtle())
turtles.append(turtle.Turtle())
turtles.append(turtle.Turtle())

for t in turtles:
    t.hideturtle()
    t.speed(0)
    t.pu()
    t.showturtle()

turtle.mainloop()

如果你不需要再次访问乌龟:

# ---
number_of_turtles = 3  # 或者任何数量

for i in range(0, number_of_turtles):
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(0)
    t.pu()
    t.showturtle()
# ---
英文:

One method would be to store the turtles in a list, and then loop through the list. Something of this nature.

import turtle
screen = turtle.Screen()
screen.title("...")
screen.setup(width=450, height=450)

# You could loop this as well, but since you're just beginning...
turtles = []
turtles[0] = turtle.Turtle()
turtles[1] = turtle.Turtle()
turtles[2] = turtle.Turtle()

for t in turtles:
     t.hideturtle()
     t.speed(0)
     t.pu()
     t.showturtle()

turtle.mainloop()

If you didn't need to access the turtles again:

# ---
number_of_turtles = 3  # or however many

for i in range(0, number_of_turtles):
     t = turtle.Turtle()
     t.hideturtle()
     t.speed(0)
     t.pu()
     t.showturtle()
# ---

答案2

得分: 1

一种方法是将一个乌龟配置为(隐式或显式)原型,具有所有共同的特性。然后使用clone()来创建原型的副本,然后设置它们的各自属性:

from turtle import Screen, Turtle

screen = Screen()
screen.title("...")
screen.setup(width=450, height=450)

t1 = Turtle()
t1.hideturtle()
t1.speed('fastest')
t1.penup()

t2 = t1.clone()
t2.setx(100)

t3 = t1.clone()
t3.sety(100)

t1.showturtle()
t2.showturtle()
t3.showturtle()

screen.mainloop()
英文:

One approach is to configure one turtle as an (implicit or explicit) prototype with all the common features. Then use clone() to create copies of that prototype and then set their individual properties:

from turtle import Screen, Turtle

screen = Screen()
screen.title("...")
screen.setup(width=450, height=450)

t1 = Turtle()
t1.hideturtle()
t1.speed('fastest')
t1.penup()

t2 = t1.clone()
t2.setx(100)

t3 = t1.clone()
t3.sety(100)

t1.showturtle()
t2.showturtle()
t3.showturtle()

screen.mainloop()

huangapple
  • 本文由 发表于 2023年2月9日 01:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定