python3迷宫,多线程版
上图:
直接上代码
#!/usr/bin/python3
#coding=GB2312
import tkinter as tk
import threading
import time
import random
import sys class Cell():
def __init__(self, row, col):
self.row, self.col = row, col
self.top, self.right, self.bottom, self.left = True, True, True, True
self.visited = False
def __str__(self):
return 'row:{} col:{}--{} {} {} {}'.format( \
self.row, self.col, self.top, self.right, \
self.bottom, self.left)
def setVisited(self):
self.visited = True
def isVisited(self):
return self.visited class Maze(threading.Thread):
colCount = 50
rowCount = 50
winWidth = 700
winHeight = 700
beginOf = (0, 0)
endOf = (colCount - 1, rowCount - 1)
def __init__(self):
threading.Thread.__init__(self)
self.initData()
self.initUi() """
以下是ui界面方法
"""
def initUi(self):
self.ui = tk.Tk()
self.centeredDisplay()
self.cs = tk.Canvas(self.ui, bg = '#121a2a')
self.cs.pack(fill = tk.BOTH, expand = 1)
self.ui.bind('<Key-h>', self.hideCell)
self.ui.bind('<Key-Up>', self.up)
#self.updateUi() self.start()
def hideCell(self, event):
self.cs.delete('currend')
def up(self, event):
pass
def updateUi(self):
w = float(self.winWidth / self.colCount)
h = float(self.winHeight / self.rowCount)
for row in range(self.rowCount):
for col in range(self.colCount):
cell = self.cells[row][col]
tagtmp = 'wall%02d%02d' % (row, col)
if cell.top:
self.cs.create_line(\
(w * col, h * row), \
(w * (col + 1), h * row), \
width = 3, fill = 'yellow', tag = 'top' + tagtmp)
else:
self.cs.delete('top' + tagtmp)
if cell.right:
self.cs.create_line(\
(w * (col + 1), h * row), \
(w * (col + 1), h * (row + 1)), \
width = 3, fill = 'yellow', tag = 'right' + tagtmp)
else:
self.cs.delete('right' + tagtmp)
if cell.bottom:
self.cs.create_line(\
(w * (col + 1), h * (row + 1)), \
(w * col, h * (row + 1)), \
width = 3, fill = 'yellow', tag = 'bottom' + tagtmp)
else:
self.cs.delete('bottom' + tagtmp)
if cell.left:
self.cs.create_line(\
(w * col, h * (row + 1), \
(w * col, h * row)), \
width = 3, fill = 'yellow', tag = 'left' + tagtmp)
else:
self.cs.delete('left' + tagtmp) self.cs.create_rectangle((self.beginOf[0] * w + 3, self.beginOf[1] * h + 3), \
(self.beginOf[0] + 1) * w - 3, (self.beginOf[1] + 1) * h - 3, \
fill = '#b4532a', tag = 'begin')
self.cs.create_rectangle((self.endOf[0] * w + 3, self.endOf[1] * h + 3), \
(self.endOf[0] + 1) * w - 3, (self.endOf[1] + 1) * h - 3, \
fill = '#ff0000', tag = 'end')
self.cs.delete('currend')
self.cs.create_rectangle((self.currentCell.col * w + 10, self.currentCell.row * h + 10), \
(self.currentCell.col + 1) * w - 10, (self.currentCell.row + 1) * h - 10, \
fill = '#00ff00', tag = 'currend') self.cs.update() def centeredDisplay(self):
w = self.ui.winfo_screenwidth()
h = self.ui.winfo_screenheight()
self.ui.geometry('{}x{}+{}+{}'.format(\
self.winWidth, self.winHeight, \
int((w - self.winWidth)/2), \
int((h - self.winHeight)/2)))
self.ui.resizable(False, False)
self.ui.title('Maze by jianc') """
以是ui界面方法 以下是逻辑线程方法
"""
def initData(self):
self.cells = [[Cell(row, col) for col in range(self.colCount)] \
for row in range(self.rowCount)]
self.cellStack = [] self.currentCell = self.cells[self.beginOf[0]][self.beginOf[1]]
def delWall(self, cell, cell2):
if 1 == cell.row - cell2.row:
cell.top, cell2.bottom = False, False
elif -1 == cell.row - cell2.row:
cell.bottom, cell2.top = False, False
if 1 == cell.col - cell2.col:
cell.left, cell2.right = False, False
elif -1 == cell.col - cell2.col:
cell.right, cell2.left = False, False
def topCell(self, cell):
if 0 == cell.row:
return None
ret = self.cells[cell.row - 1][cell.col]
if ret.isVisited():
return None
return ret
def rightCell(self, cell):
if self.colCount - 1 == cell.col:
return None
ret = self.cells[cell.row][cell.col + 1]
if ret.isVisited():
return None
return ret
def bottomCell(self, cell):
if self.rowCount - 1 == cell.row:
return None
ret = self.cells[cell.row + 1][cell.col]
if ret.isVisited():
return None
return ret
def leftCell(self, cell):
if 0 == cell.col:
return None
ret = self.cells[cell.row][cell.col - 1]
if ret.isVisited():
return None
return ret def checkNeighbor(self):
curCell = self.currentCell
curCell.setVisited()
neighbor = [self.topCell(curCell), self.rightCell(curCell), \
self.bottomCell(curCell), self.leftCell(curCell)]
while None in neighbor:
neighbor.remove(None)
n = len(neighbor)
if 0 == n:
try:
self.currentCell = self.cellStack.pop()
if None == curCell:
return
#self.updateUi()
self.checkNeighbor()
return
except:
return
self.cellStack.append(self.currentCell)
self.currentCell = neighbor[random.randint(0, n - 1)] self.delWall(curCell, self.currentCell) #self.updateUi()
self.checkNeighbor() def run(self):
self.checkNeighbor()
self.updateUi()
print('thread finish')
"""
以上是逻辑线程方法
""" sys.setrecursionlimit(100000)
maze = Maze()
tk.mainloop()
python3迷宫,多线程版的更多相关文章
- python网络聊天器多线程版
在之前的一篇文章(python网络编程-udp)中实现了一个简单的udp聊天器,只能在单线程下进行收发数据,在学习完多线程之后,实现一个能同时收发数据的udp聊天器. 说明: 编写一个有2个线程的程序 ...
- 【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)
# nvshens按目录图片批量下载爬虫1.00(多线程版) from bs4 import BeautifulSoup import requests import datetime import ...
- Python3.11正式版,它来了!
转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/b055fbf2.html 你好,我是测试蔡坨坨. 就在前几天,2022年10月24日,Python3.11正式版发布了! P ...
- Python3之多线程学习
这里做一个自己复习多线程的笔记 Python中使用线程有两种方式:函数或者用类来包装线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: ...
- Python3.4 多线程
线程安全和全局解释器锁 Thread State and the Global Interpreter Lock 总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步. Python ...
- 【Python3之多线程】
一.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性. 1.开启线程的两种方式(同Process) 方法一 from thr ...
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多线程版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...
- Python3用多线程替代for循环提升程序运行速度
[本文出自天外归云的博客园] 优化前后新老代码如下: from git_tools.git_tool import get_collect_projects, QQNews_Git from thre ...
- python3.4多线程实现同步的四种方式
临界资源即那些一次只能被一个线程访问的资源,典型例子就是打印机,它一次只能被一个程序用来执行打印功能,因为不能多个线程同时操作,而访问这部分资源的代码通常称之为临界区. 1. 锁机制 threadin ...
随机推荐
- 嵌入式开发环境搭建(一) 虚拟机实现桥接Ethernet网口 并且通过WIFI进行NAT联网
背景: 目前手头上有一块JZ2440的板子,之前有搭建完整套开发环境,由于虚拟机故障需要从新搭建服务器端,故在此记录搭建步骤 环境: Ubuntu16.4 VMWare 12 先行条件: 先按照自定义 ...
- 【转】 Linux 的目录详解 (Linux基础一)
前言 转自: http://c.biancheng.net/view/2833.html 进行了一些提炼和修改. 学习 Linux,不仅限于学习各种命令,了解整个 Linux 文件系统的目录结构以及各 ...
- drf:筛选,序列化
1.基础 restful规范: - url:一般用名词 http://www.baidu.com/article (面向资源编程) - 根据请求方式的不同做不同操作:get,post,put,dele ...
- HTML块,含样式的标签
HTML块,含样式的标签 html块 div标签 块元素,表示一块内容,没有具体的语义. span标签 行内元素,表示一行中的一小段内容,没有具体的语义. 含样式和语义的标签 em标签 行内元素,表示 ...
- 重读ORB_SLAM之Tracking线程难点
1. 初始化 当获取第一帧图像与深度图后,首先设置第一帧位姿为4*4单位矩阵,然后为整个map添加关键帧与地图点.且更新地图点与关键帧的联系,例如地图点被哪个关键帧观测到,而此关键帧又包含哪些地图点. ...
- SpringMVC学习(11):表单标签
本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松. 一.首先我们先做一个简单了例子来对Spring MV ...
- java 多线程 线程安全及非线程安全的集合对象
一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读取完之后,其他线程才可以使用.防止出现数据不一致或 ...
- elasticsearch relevance score相关性评分的计算
一.多shard场景下relevance score不准确问题 1.问题描述: 多个shard下,如果每个shard包含指定搜索条件的document数量不均匀的情况下,会导致在某个shard上doc ...
- codelite配置信息
codelite下编译执行wxwidgets库需要修改链接库如下: 原来的c++ compiler配置-g;-O0;-Wall;$(shell wx-config --cflags --debug) ...
- Linux系统分辨率设置
linux 设置分辨率 如果你需要在linux上设置显示屏的分辨率,分两种情况:分辨率模式存在与分辨率模式不存在,具体如下. 1,分辨率模式已存在 1)如何查询是否存在: 图形界面:在System S ...