Python 乌龟摸迷宫的墙壁
Python Turtle Touching Walls Of Maze
我需要让 Python 中的海龟在碰到迷宫的墙壁时打印“碰撞”。这是我的代码:
maze=turtle.Turtle()
maze.speed(10)
maze.penup()
maze.goto(-12.5, -175)
maze.pendown()
maze.begin_fill()
maze.goto(-12.5, -100)
maze.goto(37.5, -100)
maze.goto(37.5, -75)
maze.goto(-112.5, -75)
maze.goto(-112.5, -37.5)
maze.goto(-125, -37.5)
maze.goto(-125, -87.5)
maze.goto(-87.5, -87.5)
maze.goto(-87.5, -137.5)
maze.goto(-75, -137.5)
maze.goto(-75, -100)
maze.goto(-50, -100)
maze.goto(-50, -162.5)
maze.goto(-150, -162.5)
maze.goto(-150, -137.5)
maze.goto(-112.5, -137.5)
maze.goto(-112.5, -112.5)
maze.goto(-162.5, -112.5)
maze.goto(-162.5, -87.5)
maze.goto(-150, -87.5)
maze.goto(-150, -37.5)
maze.goto(-162.5, -37.5)
maze.goto(-162.5, 12.5)
maze.goto(-137.5, 12.5)
maze.goto(-137.5, -12.5)
maze.goto(-112.5, -12.5)
maze.goto(-112.5, 12.5)
maze.goto(-87.5, 12.5)
maze.goto(-87.5, -50)
maze.goto(-62.5, -50)
maze.goto(-62.5, 25)
maze.goto(-162.5, 25)
maze.goto(-162.5, 137.5)
maze.goto(-112.5, 137.5)
maze.goto(-112.5, 162.5)
maze.goto(-87.5, 162.5)
maze.goto(-87.5, 137.5)
maze.goto(-25, 137.5)
maze.goto(-25, 87.5)
maze.goto(12.5, 87.5)
maze.goto(12.5, 112.5)
maze.goto(37.5, 112.5)
maze.goto(37.5, 87.5)
maze.goto(62.5, 87.5)
maze.goto(62.5, 125)
maze.goto(-12.5, 125)
maze.goto(-12.5, 175)
maze.goto(-175, 175)
maze.goto(-175, -175)
maze.goto(-12.5, -175)
maze.end_fill()
maze.penup()
maze.goto(12.5, -175)
maze.pendown()
maze.begin_fill()
maze.goto(12.5, -125)
maze.goto(87.5, -125)
maze.goto(87.5, -162.5)
maze.goto(150, -162.5)
maze.goto(150, -125)
maze.goto(162.5, -125)
maze.goto(162.5, -100)
maze.goto(125, -100)
maze.goto(125, -137.5)
maze.goto(112.5, -137.5)
maze.goto(112.5, -100)
maze.goto(62.5, -100)
maze.goto(62.5, -75)
maze.goto(150, -75)
maze.goto(150, -12.5)
maze.goto(162.5, -12.5)
maze.goto(162.5, 12.5)
maze.goto(125, 12.5)
maze.goto(125, 37.5)
maze.goto(150, 37.5)
maze.goto(150, 150)
maze.goto(125, 150)
maze.goto(125, 112.5)
maze.goto(100, 112.5)
maze.goto(100, 87.5)
maze.goto(125, 87.5)
maze.goto(125, 62.5)
maze.goto(100, 62.5)
maze.goto(100, 12.5)
maze.goto(37.5, 12.5)
maze.goto(37.5, -50)
maze.goto(-37.5, -50)
maze.goto(-37.5, -25)
maze.goto(25, -25)
maze.goto(25, 25)
maze.goto(0, 25)
maze.goto(0, 0)
maze.goto(-37.5, 0)
maze.goto(-37.5, 50)
maze.goto(-87.5, 50)
maze.goto(-87.5, 75)
maze.goto(-62.5, 75)
maze.goto(-62.5, 100)
maze.goto(-112.5, 100)
maze.goto(-112.5, 50)
maze.goto(-137.5, 50)
maze.goto(-137.5, 112.5)
maze.goto(-50, 112.5)
maze.goto(-50, 62.5)
maze.goto(12.5, 62.5)
maze.goto(12.5, 37.5)
maze.goto(37.5, 37.5)
maze.goto(37.5, 62.5)
maze.goto(87.5, 62.5)
maze.goto(87.5, 150)
maze.goto(12.5, 150)
maze.goto(12.5, 175)
maze.goto(175, 175)
maze.goto(175, -175)
maze.goto(12.5, -175)
maze.end_fill()
maze.penup()
maze.goto(62.5, -50)
maze.pendown()
maze.begin_fill()
maze.goto(125, -50)
maze.goto(125, -12.5)
maze.goto(62.5, -12.5)
maze.goto(62.5, -50)
maze.end_fill()
maze.ht()
myPen=turtle.Turtle()
myPen.penup()
myPen.goto(0,-180)
myPen.pendown()
myPen.color("#DB148E")
myPen.width(4)
myPen.left(90)
#Start of maze
myPen.forward(70)
myPen.right(90)
from turtle import Turtle, Screen
wn = Screen()
speed = 0.5
myPen.speed(10)
myPen.ht()
def travel():
myPen.forward(speed)
wn.ontimer(travel, 10)
wn.onkey(lambda: myPen.setheading(90), 'w')
wn.onkey(lambda: myPen.setheading(180), 'a')
wn.onkey(lambda: myPen.setheading(0), 'd')
wn.onkey(lambda: myPen.setheading(270), 's')
wn.listen()
travel()
wn.mainloop()
现在发生的事情是我可以用 W A S D 控制乌龟并绘制迷宫,但我只能越过迷宫而它什么也做不了。我在想这可以通过检测乌龟的位置或者检测乌龟的颜色来完成。谢谢!
通常情况下,我不会有好消息告诉你。但是,因为您将迷宫创建为一组三个封闭的多边形:
有一个相对简单的修复方法!首先,从 Geeks for Geeks article "How to check if a given point lies inside or outside a polygon?" 下载 Python 代码。将此代码保存到一个文件中,我将其命名为“poly_util.py”,但您可以随意命名。编辑此文件以删除代码:
# Driver code
if __name__ == '__main__':
以及它之后的所有内容。我们不需要这段代码,而且它在缩进方面有问题。接下来,编辑您自己的代码以导入此文件,并使用 begin_poly()
和 end_poly()
调用包装您的 begin_fill()
和 end_fill()
段,以便我们可以使用 [=16= 捕获多边形]:
from turtle import Screen, Turtle
from poly_util import is_inside_polygon
screen = Screen()
maze = Turtle()
maze.hideturtle()
maze.speed('fastest')
maze.penup()
maze.goto(-12.5, -175)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(-12.5, -100)
maze.goto(37.5, -100)
maze.goto(37.5, -75)
maze.goto(-112.5, -75)
maze.goto(-112.5, -37.5)
maze.goto(-125, -37.5)
maze.goto(-125, -87.5)
maze.goto(-87.5, -87.5)
maze.goto(-87.5, -137.5)
maze.goto(-75, -137.5)
maze.goto(-75, -100)
maze.goto(-50, -100)
maze.goto(-50, -162.5)
maze.goto(-150, -162.5)
maze.goto(-150, -137.5)
maze.goto(-112.5, -137.5)
maze.goto(-112.5, -112.5)
maze.goto(-162.5, -112.5)
maze.goto(-162.5, -87.5)
maze.goto(-150, -87.5)
maze.goto(-150, -37.5)
maze.goto(-162.5, -37.5)
maze.goto(-162.5, 12.5)
maze.goto(-137.5, 12.5)
maze.goto(-137.5, -12.5)
maze.goto(-112.5, -12.5)
maze.goto(-112.5, 12.5)
maze.goto(-87.5, 12.5)
maze.goto(-87.5, -50)
maze.goto(-62.5, -50)
maze.goto(-62.5, 25)
maze.goto(-162.5, 25)
maze.goto(-162.5, 137.5)
maze.goto(-112.5, 137.5)
maze.goto(-112.5, 162.5)
maze.goto(-87.5, 162.5)
maze.goto(-87.5, 137.5)
maze.goto(-25, 137.5)
maze.goto(-25, 87.5)
maze.goto(12.5, 87.5)
maze.goto(12.5, 112.5)
maze.goto(37.5, 112.5)
maze.goto(37.5, 87.5)
maze.goto(62.5, 87.5)
maze.goto(62.5, 125)
maze.goto(-12.5, 125)
maze.goto(-12.5, 175)
maze.goto(-175, 175)
maze.goto(-175, -175)
maze.goto(-12.5, -175)
maze.end_poly()
maze.end_fill()
poly_left = maze.get_poly()
maze.penup()
maze.goto(12.5, -175)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(12.5, -125)
maze.goto(87.5, -125)
maze.goto(87.5, -162.5)
maze.goto(150, -162.5)
maze.goto(150, -125)
maze.goto(162.5, -125)
maze.goto(162.5, -100)
maze.goto(125, -100)
maze.goto(125, -137.5)
maze.goto(112.5, -137.5)
maze.goto(112.5, -100)
maze.goto(62.5, -100)
maze.goto(62.5, -75)
maze.goto(150, -75)
maze.goto(150, -12.5)
maze.goto(162.5, -12.5)
maze.goto(162.5, 12.5)
maze.goto(125, 12.5)
maze.goto(125, 37.5)
maze.goto(150, 37.5)
maze.goto(150, 150)
maze.goto(125, 150)
maze.goto(125, 112.5)
maze.goto(100, 112.5)
maze.goto(100, 87.5)
maze.goto(125, 87.5)
maze.goto(125, 62.5)
maze.goto(100, 62.5)
maze.goto(100, 12.5)
maze.goto(37.5, 12.5)
maze.goto(37.5, -50)
maze.goto(-37.5, -50)
maze.goto(-37.5, -25)
maze.goto(25, -25)
maze.goto(25, 25)
maze.goto(0, 25)
maze.goto(0, 0)
maze.goto(-37.5, 0)
maze.goto(-37.5, 50)
maze.goto(-87.5, 50)
maze.goto(-87.5, 75)
maze.goto(-62.5, 75)
maze.goto(-62.5, 100)
maze.goto(-112.5, 100)
maze.goto(-112.5, 50)
maze.goto(-137.5, 50)
maze.goto(-137.5, 112.5)
maze.goto(-50, 112.5)
maze.goto(-50, 62.5)
maze.goto(12.5, 62.5)
maze.goto(12.5, 37.5)
maze.goto(37.5, 37.5)
maze.goto(37.5, 62.5)
maze.goto(87.5, 62.5)
maze.goto(87.5, 150)
maze.goto(12.5, 150)
maze.goto(12.5, 175)
maze.goto(175, 175)
maze.goto(175, -175)
maze.goto(12.5, -175)
maze.end_poly()
maze.end_fill()
poly_right = maze.get_poly()
maze.penup()
maze.goto(62.5, -50)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(125, -50)
maze.goto(125, -12.5)
maze.goto(62.5, -12.5)
maze.goto(62.5, -50)
maze.end_poly()
maze.end_fill()
poly_inner = maze.get_poly()
pen = Turtle()
pen.hideturtle()
pen.speed('fastest')
pen.color("#DB148E")
pen.width(4)
pen.penup()
pen.sety(-180)
pen.pendown()
pen.left(90)
pen.forward(70)
pen.right(90)
# Start of maze
distance = 0.5
def travel():
pen.forward(distance)
position = pen.position()
if is_inside_polygon(poly_left, position) or is_inside_polygon(poly_right, position) or is_inside_polygon(poly_inner, position):
pen.undo()
screen.ontimer(travel, 50)
screen.onkey(lambda: pen.setheading(90), 'w')
screen.onkey(lambda: pen.setheading(180), 'a')
screen.onkey(lambda: pen.setheading(0), 'd')
screen.onkey(lambda: pen.setheading(270), 's')
screen.listen()
travel()
screen.mainloop()
现在检测碰撞只是对三个多边形组件中的每一个组件调用 is_inside_polygon()
的问题,如上面的代码所示。笔将停在墙上,直到您将 and/or 转回以沿合法方向移动。代码可能需要一些微调,但因为您使用了多边形,所以对于通常比较困难的问题,这是一个相对容易的解决方法。
请确保将这个伟大的多边形库归功于 Vikas Chitturi。
我需要让 Python 中的海龟在碰到迷宫的墙壁时打印“碰撞”。这是我的代码:
maze=turtle.Turtle()
maze.speed(10)
maze.penup()
maze.goto(-12.5, -175)
maze.pendown()
maze.begin_fill()
maze.goto(-12.5, -100)
maze.goto(37.5, -100)
maze.goto(37.5, -75)
maze.goto(-112.5, -75)
maze.goto(-112.5, -37.5)
maze.goto(-125, -37.5)
maze.goto(-125, -87.5)
maze.goto(-87.5, -87.5)
maze.goto(-87.5, -137.5)
maze.goto(-75, -137.5)
maze.goto(-75, -100)
maze.goto(-50, -100)
maze.goto(-50, -162.5)
maze.goto(-150, -162.5)
maze.goto(-150, -137.5)
maze.goto(-112.5, -137.5)
maze.goto(-112.5, -112.5)
maze.goto(-162.5, -112.5)
maze.goto(-162.5, -87.5)
maze.goto(-150, -87.5)
maze.goto(-150, -37.5)
maze.goto(-162.5, -37.5)
maze.goto(-162.5, 12.5)
maze.goto(-137.5, 12.5)
maze.goto(-137.5, -12.5)
maze.goto(-112.5, -12.5)
maze.goto(-112.5, 12.5)
maze.goto(-87.5, 12.5)
maze.goto(-87.5, -50)
maze.goto(-62.5, -50)
maze.goto(-62.5, 25)
maze.goto(-162.5, 25)
maze.goto(-162.5, 137.5)
maze.goto(-112.5, 137.5)
maze.goto(-112.5, 162.5)
maze.goto(-87.5, 162.5)
maze.goto(-87.5, 137.5)
maze.goto(-25, 137.5)
maze.goto(-25, 87.5)
maze.goto(12.5, 87.5)
maze.goto(12.5, 112.5)
maze.goto(37.5, 112.5)
maze.goto(37.5, 87.5)
maze.goto(62.5, 87.5)
maze.goto(62.5, 125)
maze.goto(-12.5, 125)
maze.goto(-12.5, 175)
maze.goto(-175, 175)
maze.goto(-175, -175)
maze.goto(-12.5, -175)
maze.end_fill()
maze.penup()
maze.goto(12.5, -175)
maze.pendown()
maze.begin_fill()
maze.goto(12.5, -125)
maze.goto(87.5, -125)
maze.goto(87.5, -162.5)
maze.goto(150, -162.5)
maze.goto(150, -125)
maze.goto(162.5, -125)
maze.goto(162.5, -100)
maze.goto(125, -100)
maze.goto(125, -137.5)
maze.goto(112.5, -137.5)
maze.goto(112.5, -100)
maze.goto(62.5, -100)
maze.goto(62.5, -75)
maze.goto(150, -75)
maze.goto(150, -12.5)
maze.goto(162.5, -12.5)
maze.goto(162.5, 12.5)
maze.goto(125, 12.5)
maze.goto(125, 37.5)
maze.goto(150, 37.5)
maze.goto(150, 150)
maze.goto(125, 150)
maze.goto(125, 112.5)
maze.goto(100, 112.5)
maze.goto(100, 87.5)
maze.goto(125, 87.5)
maze.goto(125, 62.5)
maze.goto(100, 62.5)
maze.goto(100, 12.5)
maze.goto(37.5, 12.5)
maze.goto(37.5, -50)
maze.goto(-37.5, -50)
maze.goto(-37.5, -25)
maze.goto(25, -25)
maze.goto(25, 25)
maze.goto(0, 25)
maze.goto(0, 0)
maze.goto(-37.5, 0)
maze.goto(-37.5, 50)
maze.goto(-87.5, 50)
maze.goto(-87.5, 75)
maze.goto(-62.5, 75)
maze.goto(-62.5, 100)
maze.goto(-112.5, 100)
maze.goto(-112.5, 50)
maze.goto(-137.5, 50)
maze.goto(-137.5, 112.5)
maze.goto(-50, 112.5)
maze.goto(-50, 62.5)
maze.goto(12.5, 62.5)
maze.goto(12.5, 37.5)
maze.goto(37.5, 37.5)
maze.goto(37.5, 62.5)
maze.goto(87.5, 62.5)
maze.goto(87.5, 150)
maze.goto(12.5, 150)
maze.goto(12.5, 175)
maze.goto(175, 175)
maze.goto(175, -175)
maze.goto(12.5, -175)
maze.end_fill()
maze.penup()
maze.goto(62.5, -50)
maze.pendown()
maze.begin_fill()
maze.goto(125, -50)
maze.goto(125, -12.5)
maze.goto(62.5, -12.5)
maze.goto(62.5, -50)
maze.end_fill()
maze.ht()
myPen=turtle.Turtle()
myPen.penup()
myPen.goto(0,-180)
myPen.pendown()
myPen.color("#DB148E")
myPen.width(4)
myPen.left(90)
#Start of maze
myPen.forward(70)
myPen.right(90)
from turtle import Turtle, Screen
wn = Screen()
speed = 0.5
myPen.speed(10)
myPen.ht()
def travel():
myPen.forward(speed)
wn.ontimer(travel, 10)
wn.onkey(lambda: myPen.setheading(90), 'w')
wn.onkey(lambda: myPen.setheading(180), 'a')
wn.onkey(lambda: myPen.setheading(0), 'd')
wn.onkey(lambda: myPen.setheading(270), 's')
wn.listen()
travel()
wn.mainloop()
现在发生的事情是我可以用 W A S D 控制乌龟并绘制迷宫,但我只能越过迷宫而它什么也做不了。我在想这可以通过检测乌龟的位置或者检测乌龟的颜色来完成。谢谢!
通常情况下,我不会有好消息告诉你。但是,因为您将迷宫创建为一组三个封闭的多边形:
有一个相对简单的修复方法!首先,从 Geeks for Geeks article "How to check if a given point lies inside or outside a polygon?" 下载 Python 代码。将此代码保存到一个文件中,我将其命名为“poly_util.py”,但您可以随意命名。编辑此文件以删除代码:
# Driver code
if __name__ == '__main__':
以及它之后的所有内容。我们不需要这段代码,而且它在缩进方面有问题。接下来,编辑您自己的代码以导入此文件,并使用 begin_poly()
和 end_poly()
调用包装您的 begin_fill()
和 end_fill()
段,以便我们可以使用 [=16= 捕获多边形]:
from turtle import Screen, Turtle
from poly_util import is_inside_polygon
screen = Screen()
maze = Turtle()
maze.hideturtle()
maze.speed('fastest')
maze.penup()
maze.goto(-12.5, -175)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(-12.5, -100)
maze.goto(37.5, -100)
maze.goto(37.5, -75)
maze.goto(-112.5, -75)
maze.goto(-112.5, -37.5)
maze.goto(-125, -37.5)
maze.goto(-125, -87.5)
maze.goto(-87.5, -87.5)
maze.goto(-87.5, -137.5)
maze.goto(-75, -137.5)
maze.goto(-75, -100)
maze.goto(-50, -100)
maze.goto(-50, -162.5)
maze.goto(-150, -162.5)
maze.goto(-150, -137.5)
maze.goto(-112.5, -137.5)
maze.goto(-112.5, -112.5)
maze.goto(-162.5, -112.5)
maze.goto(-162.5, -87.5)
maze.goto(-150, -87.5)
maze.goto(-150, -37.5)
maze.goto(-162.5, -37.5)
maze.goto(-162.5, 12.5)
maze.goto(-137.5, 12.5)
maze.goto(-137.5, -12.5)
maze.goto(-112.5, -12.5)
maze.goto(-112.5, 12.5)
maze.goto(-87.5, 12.5)
maze.goto(-87.5, -50)
maze.goto(-62.5, -50)
maze.goto(-62.5, 25)
maze.goto(-162.5, 25)
maze.goto(-162.5, 137.5)
maze.goto(-112.5, 137.5)
maze.goto(-112.5, 162.5)
maze.goto(-87.5, 162.5)
maze.goto(-87.5, 137.5)
maze.goto(-25, 137.5)
maze.goto(-25, 87.5)
maze.goto(12.5, 87.5)
maze.goto(12.5, 112.5)
maze.goto(37.5, 112.5)
maze.goto(37.5, 87.5)
maze.goto(62.5, 87.5)
maze.goto(62.5, 125)
maze.goto(-12.5, 125)
maze.goto(-12.5, 175)
maze.goto(-175, 175)
maze.goto(-175, -175)
maze.goto(-12.5, -175)
maze.end_poly()
maze.end_fill()
poly_left = maze.get_poly()
maze.penup()
maze.goto(12.5, -175)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(12.5, -125)
maze.goto(87.5, -125)
maze.goto(87.5, -162.5)
maze.goto(150, -162.5)
maze.goto(150, -125)
maze.goto(162.5, -125)
maze.goto(162.5, -100)
maze.goto(125, -100)
maze.goto(125, -137.5)
maze.goto(112.5, -137.5)
maze.goto(112.5, -100)
maze.goto(62.5, -100)
maze.goto(62.5, -75)
maze.goto(150, -75)
maze.goto(150, -12.5)
maze.goto(162.5, -12.5)
maze.goto(162.5, 12.5)
maze.goto(125, 12.5)
maze.goto(125, 37.5)
maze.goto(150, 37.5)
maze.goto(150, 150)
maze.goto(125, 150)
maze.goto(125, 112.5)
maze.goto(100, 112.5)
maze.goto(100, 87.5)
maze.goto(125, 87.5)
maze.goto(125, 62.5)
maze.goto(100, 62.5)
maze.goto(100, 12.5)
maze.goto(37.5, 12.5)
maze.goto(37.5, -50)
maze.goto(-37.5, -50)
maze.goto(-37.5, -25)
maze.goto(25, -25)
maze.goto(25, 25)
maze.goto(0, 25)
maze.goto(0, 0)
maze.goto(-37.5, 0)
maze.goto(-37.5, 50)
maze.goto(-87.5, 50)
maze.goto(-87.5, 75)
maze.goto(-62.5, 75)
maze.goto(-62.5, 100)
maze.goto(-112.5, 100)
maze.goto(-112.5, 50)
maze.goto(-137.5, 50)
maze.goto(-137.5, 112.5)
maze.goto(-50, 112.5)
maze.goto(-50, 62.5)
maze.goto(12.5, 62.5)
maze.goto(12.5, 37.5)
maze.goto(37.5, 37.5)
maze.goto(37.5, 62.5)
maze.goto(87.5, 62.5)
maze.goto(87.5, 150)
maze.goto(12.5, 150)
maze.goto(12.5, 175)
maze.goto(175, 175)
maze.goto(175, -175)
maze.goto(12.5, -175)
maze.end_poly()
maze.end_fill()
poly_right = maze.get_poly()
maze.penup()
maze.goto(62.5, -50)
maze.pendown()
maze.begin_fill()
maze.begin_poly()
maze.goto(125, -50)
maze.goto(125, -12.5)
maze.goto(62.5, -12.5)
maze.goto(62.5, -50)
maze.end_poly()
maze.end_fill()
poly_inner = maze.get_poly()
pen = Turtle()
pen.hideturtle()
pen.speed('fastest')
pen.color("#DB148E")
pen.width(4)
pen.penup()
pen.sety(-180)
pen.pendown()
pen.left(90)
pen.forward(70)
pen.right(90)
# Start of maze
distance = 0.5
def travel():
pen.forward(distance)
position = pen.position()
if is_inside_polygon(poly_left, position) or is_inside_polygon(poly_right, position) or is_inside_polygon(poly_inner, position):
pen.undo()
screen.ontimer(travel, 50)
screen.onkey(lambda: pen.setheading(90), 'w')
screen.onkey(lambda: pen.setheading(180), 'a')
screen.onkey(lambda: pen.setheading(0), 'd')
screen.onkey(lambda: pen.setheading(270), 's')
screen.listen()
travel()
screen.mainloop()
现在检测碰撞只是对三个多边形组件中的每一个组件调用 is_inside_polygon()
的问题,如上面的代码所示。笔将停在墙上,直到您将 and/or 转回以沿合法方向移动。代码可能需要一些微调,但因为您使用了多边形,所以对于通常比较困难的问题,这是一个相对容易的解决方法。
请确保将这个伟大的多边形库归功于 Vikas Chitturi。