2010-06-12 68 views
3

你好,我设计了一个迷宫,当'人'从一个单元移动到另一个单元时,我想绘制一个单元之间的路径。 所以每次我移动至细胞线被画在 另外我使用图形模块如何在迷宫求解应用程序中绘制“轨迹”

图形模块是一个面向对象的库

林导入

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) 
+0

你正在使用什么图形库/框架?Circle,Point等,**不是**的一部分,所以你必须导入一些东西......并且你不告诉我们什么*! - ) – 2010-06-12 05:14:40

+0

它的一个图形模块 – user365084 2010-06-12 05:18:52

+0

哪个模块?你有链接到它的文档吗? – Soviut 2010-06-12 05:21:20

回答

1

你可能会从交互式Python外壳试试这个:

>>> import graphics 
>>> help(graphics.Circle) 

这应该告诉你什么属性圈子有。

+0

埃弗雷德,但没有帮助,但我得到一个getx没有任何属性,但我键入,而不是帮助(graphics.Circle),但它无法找到它 任何帮助吗? – user365084 2010-06-12 06:04:34

+0

不要担心我知道了但是仍然有问题 – user365084 2010-06-12 06:10:21

+0

也试试'dir(graphics.Circle)'。 – 2010-06-12 06:13:27

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,我建议你先研究official Python tutorial或其他更简单的介绍文件,然后再尝试在Python中创建或修改应用程序

+0

ey alex yeh thanx我改变了那一点,但现在我有另一个问题。没有错误,但它不会画出 – user365084 2010-06-13 04:07:19

+1

@snow,如果你不认为这个答案(帮助你修正一个错误,并且你感谢我)至少值得赞赏,那么显然你太难了我不会在这上面花费更多时间(这不是我需要代表,但是提出好的答案是Stack Overflow的礼节的一个关键部分,简单地忽略它就是纯粹的侮辱;-)。 – 2010-06-13 04:29:14

+0

好吧,我想我upvoted它 – user365084 2010-06-13 08:36:03