Python 水平移动蟒蛇龟而不改变方向
我正在用Python-3编写一个游戏,该游戏要求水平移动一个海龟对象(也称为侧向移动),而不改变我的方向。Python 水平移动蟒蛇龟而不改变方向,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我正在用Python-3编写一个游戏,该游戏要求水平移动一个海龟对象(也称为侧向移动),而不改变我的方向。 turtle.goto(x,y)或turtle.setx(x)turtle.sety(y)不起作用,因为我希望对象在移动时显示出来,就像你移动turtle.fd(距离)时一样 以下是我现在掌握的代码: import turtle turtle.speed('slowest') turtle.lt(90) turtle.fd(20) turtle.rt(90) 有了这个密码,海龟会转身,
turtle.goto(x,y)
或turtle.setx(x)turtle.sety(y)
不起作用,因为我希望对象在移动时显示出来,就像你移动turtle.fd(距离)
时一样
以下是我现在掌握的代码:
import turtle
turtle.speed('slowest')
turtle.lt(90)
turtle.fd(20)
turtle.rt(90)
有了这个密码,海龟会转身,向前移动,然后再回头。有没有一种方法可以让我不必转身就可以侧身移动
非常感谢!
欢迎任何意见
海龟。转到(x,y)或海龟。setx(x)海龟。sety(y)不会工作,因为我
希望对象在移动时显示吗
你的前提是错误的——乌龟在进行所有这些操作时都会出现:
import turtle
turtle.speed('slowest')
turtle.sety(turtle.ycor() + 100)
turtle.done()
这将垂直移动海龟,同时保持水平航向。它不传送,它与.forward()
但是,如果您有其他原因不使用goto()
、setx()
、sety()
等,并且希望使用forward()
、backward()
来代替,那么我们可以这样做。海龟光标具有倾斜的概念,允许它朝一个方向看,同时朝另一个方向移动:
import turtle
turtle.speed('slowest')
turtle.tracer(False) # hide the heading change ...
turtle.setheading(90)
turtle.settiltangle(-90) # ... until we can tilt it
turtle.tracer(True)
turtle.forward(100)
turtle.done()
我们可以使用的一种情况是一个太空入侵者式的游戏,海龟想面对窗口的顶部,但我们想使用向前()
和向后()
来控制它在屏幕上的左右移动:
""" Oversimplified Example """
from turtle import Turtle, Screen
screen = Screen()
turtle = Turtle('turtle', visible=False)
turtle.settiltangle(90)
turtle.penup()
turtle.showturtle()
screen.onkey(lambda: turtle.forward(10), "Right")
screen.onkey(lambda: turtle.backward(10), "Left")
screen.listen()
screen.mainloop()
乌龟以预定的速度移动到那个地点。如果没有指定速度,它可能看起来像乌龟跳跃。在执行
turtle.setx(a_number)
之前,执行turtle.speed('slowest')
,看看它是否解决了您的问题。