2 回答

TA貢獻2019條經驗 獲得超9個贊
如果海龜在位置向量 (x, y) 處,并且您想將其移動,例如向右移動 3 次和向上移動 5 次,您只需將其添加到坐標中,因此海龜需要移動到(x + 3, y + 5)。幸運的是,turtle.Vec2D支持這樣的加法,你可以goto在向量上使用。您可以通過以下方式獲取當前位置turtle.pos()
import turtle
def goto_relative(dx, dy=None):
"""Moves the automatic global turtle by dx and dy (Or a given vector)"""
goto_relative_on_turtle(turtle, dx, dy)
def goto_relative_on_turtle(t, dx, dy=None):
"""Moves al turtle by dx and dy (Or a given vector)"""
if dy is None:
dx, dy = dx
t.goto(t.pos() + turtle.Vec2D(dx, dy))

TA貢獻1848條經驗 獲得超2個贊
除了@Artyer 的出色答案 (+1) 之外,不要忘記forward()
, backward()
, left()
,right()
它們都是相對位置操作,而不是絕對操作goto()
和setheading()
。使用提供的相關操作可能需要重新考慮您的圖形。
在緊要關頭,你總是可以做一些丑陋的事情,比如:
t.setx(t.xcor() + 10)
添加回答
舉報