废话不多说,上代码。
local x,y,tx,ty,dirx
dirx = 1
x = 0
y = 0
function onTarget()
print('x',x,'y',y)
print('tx',tx,'ty',ty)
if x ~= tx then return false end
if y ~= ty then return false end
return true
end
function change()
turtle.turnLeft()
turtle.turnLeft()
dirx = -dirx
end
function move(d)
if d == 0 then
while not onTarget() do
print('forward...')
if not turtle.detectDown() then
change()
while not turtle.down() do turtle.attack() end
y = y - 1
move(3)
elseif turtle.detect() then move(1)
else
while not turtle.forward() do turtle.attack() end
x = x + dirx
end
end
elseif d == 1 then
while not onTarget() do
print('up...')
if not turtle.detect() then
while not turtle.forward() do turtle.attack() end
x = x + dirx
move(0)
elseif turtle.detectUp() then
change()
move(2)
else
while not turtle.up() do turtle.attack() end
y = y + 1
end
end
elseif d == 2 then
while not onTarget() do
print('forward head...')
if not turtle.detectUp() then
change()
while not turtle.up() do turtle.attack() end
y = y + 1
move(1)
elseif turtle.detect() then move(3)
else
while not turtle.forward() do turtle.attack() end
x = x + dirx
end
end
elseif d == 3 then
while not onTarget() do
print('down')
if not turtle.detect() then
while not turtle.forward() do turtle.attack() end
x = x + dirx
move(2)
elseif turtle.detectDown() then
change()
move(0)
else
while not turtle.down() do turtle.attack() end
y = y - 1
end
end
end
end
tx = 5
ty = 12
move(0)