How to draw a “footprint” in an application for solving a maze

Hi, I designed the maze, and I want to draw a path between the cells as the “person” moves from one cell to another. Therefore, every time I move a cell, a line is drawn. I also use a graphics module.

The graphics module is an object-oriented library

Import

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 cycle

 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 he says that getX is not detected every time I press a key, is it because of p2 ???

These are 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)
+3
source share
3 answers

You can try this from the interactive Python shell:

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

, Circle.

+1

getX() getY() :

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

, , - .

, , ( " "...! -) , Point.

, p1.getX() p1.getY() , . p1.getX - (.. ), " getX p1.

Python, Python , Python.

+1

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

0

Source: https://habr.com/ru/post/1749702/


All Articles