Как заставить одну черепаху следовать за другой бок о бок? - PullRequest
0 голосов
/ 05 октября 2019

Я просто пытаюсь сделать что-то очень простое в графике черепах, но мне сложно понять, как заставить одну черепаху оставаться рядом с другой черепахой, когда эта черепаха контролируется кем-то, использующим клавиатуру.

Я пытался найти это, но я не мог найти ответы. '' '

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 голосов
/ 05 октября 2019

В зависимости от того, что вы в конечном итоге пытаетесь сделать, ответ может быть таким простым:

 bob_laser.goto(bob.position())

всякий раз, когда вы перемещаете черепаху bob. Вот переделка вашего кода с использованием этого подхода:

from turtle import Screen, Turtle

def turtle_start(color, x_coordinate):
    turtle = Turtle()
    turtle.hideturtle()
    turtle.shape('square')
    turtle.shapesize(2)
    turtle.speed('fastest')
    turtle.setheading(90)
    turtle.penup()

    turtle.color(color)
    turtle.setx(x_coordinate)
    turtle.showturtle()

    return turtle

def fred_up():
    fred.forward(4)

def fred_down():
    fred.backward(4)

def bob_up():
    screen.onkeypress(None, 'Up')  # disable complex handler inside handler
    bob.forward(4)
    bob_laser.goto(bob.position())
    screen.onkeypress(bob_up, 'Up')  # reenable handler

def bob_down():
    screen.onkeypress(None, 'Down')
    bob.backward(4)
    bob_laser.goto(bob.position())
    screen.onkeypress(bob_down, 'Down')

screen = Screen()
screen.setup(1200, 600)
screen.bgcolor('black')

# Fred
fred = turtle_start('red', -500)

# Bob
bob = turtle_start('blue', 500)

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

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

screen.exitonclick()
...