如何使一只乌龟并排跟随另一只?

时间:2019-10-05 05:16:33

标签: python turtle-graphics

我只是想使乌龟图形变得非常简单,但是当有人用键盘控制乌龟时,我很难弄清楚如何让一只乌龟紧挨着另一只乌龟。

我尝试查找此内容,但找不到任何答案。 '''

import turtle

wn = turtle.Screen()
wn.bgcolor("black")

def bob_start():
    bob.speed(0)
    bob.penup()
    bob.hideturtle()
    bob.goto(500,0)
    bob.showturtle()

def fred_start():
    fred.speed(0)
    fred.penup()
    fred.hideturtle()
    fred.goto(-500,0)
    fred.showturtle()

#Fred
fred = turtle.Turtle()
fred.color("red")
fred.shape("square")
fred.shapesize(stretch_len=2,stretch_wid=2)
fred_start()

#Bob
bob = turtle.Turtle()
bob.color("blue")
bob.shape("square")
bob.shapesize(stretch_len=2,stretch_wid=2)
bob_start()

#Bob Laser
bob_laser = turtle.Turtle()
bob_laser.shape("square")
bob_laser.shapesize(stretch_len=5,stretch_wid=0.5)
bob_laser.color("yellow")
bob_laser.penup()

def fred_up():
    y = fred.ycor()
    y += 4
    fred.sety(y)
def fred_down():
    y = fred.ycor()
    y -= 4
    fred.sety(y)
def bob_up():
    y = bob.ycor()
    y += 4
    bob.sety(y)
def bob_down():
    y = bob.ycor()
    y -= 4
    bob.sety(y)

# Key Bindings
turtle.listen()
turtle.onkeypress(fred_up, "w")
turtle.onkeypress(fred_down, "s")
turtle.onkeypress(bob_up, "Up")
turtle.onkeypress(bob_down, "Down")


wn.exitonclick()



while bob_laser.ycor != bob.ycor:
    bob_laser.penup()
    bob_laser.hideturtle()
    bob_laser.goto(500,bob.ycor)
    bob_laser.showturtle()

'''

我正在试图让一只乌龟在另一只乌龟被人控制的情况下留在另一只乌龟旁边。

1 个答案:

答案 0 :(得分:0)

取决于您最终想要做什么,答案可能很简单:

IServiceProvider sp;
public WeatherForecastService(IServiceProvider sp)
{
    this.sp = sp;
}

public void Stop()
{
    var ts = sp.GetService(typeof(TimedHostedService)) as TimedHostedService;
    ts.StopAsync(new CancellationToken());
}

每当您移动龟 bob_laser.goto(bob.position()) 时。这是使用这种方法对代码进行的重做:

bob