Hi all:

Remember when I was having problems moving my .gif around my Tkinter maze?
Well, I was going about it all wrong (as some pointed out).  What REALLY
works is a graph.  I used a dictionary, the keys are main coordinates that
are a path that the .gif follows using a greedy algorithm that finds all the
paths to a determined location.  The definition of the keys are all the
possible directions the .gif can go.  Not all paths are covered on purpose.
In any case, here is the updated code, for those who asked me to post it.
I'm hoping to have it completed next week, without the gif of my dog (lol)
and a bad guy or two to follow the gif that will be used in the maze. 

The next thing I have to do is bind an event to the arrow keys that will
update the current x and y position.. I haven't quite worked that out yet.
Right now the gif moves through the maze based on the path.  Now the event
(an keyboard arrow) should tell it to go to that direction at the next turn.
The function that defines the move is called move_gif().  I could use some
ideas because I keep getting stuck on the details. How does the event effect
the current course through the maze? Does it somehow update the current call
to find_all_paths?  
Or some sort of Helper function that interrupts the current path at a vertex
giving it a new path?  That one seems most likely, but I'm still stuck on
how to stop at the vertex and move to the new location.  Any help would be
appreciated.

TYIA

T

<<attachment: DustY.gif>>

"""
Build the maze and pathway
"""
from Tkinter import *
import time

root = Tk()
root.title("Background")
canvasOne = Canvas(width = 800, height = 700, bg = 'black')
canvasOne.pack()

def createWall((x0, y0), (x1, y1), colour = 'blue', width = 3):
    """ create a double wall outlining the maze. """
    canvasOne.create_line(x0, y0, x1, y1, width = width, fill = colour)
    canvasOne.create_line(350, 630, 450, 630, width = 3, fill = 'gold', tag = 
'gate')

def buildPathHorizontal((val1, val2, number), increment = 20, delta = 5):
    """ build the  horizontal path through the maze, small white squares"""
    for i in range(number):
        val1 += increment
        x, y = val1, val2
        deltax = x + delta
        deltay = y + delta
        canvasOne.create_rectangle(x, y, deltax, deltay, fill = 'white')

def buildPathVertical((val1, val2, number), increment = 20, delta = 5):
    """ build the vertical path through the maze, small white squares"""
    for i in range(number):
        val2 += increment
        x, y = val1, val2
        deltax = x + delta
        deltay = y + delta
        canvasOne.create_rectangle(x, y, deltax, deltay, fill = 'white')
    
outerWall = [(450, 640), (475, 640), (475, 640), (475, 690), (475, 690), (790, 
690), (790, 690),
                 (790, 530), (790, 530), (660, 530), (660, 530), (660, 360), 
(790, 360), (790, 10), (790, 10),
                 (10, 10), (10, 10), (10, 360), (10, 360), (150, 360), (150, 
360), (150, 530), (150, 530),
                 (10, 530), (10, 530), (10, 690), (10, 690), (325, 690), (325, 
690), (325, 640), (325, 640),
                 (350, 640), (350, 630), (350, 630), (315, 630), (315, 680), 
(20, 680), (20, 680), (20, 560),
             (20, 540), (160, 540), (160, 540), (160, 350), (20, 350), (20, 
350), (20, 20), (20, 20),
             (380, 20), (380, 20), (380, 130), (380, 130), (420, 130), (420, 
130), (420, 20), (420, 20),
             (780, 20), (780, 350), (780, 350), (650, 350), (650, 350), (650, 
540), (650, 540), (780, 540),
             (780, 540), (780, 680), (780, 680), (485, 680), (485, 630), (485, 
630), (450, 630), (450, 630),
             (450, 640)]
topLeftBox = [(130, 105), (130, 125), (130, 125), (250, 125), (250, 125), (250, 
105),
                    (130, 105), (250, 105)]
secondTopLeftBox = [(160, 215), (160, 225), (160, 215), (230, 215), (230, 215),
                    (230, 225), (160, 225), (230, 225)]
topRightBox = [(545, 105), (545, 125), (545, 125), (665, 125), (665, 125), 
(665, 105), 
               (545, 105), (665, 105)]
secondTopRightBox = [(560, 215), (560, 225), (560, 215), (625, 215), (625, 
215), (625, 225),
                     (625, 225), (560, 225)]
middleT = [(345, 240), (455, 240), (345, 240), (345, 240), (345, 250), (395, 
250), (395, 250), (395, 335),
           (395, 335), (405, 335), (405, 335), (405, 250), (405, 250), (455, 
250), (455, 250), (455, 240)]
leftSidewaysT = [(275, 340), (275, 500), (275, 340), (285, 340), (285, 340), 
(285, 415), (285, 415), (345, 415),
                 (345, 415), (345, 425), (285, 425), (345, 425), (285, 425), 
(285, 500), (275, 500), (285, 500)]
rightSidewaysT = [(525, 340), (525, 500), (525, 340), (515, 340), (515, 340), 
(515, 415), (515, 415), (455, 415),
                  (455, 415), (455, 425), (515, 425), (455, 425), (515, 425), 
(515, 500), (515, 500), (525, 500)]

walls = [outerWall] 

for wall in walls: 
    for i in range(len(wall) - 1):
        createWall(outerWall[i], outerWall[i+1])    

boxes = [topLeftBox, secondTopLeftBox, topRightBox, secondTopRightBox]

