【发布时间】:2018-05-30 04:44:26
【问题描述】:
如何在海龟图形中告诉海龟朝向一个方向? 我想让乌龟无论原来的位置都转向并面向一个方向,我该如何实现?
【问题讨论】:
-
嗯,总是有Python documentation。
标签: python turtle-graphics python-turtle
如何在海龟图形中告诉海龟朝向一个方向? 我想让乌龟无论原来的位置都转向并面向一个方向,我该如何实现?
【问题讨论】:
标签: python turtle-graphics python-turtle
我认为 turtle.setheading() AKA seth() 是您正在寻找的功能,所以如果您希望它指向北方:
turtle.setheading(0)
或
turtle.setheading(90)
取决于您是处于“标准”模式还是“徽标”模式。
正如 cmets 中指出的,您可以找到此信息 here。
【讨论】:
这是我在游戏中使用的:
#Functions
def go_up():
head.direction="up"
def go_down():
head.direction="down"
def go_left():
head.direction="left"
def go_right():
head.direction="right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# Keyboard
win.listen()
win.onkeypress(go_up, "Up")
win.onkeypress(go_down, "Down")
win.onkeypress(go_left, "Left")
win.onkeypress(go_right, "Right")
【讨论】:
或者如果您计划将您的乌龟移动到某个地方 (x, y) 并且您想先将您的乌龟指向那里,您可以使用:
turtle.setheading(turtle.towards(x,y))
【讨论】:
setheading模式可用here(指向另一只海龟,将航向设置为另一只海龟的航向)。
您可以使用:
turtle.right(angle)
要么:
turtle.left(angle)。
希望这会有所帮助!
【讨论】:
setheading 命令可以做到这一点。
turtle.setheading(<degrees/radians>)
是你将如何使用它。在不更改设置的情况下,您将处于标准模式,所以
turtle.setheading(0)
会面对乌龟,
turtle.setheading(90)
会让乌龟朝上,
turtle.setheading(180)
会面向左边的乌龟,并且
turtle.setheading(270)
会让乌龟朝下。希望这会有所帮助!
【讨论】:
import turtle
angle = 50
turtle.tiltangle(angle)
【讨论】:
无论在哪里都可以转一个角度,你可以使用
turtle.setheading()
把你的角度(转弯方向)放在括号里。
【讨论】: