改变海龟1的位置改变海龟2的位置

2024-05-13 10:16:13 发布

您现在位置:Python中文网/ 问答频道 /正文

def func():
    print("T POSITION: ", t.pos()) # prints 100, 100
    t2.pencolor("black")
    t2.setpos(0,0)
    print("T POSITION: ", t.pos()) # Now, prints 0, 0
    print("T2 POISTION: ", t2.pos())

两者都有。t.pos()t2.pos()被设置为(0,0),即使我分别声明为全局变量t1和t2。你知道吗

t= turtle.getturtle()
t.setpos(100,100)
t2 = turtle.getturtle().

如果我只想把t2的位置改成0,0,我怎么能做到呢?你知道吗


Tags: posdefpositionprintsnowblackfuncprint
2条回答

简短回答:“不要使用getturtle()!”这不是你想要的功能。它用于访问singlulardefaultturtle,很少需要/使用。相反,使用Turtle()获得一个新海龟:

import turtle

def func():
    print("T1 POSITION: ", t1.pos())
    t2.setpos(0, 0)
    print("T1 POSITION: ", t1.pos())
    print("T2 POSITION: ", t2.pos())

t1 = turtle.Turtle()
t1.pencolor("red")
t1.setpos(100, 100)

t2 = turtle.Turtle()
t2.pencolor("green")

func()

t2.circle(100)

t2.clear()

turtle.done()

你不需要copy.copy()海龟。如果你想要一只全新的海龟,请使用Turtle()。如果你想要一个新的海龟就像一个现存的海龟,给它打电话.clone(),例如t3 = t1.clone()。你知道吗

你需要copy.copy这个t2

import turtle,copy
t= turtle.getturtle()
t.setpos(100,100)
t2 = copy.copy(turtle.getturtle())
def func():
    print("T POSITION: ", t.pos())
    t2.pencolor("black")
    t2.setpos(0,0)
    print("T POSITION: ", t.pos())
    print("T2 POISTION: ", t2.pos())
func()

现在你得到的结果是:

T POSITION:  (100.00,100.00)
T POSITION:  (100.00,100.00)
T2 POISTION:  (0.00,0.00)

否则:

>>> t==t2
True
>>> t is t2
True
>>> id(t)
333763277936
>>> id(t2)
333763277936
>>> id(t) == id(t2)
True
>>> 

它们是一样的东西!!!完全地!你知道吗

相关问题 更多 >