2010-06-12 91 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