上图:

直接上代码

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

python3迷宫,多线程版的更多相关文章

  1. python网络聊天器多线程版

    在之前的一篇文章(python网络编程-udp)中实现了一个简单的udp聊天器,只能在单线程下进行收发数据,在学习完多线程之后,实现一个能同时收发数据的udp聊天器. 说明: 编写一个有2个线程的程序 ...

  2. 【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)

    # nvshens按目录图片批量下载爬虫1.00(多线程版) from bs4 import BeautifulSoup import requests import datetime import ...

  3. Python3.11正式版,它来了!

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/b055fbf2.html 你好,我是测试蔡坨坨. 就在前几天,2022年10月24日,Python3.11正式版发布了! P ...

  4. Python3之多线程学习

    这里做一个自己复习多线程的笔记 Python中使用线程有两种方式:函数或者用类来包装线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: ...

  5. Python3.4 多线程

    线程安全和全局解释器锁 Thread State and the Global Interpreter Lock 总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步. Python ...

  6. 【Python3之多线程】

    一.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性. 1.开启线程的两种方式(同Process) 方法一 from thr ...

  7. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多线程版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...

  8. Python3用多线程替代for循环提升程序运行速度

    [本文出自天外归云的博客园] 优化前后新老代码如下: from git_tools.git_tool import get_collect_projects, QQNews_Git from thre ...

  9. python3.4多线程实现同步的四种方式

    临界资源即那些一次只能被一个线程访问的资源,典型例子就是打印机,它一次只能被一个程序用来执行打印功能,因为不能多个线程同时操作,而访问这部分资源的代码通常称之为临界区. 1. 锁机制 threadin ...

随机推荐

  1. day64 views文件

    from django.shortcuts import HttpResponse, render, redirect from app01 import models # Create your v ...

  2. php的注释方法

    注释是每个程序员学习时的基础,我们通过可以注释来备注一信息.增加代码的可读性.下面我们就为大家介绍一下PHP的注释方法. 1, // 这是单行注释 2,# 这也是单行注释 3,/* */多行注释块   ...

  3. 常用css代码(scss mixin)

    溢出显示省略号 参过参数可以只是单/多行. /** * 溢出省略号 * @param {Number} 行数 */ @mixin ellipsis($rowCount: 1) { @if $rowCo ...

  4. 46-python基础-python3-字符串-常用字符串方法(四)-join()-split()

    5-字符串方法 join()和 split() 1-join()方法 将字符串列表连接成一个单独的字符串. join()方法在一个字符串上调用,参数是一个字符串列表,返回一个字符串. 请注意,调用 j ...

  5. 领域驱动设计(DDD:Domain-Driven Design) 转摘自:http://www.jdon.com/ddd.html

    Eric Evans的“Domain-Driven Design领域驱动设计”简称DDD,Evans DDD是一套综合软件系统分析和设计的面向对象建模方法,本站Jdon.com是国内公开最早讨论DDD ...

  6. mySQL的表连接

    一.mysql表的连接方式 内连接和外连接的区别: 我把两个表比作集合A.B,其中,内连接是集合A和集合B的交集,而交集的内容在两个表中都存在,即在每一个表的内部:而外连接则是除了交集外,还有另一个表 ...

  7. opensns建站

    opensns建站 标签(空格分隔):软件工程 贪玩蓝月 购买云服务器 首选阿里云,一个月9.9元,还能直接安全宝塔Linux界面 后台初始化 输入ip地址:8888访问宝塔后台,第一次进入后台会让你 ...

  8. 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误

    一.远程服务器返回错误: (400) 错误的请求错误 捕获异常查看具体错误 using Newtonsoft.Json; using System; using System.Collections. ...

  9. 【Java学习笔记】百度面试问题回顾(一)

    今天回顾了部分百度面试时被问到的问题: 1.常见的包装类有哪些,他们与基本类型的区别,有哪些方法? Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很 ...

  10. python连接mongodb集群

    一 安装模块pymongo pip3 install pymongo 二 创建一个MongoClient conn=MongoClient('mongodb://cbi:pass@ip1:20000, ...