abcdef

python3迷宫,多线程版

上图:

直接上代码

  1 #!/usr/bin/python3
  2 #coding=GB2312
  3 import tkinter as tk
  4 import threading
  5 import time
  6 import random
  7 import sys
  8 
  9 class Cell():
 10     def __init__(self, row, col):
 11         self.row, self.col = row, col 
 12         self.top, self.right, self.bottom, self.left = True, True, True, True
 13         self.visited = False
 14     def __str__(self):
 15         return 'row:{} col:{}--{} {} {} {}'.format( \
 16                 self.row, self.col, self.top, self.right, \
 17                 self.bottom, self.left)
 18     def setVisited(self):
 19         self.visited = True
 20     def isVisited(self):
 21         return self.visited
 22 
 23 class Maze(threading.Thread):
 24     colCount = 50
 25     rowCount = 50
 26     winWidth = 700
 27     winHeight = 700
 28     beginOf = (0, 0)
 29     endOf = (colCount - 1, rowCount - 1)
 30     def __init__(self):
 31         threading.Thread.__init__(self)
 32         self.initData()
 33         self.initUi()
 34 
 35 
 36     """
 37         以下是ui界面方法
 38     """
 39     def initUi(self):
 40         self.ui = tk.Tk()
 41         self.centeredDisplay()
 42         self.cs = tk.Canvas(self.ui, bg = '#121a2a')
 43         self.cs.pack(fill = tk.BOTH, expand = 1)
 44         self.ui.bind('<Key-h>', self.hideCell)
 45         self.ui.bind('<Key-Up>', self.up)
 46         #self.updateUi()
 47 
 48         self.start()
 49     def hideCell(self, event):
 50         self.cs.delete('currend')
 51     def up(self, event):
 52         pass
 53     def updateUi(self):
 54         w = float(self.winWidth / self.colCount)
 55         h = float(self.winHeight / self.rowCount)
 56         for row in range(self.rowCount):
 57             for col in range(self.colCount):
 58                 cell = self.cells[row][col]
 59                 tagtmp = 'wall%02d%02d' % (row, col)
 60                 if cell.top:
 61                     self.cs.create_line(\
 62                             (w * col, h * row), \
 63                             (w * (col + 1), h * row), \
 64                             width = 3, fill = 'yellow', tag = 'top' + tagtmp) 
 65                 else:
 66                     self.cs.delete('top' + tagtmp)
 67                 if cell.right:
 68                     self.cs.create_line(\
 69                             (w * (col + 1), h * row), \
 70                             (w * (col + 1), h * (row + 1)), \
 71                             width = 3, fill = 'yellow', tag = 'right' + tagtmp)
 72                 else:
 73                     self.cs.delete('right' + tagtmp)
 74                 if cell.bottom:
 75                     self.cs.create_line(\
 76                             (w * (col + 1), h * (row + 1)), \
 77                             (w * col, h * (row + 1)), \
 78                             width = 3, fill = 'yellow', tag = 'bottom' + tagtmp)
 79                 else:
 80                     self.cs.delete('bottom' + tagtmp)
 81                 if cell.left:
 82                     self.cs.create_line(\
 83                             (w * col, h * (row + 1), \
 84                             (w * col, h * row)), \
 85                             width = 3, fill = 'yellow', tag = 'left' + tagtmp)
 86                 else:
 87                     self.cs.delete('left' + tagtmp)
 88 
 89         self.cs.create_rectangle((self.beginOf[0] * w + 3, self.beginOf[1] * h + 3), \
 90                 (self.beginOf[0] + 1) * w - 3, (self.beginOf[1] + 1) * h - 3, \
 91                 fill = '#b4532a', tag = 'begin')
 92         self.cs.create_rectangle((self.endOf[0] * w + 3, self.endOf[1] * h + 3), \
 93                 (self.endOf[0] + 1) * w - 3, (self.endOf[1] + 1) * h - 3, \
 94                 fill = '#ff0000', tag = 'end')
 95         self.cs.delete('currend')
 96         self.cs.create_rectangle((self.currentCell.col * w + 10, self.currentCell.row * h + 10), \
 97                 (self.currentCell.col + 1) * w - 10, (self.currentCell.row + 1) * h - 10, \
 98                 fill = '#00ff00', tag = 'currend')
 99         
100         self.cs.update()    
101 
102     def centeredDisplay(self):
103         w = self.ui.winfo_screenwidth()
104         h = self.ui.winfo_screenheight()
105         self.ui.geometry('{}x{}+{}+{}'.format(\
106                 self.winWidth, self.winHeight, \
107                 int((w - self.winWidth)/2), \
108                 int((h - self.winHeight)/2)))
109         self.ui.resizable(False, False)
110         self.ui.title('Maze by jianc')
111 
112     """
113         以是ui界面方法
114 
115         以下是逻辑线程方法
116     """
117     def initData(self):
118         self.cells = [[Cell(row, col) for col in range(self.colCount)] \
119                 for row in range(self.rowCount)]
120         self.cellStack = []
121 
122         self.currentCell = self.cells[self.beginOf[0]][self.beginOf[1]]
123     def delWall(self, cell, cell2):
124         if 1 == cell.row - cell2.row:
125             cell.top, cell2.bottom = False, False
126         elif -1 == cell.row - cell2.row:
127             cell.bottom, cell2.top = False, False
128         if 1 == cell.col - cell2.col:
129             cell.left, cell2.right = False, False
130         elif -1 == cell.col - cell2.col:
131             cell.right, cell2.left = False, False
132     def topCell(self, cell):
133         if 0 == cell.row:
134             return None
135         ret = self.cells[cell.row - 1][cell.col]
136         if ret.isVisited():
137             return None
138         return ret
139     def rightCell(self, cell):
140         if self.colCount - 1 == cell.col:
141             return None
142         ret = self.cells[cell.row][cell.col + 1]
143         if ret.isVisited():
144             return None
145         return ret 
146     def bottomCell(self, cell):
147         if self.rowCount - 1 == cell.row:
148             return None
149         ret = self.cells[cell.row + 1][cell.col]
150         if ret.isVisited():
151             return None
152         return ret 
153     def leftCell(self, cell):
154         if 0 == cell.col:
155             return None
156         ret = self.cells[cell.row][cell.col - 1]
157         if ret.isVisited():
158             return None
159         return ret 
160 
161     def checkNeighbor(self):
162         curCell = self.currentCell
163         curCell.setVisited()
164         neighbor = [self.topCell(curCell), self.rightCell(curCell), \
165                 self.bottomCell(curCell), self.leftCell(curCell)]
166         while None in neighbor:
167             neighbor.remove(None)
168         n = len(neighbor)
169         if 0 == n:
170             try:
171                 self.currentCell = self.cellStack.pop()
172                 if None == curCell:
173                     return
174                 #self.updateUi()
175                 self.checkNeighbor()
176                 return
177             except:
178                 return
179         self.cellStack.append(self.currentCell)
180         self.currentCell = neighbor[random.randint(0, n - 1)]  
181         
182         self.delWall(curCell, self.currentCell)
183 
184         #self.updateUi()
185         self.checkNeighbor()
186 
187     def run(self):
188         self.checkNeighbor()
189         self.updateUi()
190         print('thread finish')
191     """
192         以上是逻辑线程方法
193     """
194 
195 sys.setrecursionlimit(100000)
196 maze = Maze()
197 tk.mainloop()

 

posted @ 2019-11-01 13:55  jianc  阅读(345)  评论(0编辑  收藏  举报