Python: 如何让海龟永远不会越线
Python: how to make a turtle never cross over a line
我正在尝试编写一个 python 程序,使海龟要么向前移动,向左转然后移动,要么向右转然后随机移动,而不会越过它自己先前绘制的线。它必须留在屏幕内并在完成最可能的移动后清除并重新启动。据我所知:
import turtle
positions=[]
while 'x':
count=132
for x in range (132):
count-=1
print(count)
move = randint(1,3)
if move == 1:
turtle.fd(50)
if move == 2:
turtle.rt(90)
turtle.fd(50)
if move == 3:
turtle.lt(90)
turtle.fd(50)
if turtle.xcor()>290:
turtle.rt(180)
turtle.fd(50)
if turtle.xcor()<-290:
turtle.rt(180)
turtle.fd(50)
if turtle.ycor()>240:
turtle.rt(180)
turtle.fd(50)
if turtle.ycor()<-240:
turtle.rt(180)
turtle.fd(50)
turtle.clear()
如何让它记住它的位置而不是越过它们?非常感谢!
下面是我尝试解决你的自主乌龟问题的尝试。我选择了一个 set()
来跟踪访问过的位置,但也将海龟强制放到一个网格上以确保只能访问一组有限的点。我不喜欢你在撞墙时做 180 度转弯的方法,因为这只会让你原路返回并失败——相反,我的乌龟试图避免撞墙。
我用一只隐形的海龟克隆体来"test the waters" 看一招好坏。如果没有好的动作,它会放弃、重置并重新开始。您必须关闭 window 才能终止程序:
import turtle
from random import shuffle
WIDTH, HEIGHT = 600, 500
INCREMENT = 50
TURTLE_WIDTH = 20
X, Y = 0, 1
def dot_round(x, base=INCREMENT):
return int(base * round(float(x) / base))
turtle.setup(WIDTH, HEIGHT)
turtle.shape("turtle")
while True:
positions = set()
while True:
position = (dot_round(turtle.xcor()), dot_round(turtle.ycor())) # coerce position to grid
if position in positions:
break # collision with line
positions.add(position)
turtle.setposition(position) # coerce turtle back onto our grid
moves = list(range(3))
shuffle(moves)
clone = None
for move in moves:
clone = turtle.clone() # use an invisible clone to test the waters
clone.hideturtle()
clone.penup()
if move == 1:
clone.right(90)
elif move == 2:
clone.left(90)
clone.forward(INCREMENT)
position = (dot_round(clone.xcor()), dot_round(clone.ycor()))
if position[X] <= TURTLE_WIDTH//2 - WIDTH//2 or position[X] > WIDTH//2 - TURTLE_WIDTH//2:
continue # avoid collision with wall
if position[Y] <= TURTLE_WIDTH//2 - HEIGHT//2 or position[Y] > HEIGHT//2 - TURTLE_WIDTH//2:
continue # avoid collision with wall
if position not in positions:
break
else: # no break
break # accept the inevitable, there's no good move
turtle.setheading(clone.heading())
turtle.forward(INCREMENT)
turtle.reset()
# close the turtle window to get out of this program
这只乌龟只向前看一步 -- 他很容易让自己陷入困境,无法摆脱困境。
希望这能给您一些设计自己的海龟自动机的想法。
我正在尝试编写一个 python 程序,使海龟要么向前移动,向左转然后移动,要么向右转然后随机移动,而不会越过它自己先前绘制的线。它必须留在屏幕内并在完成最可能的移动后清除并重新启动。据我所知:
import turtle
positions=[]
while 'x':
count=132
for x in range (132):
count-=1
print(count)
move = randint(1,3)
if move == 1:
turtle.fd(50)
if move == 2:
turtle.rt(90)
turtle.fd(50)
if move == 3:
turtle.lt(90)
turtle.fd(50)
if turtle.xcor()>290:
turtle.rt(180)
turtle.fd(50)
if turtle.xcor()<-290:
turtle.rt(180)
turtle.fd(50)
if turtle.ycor()>240:
turtle.rt(180)
turtle.fd(50)
if turtle.ycor()<-240:
turtle.rt(180)
turtle.fd(50)
turtle.clear()
如何让它记住它的位置而不是越过它们?非常感谢!
下面是我尝试解决你的自主乌龟问题的尝试。我选择了一个 set()
来跟踪访问过的位置,但也将海龟强制放到一个网格上以确保只能访问一组有限的点。我不喜欢你在撞墙时做 180 度转弯的方法,因为这只会让你原路返回并失败——相反,我的乌龟试图避免撞墙。
我用一只隐形的海龟克隆体来"test the waters" 看一招好坏。如果没有好的动作,它会放弃、重置并重新开始。您必须关闭 window 才能终止程序:
import turtle
from random import shuffle
WIDTH, HEIGHT = 600, 500
INCREMENT = 50
TURTLE_WIDTH = 20
X, Y = 0, 1
def dot_round(x, base=INCREMENT):
return int(base * round(float(x) / base))
turtle.setup(WIDTH, HEIGHT)
turtle.shape("turtle")
while True:
positions = set()
while True:
position = (dot_round(turtle.xcor()), dot_round(turtle.ycor())) # coerce position to grid
if position in positions:
break # collision with line
positions.add(position)
turtle.setposition(position) # coerce turtle back onto our grid
moves = list(range(3))
shuffle(moves)
clone = None
for move in moves:
clone = turtle.clone() # use an invisible clone to test the waters
clone.hideturtle()
clone.penup()
if move == 1:
clone.right(90)
elif move == 2:
clone.left(90)
clone.forward(INCREMENT)
position = (dot_round(clone.xcor()), dot_round(clone.ycor()))
if position[X] <= TURTLE_WIDTH//2 - WIDTH//2 or position[X] > WIDTH//2 - TURTLE_WIDTH//2:
continue # avoid collision with wall
if position[Y] <= TURTLE_WIDTH//2 - HEIGHT//2 or position[Y] > HEIGHT//2 - TURTLE_WIDTH//2:
continue # avoid collision with wall
if position not in positions:
break
else: # no break
break # accept the inevitable, there's no good move
turtle.setheading(clone.heading())
turtle.forward(INCREMENT)
turtle.reset()
# close the turtle window to get out of this program
这只乌龟只向前看一步 -- 他很容易让自己陷入困境,无法摆脱困境。
希望这能给您一些设计自己的海龟自动机的想法。