如何让海龟在海龟图形中面向一个方向? 我想让海龟转向并面向一个方向,无论它的原始位置在哪里,我该怎么做?
如何让海龟在海龟图形中面向一个方向? 我想让海龟转向并面向一个方向,无论它的原始位置在哪里,我该怎么做?
turtle.setheading()
,也可以写作seth()
,如果你想让它指向北方:turtle.setheading(0)
或者
turtle.setheading(90)
根据您所处的“标准”或“标志”模式不同,此设置将有所不同。
如评论中所指出,您可以在此处找到此信息。
#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))
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)