如何在迷宫求解应用中绘制“轨迹”
你好,我设计了一个迷宫,我想在“人”从一个单元格移动到另一个单元格时,画出一条路径。每次我移动单元格时,就会画一条线。同时,我在使用图形模块。
图形模块是一个面向对象的库。
我正在导入
from graphics import*
from maze import*
我的圆形代表我的单元格
center = Point(15, 15)
c = Circle(center, 12)
c.setFill('blue')
c.setOutline('yellow')
c.draw(win)
p1 = Point(c.getCenter().getX(), c.getCenter().getY())
这是我的循环
if mazez.blockedCount(cloc)> 2:
mazez.addDecoration(cloc, "grey")
mazez[cloc].deadend = True
c.move(-25, 0)
p2 = Point(p1.getX(), p1.getY())
line = graphics.Line(p1, p2)
cloc.col = cloc.col - 1
现在每次我按一个键时,它都说getX未定义,这是不是因为p2的问题??
这是这个模块中最重要的部分
def __init__(self, title="Graphics Window",
width=200, height=200, autoflush=True):
master = tk.Toplevel(_root)
master.protocol("WM_DELETE_WINDOW", self.close)
tk.Canvas.__init__(self, master, width=width, height=height)
self.master.title(title)
self.pack()
master.resizable(0,0)
self.foreground = "black"
self.items = []
self.mouseX = None
self.mouseY = None
self.bind("<Button-1>", self._onClick)
self.height = height
self.width = width
self.autoflush = autoflush
self._mouseCallback = None
self.trans = None
self.closed = False
master.lift()
if autoflush: _root.update()
def __checkOpen(self):
if self.closed:
raise GraphicsError("window is closed")
def setCoords(self, x1, y1, x2, y2):
"""Set coordinates of window to run from (x1,y1) in the
lower-left corner to (x2,y2) in the upper-right corner."""
self.trans = Transform(self.width, self.height, x1, y1, x2, y2)
def plot(self, x, y, color="black"):
"""Set pixel (x,y) to the given color"""
self.__checkOpen()
xs,ys = self.toScreen(x,y)
self.create_line(xs,ys,xs+1,ys, fill=color)
self.__autoflush()
def plotPixel(self, x, y, color="black"):
"""Set pixel raw (independent of window coordinates) pixel
(x,y) to color"""
self.__checkOpen()
self.create_line(x,y,x+1,y, fill=color)
self.__autoflush()
def draw(self, graphwin):
if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
if graphwin.isClosed(): raise GraphicsError("Can't draw to closed window")
self.canvas = graphwin
self.id = self._draw(graphwin, self.config)
if graphwin.autoflush:
_root.update()
def move(self, dx, dy):
"""move object dx units in x direction and dy units in y
direction"""
self._move(dx,dy)
canvas = self.canvas
if canvas and not canvas.isClosed():
trans = canvas.trans
if trans:
x = dx/ trans.xscale
y = -dy / trans.yscale
else:
x = dx
y = dy
self.canvas.move(self.id, x, y)
if canvas.autoflush:
_root.update()
class Point(GraphicsObject):
def __init__(self, x, y):
GraphicsObject.__init__(self, ["outline", "fill"])
self.setFill = self.setOutline
self.x = x
self.y = y
def _draw(self, canvas, options):
x,y = canvas.toScreen(self.x,self.y)
return canvas.create_rectangle(x,y,x+1,y+1,options)
def _move(self, dx, dy):
self.x = self.x + dx
self.y = self.y + dy
def clone(self):
other = Point(self.x,self.y)
other.config = self.config.copy()
return other
def getX(self): return self.x
def getY(self): return self.y
def __init__(self, p1, p2, options=["outline","width","fill"]):
GraphicsObject.__init__(self, options)
self.p1 = p1.clone()
self.p2 = p2.clone()
def _move(self, dx, dy):
self.p1.x = self.p1.x + dx
self.p1.y = self.p1.y + dy
self.p2.x = self.p2.x + dx
self.p2.y = self.p2.y + dy
def getP1(self): return self.p1.clone()
def getP2(self): return self.p2.clone()
def getCenter(self):
p1 = self.p1
p2 = self.p2
return Point((p1.x+p2.x)/2.0, (p1.y+p2.y)/2.0)
3 个回答
0
我不知道 maze
是怎么解决这个难题的,所以我假设它的工作方式像一个生成器,yield
出下一个圆圈要走的步骤。大概是这个意思:
while not this_maze.solved():
next_position = this_maze.next()
my_circle.move(next_position)
然后你只需要记录当前圆圈的位置和之前圆圈的位置。
prev_position = this_maze.starting_point
while not this_maze.solved():
next_position = this_maze.next()
my_circle.clear()
draw_trail(prev_position, next_position)
my_circle.draw_at(next_position)
prev_position = next_position
显然,把这个改成适合你使用的框架的方式就得靠你自己了。使用 dir()
、help()
以及查看库的源代码都会对你有帮助。
1
你正在尝试把 getX()
和 getY()
当作独立的函数来使用:
p2 = Point(getX(), getY())
注意,你是直接用它们的名字在调用,而不是加上其他东西的名字——所以,这里是作为函数在用,而不是作为方法。
然而,你引用的文档说它们是方法——所以,它们必须作为带有完整名字的调用(就是“点后面”的那种!)并且在点前面必须是一个 Point
的实例。
因此,你应该使用 p1.getX()
和 p1.getY()
,而不是你现在用的那些简单名字。 p1.getX
是一个完整的名字(也就是带点的那种),它的意思是“对象 p1
的方法或属性 getX
。”
这其实是非常基础的 Python 知识,我建议你先去学习一下 官方的 Python 教程,或者其他更简单的入门资料,然后再尝试制作或修改 Python 应用。
1
你可以在一个交互式的Python环境中试试这个:
>>> import graphics
>>> help(graphics.Circle)
这样你就能知道Circle这个东西有哪些属性了。