Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让一只海龟并排跟随另一只海龟?_Python_Turtle Graphics - Fatal编程技术网

Python 如何让一只海龟并排跟随另一只海龟?

Python 如何让一只海龟并排跟随另一只海龟?,python,turtle-graphics,Python,Turtle Graphics,我只是想在海龟图形中做一些非常简单的东西,但是我很难弄清楚如何让一只海龟和另一只海龟靠在一起,当那只海龟被人用键盘控制时 我试过查这个,但找不到任何答案。 ''' ''' 我试图让一只乌龟和另一只乌龟呆在一起,而那只乌龟被一个人控制。根据你最终要做的事情,答案可能很简单: bob_laser.goto(bob.position()) 无论何时移动海龟bob。下面是使用此方法对代码进行的返工: from turtle import Screen, Turtle def turtle_star

我只是想在海龟图形中做一些非常简单的东西,但是我很难弄清楚如何让一只海龟和另一只海龟靠在一起,当那只海龟被人用键盘控制时

我试过查这个,但找不到任何答案。 '''

'''


我试图让一只乌龟和另一只乌龟呆在一起,而那只乌龟被一个人控制。

根据你最终要做的事情,答案可能很简单:

 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()
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()