for box in boxes:
    for i in range(len(box)-1):
        createWall(topLeftBox[i], topLeftBox[i+1]),
        createWall(secondTopLeftBox[i], secondTopLeftBox[i+1]),
        createWall(topRightBox[i], topRightBox[i+1]),
        createWall(secondTopRightBox[i], secondTopRightBox[i+1])

Ts = [middleT, leftSidewaysT, rightSidewaysT]

for t in Ts:
    for i in range(len(t)-1):
        createWall(middleT[i], middleT[i+1]),
        createWall(leftSidewaysT[i], leftSidewaysT[i+1]),
        createWall(rightSidewaysT[i], rightSidewaysT[i+1])


horizontalPath = [(40, 610, 9), (60, 290, 14), (60, 50, 12), (60, 170, 33), 
(440, 290, 1),
                       (440, 290, 14), (480, 50, 12), (320, 370, 6), (300, 510, 
9), (220, 570, 5), (460, 570, 5), (580, 610, 8),
                  (0,0,0)]
verticalPath = [(60, 30, 13), (220, 290, 15), (300, 50, 11), (500, 30, 13), 
(340, 290, 4), (340, 290, 4),
                (400, 370, 6), (320, 510, 3), (460, 290, 4), (480, 510, 3), 
(580, 290, 16), (720, 30, 13), (0,0,0)]

paths = [horizontalPath, verticalPath]

for path in paths:
    for i in range(len(path)-1):
        buildPathHorizontal(horizontalPath[i]),
        buildPathVertical(verticalPath[i])


image = 'DustY.gif'
photo = PhotoImage(file=image)
xGif = 100
yGif = 610
ph = canvasOne.create_image(xGif, yGif, image = photo)

graph = {(100, 610) : [(270, 610)],
         (270, 610) : [(270, 280), (270, 560), (100, 610)],
         (270, 560) : [(400, 550), (370, 560), (270, 280), (270, 610)],
         (270, 280) : [(110, 280), (300, 290), (380, 290), (270, 610)],
         (370, 560) : [(370, 500), (270, 280)],
         (370, 500) : [(450, 500), (370, 560)],
         (450, 500) : [(390, 300), (450, 370), (370, 500)],
         (450, 370) : [(390, 370), (450, 500)],
         (390, 370) : [(390, 290), (450, 370)],
         (390, 290) : [(350, 290), (390, 370)],
         (350, 290) : [(350, 170), (110, 280), (390, 290)],
         (350, 170) : [(110, 170), (350, 290), (350, 290)],
         (110, 280) : [(110, 170), (110, 40), (270, 280)],
         (110, 170) : [(350, 170)],
         (110, 40) : [(350, 40), (110, 170), (110, 280)],
         (350, 40) : [(350, 170), (110, 40)],
         (350, 170) : [(550, 170), (110, 170), (350, 40)],
         (550, 170) : [(550, 40), (550, 280), (350, 170), (750, 170)],
         (550, 40) : [(770, 40), (550, 40)],
         (550, 280) : [(510, 280), (520, 200), (520, 40)],
         (510, 280) : [(510, 360), (550, 280)],
         (510, 360) : [(450, 370), (510, 280)],
         (770, 40) : [(770, 280), (770, 170), (550, 40)],
         (770, 280) : [(630, 280), (550, 280), (470, 280), (770, 40)],
         (630, 280) : [(630, 610), (550, 280), (470, 280), (770, 280)],
         (630, 610) : [(790, 610), (630, 560), (630, 570), (630, 280)],
         (630, 560) : [(530, 560), (630, 280), (630, 610)],
         (530, 560) : [(530, 500), (630, 560)],
         (530, 500) : [(450, 500), (530, 370)],
         (450, 500) : [(450, 350), (530, 500)],
         (790, 610) : [(630, 610)]}

"""
find_all_paths is a greedy algorithm based function that will get the .gif 
moving on the
path. It returns the path for the .gif to follow.  
"""
def find_all_paths(graph, start, end, path=[]):
        path = path + [start]
        if start == end:
            return [path]
        if not graph.has_key(start):
            return []
        paths = []
        for node in graph[start]:
            if node not in path:
                newpaths = find_all_paths(graph, node, end, path)
                for newpath in newpaths:
                    paths.append(newpath)
        return paths

def move_gif(pic, x = xGif, y = yGif):
    endX = 790
    endY = 610
    path = find_all_paths(graph, (x, y), (endX, endY))
    #print path
    try:
        for i in path[0]:
            dest = list(i)
            newX = dest[0]
            newY = dest[1]
            if newX == x:
                while y != newY:
                    if y > newY:
                        y -= 2
                        canvasOne.coords(pic, x, y)
                        time.sleep(.02)
                        canvasOne.update()
                        path = find_all_paths(graph, (x, y), (endX, endY))
                    if y < newY:
                        y+=2
                        canvasOne.coords(pic, x, y)
                        time.sleep(.02)
                        canvasOne.update()
                        path = find_all_paths(graph, (x, y), (endX, endY))
            if newY == y:
                while x != newX:
                    if x > newX:
                        x -= 2
                        canvasOne.coords(pic, x, y)
                        time.sleep(.02)
                        canvasOne.update()
                        path = find_all_paths(graph, (x, y), (endX, endY))
                    if x < newX:
                        x += 2
                        canvasOne.coords(pic, x, y)
                        time.sleep(.02)
                        canvasOne.update()
                        path = find_all_paths(graph, (x, y), (endX, endY))
    except IndexError:
        print 'path: ' , path
        print "out of range"
        
move_gif(ph)        
root.mainloop()    
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to