views:

137

answers:

3

Hello i have designed a maze and i want to draw a path between the cells as the 'person' moves from one cell to the next. So each time i move the cell a line is drawn Also i am using the graphics module

The graphics module is an object oriented library

Im importing

from graphics import*
from maze import*

my circle which is my cell

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())

this is my loop

 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

Now it says getX not defined every time i press a key is this because of p2???

This is the most important bits in the module for this part

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)
+1  A: 

You might try this from an interactive Python shell:

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

That should tell you what attributes Circle does have.

Fred Larson
ey fred that did help but i get a getx has no attribute but i type that in instead of help(graphics.Circle) but it cannot find it any help there?
dont worry i got it however still a problem
Also try `dir(graphics.Circle)`.
Xavier Ho
@snow-spur: In the line `p2 = Point(getX(), getY())`, I'm not sure what object `getX()` and `getY()` are supposed to be getting called on. Those are probably methods of `Point`, right? You can do a `help(graphics.Point)` to check. I think your problem is now in that line, anyway.
Fred Larson
yeh thanx fred i know its in that line ive found this in the module def getX(self): return self.x def getY(self): return self.y
A: 

I don't know how maze solves the puzzle, so I am going to assume it works like a generator, yielding the next move for the circle to make. Something to this effect:

while not this_maze.solved():
  next_position = this_maze.next()
  my_circle.move(next_position)

Then all you need to do is keep track of the current circle position and the previous circle 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

Obviously, changing this in something compatible with your framework is left up to you. dir(), help() and reading the libraries' source will all help you.

badp
+1  A: 

You're trying to use getX() and getY() as free-standing FUNCTIONS:

p2 = Point(getX(), getY())

Note that you're calling them as bare names, not qualified names -- therefore, as functions, not as methods.

And yet the docs you quote say they're methods -- therefore, they must be called as part of qualified names ("after a dot"...!-) and before the dot must be an instance of Point.

Presumably, therefore, you need p1.getX() and p1.getY() instead of the bare names you're using. p1.getX is a qualified name (i.e., one with a dot) and it means "method or attribute getX of object p1.

This is really super-elementary Python, and I recommend you first study the official Python tutorial or other even simpler introductory documents before you try making or modifying applications in Python.

Alex Martelli
ey alex yeh thanx i changed that bit but now i have another problem. There is no error however it does not draw
@snow, if you don't consider this answer (which helped you fix an error and you're thanking me for) worth at least an upvote, then clearly you're just too hard to please and I won't spend more time on this (it's not as if I need the rep, but upvoting good answers is such a crucial part of Stack Overflow's etiquette that simply ignoring it is just plain insulting;-).
Alex Martelli
ok i think i upvoted it
So where's any call to `.draw(win)` in your loop? If you still need more help give us a URL to the `graphics` module or at least its docs, since of course we have no clue about how it works except from deductions from what little you tell us.
Alex Martelli
i have just update the module bit and copied the most important bits in
Since you've accepted an answer, this means that you think this question is solved (of course if there are other questions you can always open other questions for them).
Alex Martelli