python3练习,做一个迷宫生成程序
直接上代码:
#!/usr/bin/python3
#coding=utf-8
import random
import tkinter as tk class Cell():
TOP = (0)
RIGHT = (1)
BOTTOM = (2)
LEFT = (3)
def __init__(self, x, y):
self.index = 0
self.x = x
self.y = y
self.walls = [True, True, True, True]
self.visited = False
def __str__(self):
return 'x:{}-y:{}, walls:{}'.format(self.x, self.y, self.walls)
def Walls(self):
return self.walls
def delTop(self):
self.walls[0] = False
def delRight(self):
self.walls[1] = False
def delBottom(self):
self.walls[2] = False
def delLeft(self):
self.walls[3] = False
def setWall(self, isWall, index):
if index not in [0, 1, 2, 3]:
return
self.walls[index] = isWall
def XY(self):
return [self.x, self.y]
def X(self):
return self.x
def Y(self):
return self.y
def isVisited(self):
return self.visited
def setVisited(self):
self.visited = True class Maze():
SIZE = (18)
START = (0, 0)
def __init__(self):
self.size = self.SIZE
self.bfsBuffer = []
self.cells = \
[[Cell(y, x) for x in range(self.SIZE)] \
for y in range(self.SIZE)] self.current = self.cells[self.START[0]][self.START[1]] def __str__(self):
info = ''
for rows in self.cells:
for cell in rows:
info += str(cell)
return info
def trblWalls(self, x, y):
return self.cells[x][y].Walls()
def visited(self, x, y):
return self.cells[x][y].isVisited()
def CurrentCell(self):
return self.current
def setCurrent(self, cell):
self.current = cell
def topCell(self):
if 0 == self.current.Y():
return None
return self.cells[self.current.X()][self.current.Y() - 1]
def rightCell(self):
if self.current.X() == (self.SIZE - 1):
return None
return self.cells[self.current.X() + 1][self.current.Y()]
def bottomCell(self):
if self.current.Y() == (self.SIZE - 1):
return None
return self.cells[self.current.X()][self.current.Y() + 1]
def leftCell(self):
if 0 == self.current.X():
return None
return self.cells[self.current.X() - 1][self.current.Y()]
def delWall(self, current, neighbor):
x = current.X()
y = current.Y()
x2 = neighbor.X()
y2 = neighbor.Y()
#print("({}x{}) and ({}x{})".format(x, y, x2, y2)) if (1 == (x - x2)):
current.delLeft()
neighbor.delRight()
elif (-1 == (x - x2)):
current.delRight()
neighbor.delLeft()
if (1 == (y - y2)):
current.delTop()
neighbor.delBottom()
elif (-1 == (y - y2)):
current.delBottom()
neighbor.delTop() def checkNeighbor(self):
neighbor = []
top = self.topCell()
right = self.rightCell()
bottom = self.bottomCell()
left = self.leftCell()
if (None != top and not top.isVisited()):
neighbor.append(self.topCell())
if (None != right and not right.isVisited()):
neighbor.append(self.rightCell())
if (None != bottom and not bottom.isVisited()):
neighbor.append(self.bottomCell())
if (None != left and not left.isVisited()):
neighbor.append(self.leftCell())
count = len(neighbor)
if 0 == count: if (len(self.bfsBuffer) == 0):
return
self.current = self.bfsBuffer.pop()
self.checkNeighbor() return old = self.current self.current = neighbor[random.randint(0, count - 1)] self.delWall(old, self.current)
#print('neighbor count:{} ->{}'.format(count, str(self.current)))
self.setUp() def setUp(self):
self.current.setVisited()
self.bfsBuffer.append(self.current)
self.checkNeighbor() class MazeUI():
winWidth = (600)
winHeight = (600)
def __init__(self):
self.maze = Maze()
self.ui = tk.Tk()
self.centeredDisplay() self.createMaze() self.ui.mainloop()
def centeredDisplay(self):
self.ui.title('Maze by jianc')
self.ui.geometry('{}x{}+{}+{}'.format( \
self.winWidth, self.winHeight, \
int((self.ui.winfo_screenwidth() - self.winWidth)/2), \
int((self.ui.winfo_screenheight() - self.winHeight)/2)))
self.ui.resizable(False, False)
def createMaze(self):
self.cs = tk.Canvas(self.ui, bg = '#5f3c23') self.cs.pack(side = tk.TOP, fill = 'both', expand=1, \
padx=0, ipadx=0, pady=0, ipady=0) self.maze.setUp() #print(self.maze)
self.drawCells() def drawCells(self):
w = float(self.winWidth / self.maze.SIZE)
h = float(self.winHeight / self.maze.SIZE)
current = self.maze.CurrentCell()
y = current.X()
x = current.Y()
self.cs.create_rectangle(y * w + 4, x * h + 4, (y + 1) * w - 4, (x + 1) * h - 4, \
fill='#ff0000', width = 0) for rows in range(self.maze.SIZE):
for cols in range(self.maze.SIZE):
top, right, bottom, left = self.maze.trblWalls(cols, rows)
#print("top:{} right:{} bottom:{} left:{}".format(top, right, bottom, left))
bVisited = self.maze.visited(rows, cols) """if bVisited:
self.cs.create_rectangle(rows * w + 10, cols * h + 10, \
(rows + 1) * w - 10, (cols+ 1) * h - 10, fill='#00ff00', width = 0)
"""
if top:
self.cs.create_line(cols * w, rows * h, \
(cols + 1) * w, rows * h, width=5)
if right:
self.cs.create_line((cols + 1) * w, rows * h, \
(cols + 1) * w, (rows + 1) * h, width=5)
if bottom:
self.cs.create_line((cols + 1) * w, (rows + 1) * h, \
cols * w, (rows + 1) * h, width=5)
if left:
self.cs.create_line(cols * w, (rows + 1) * h, \
cols * w, rows * h, width=5) current = self.maze.CurrentCell()
y = current.X()
x = current.Y()
self.cs.create_rectangle(y * w + 5, x * h + 5, (y + 1) * w - 5, (x + 1) * h - 5, \
fill='#ff0000', width = 0) maze = MazeUI()
python3练习,做一个迷宫生成程序的更多相关文章
- SLAM+语音机器人DIY系列:(三)感知与大脑——6.做一个能走路和对话的机器人
摘要 在我的想象中机器人首先应该能自由的走来走去,然后应该能流利的与主人对话.朝着这个理想,我准备设计一个能自由行走,并且可以与人语音对话的机器人.实现的关键是让机器人能通过传感器感知周围环境,并通过 ...
- 在树莓派上用 python 做一个炫酷的天气预报
教大家如何在树莓派上自己动手做一个天气预报.此次教程需要大家有一定的python 基础,没有也没关系,文末我会放出我已写好的代码供大家下载. 首先在开始之前 需要申请高德地图API,去高德地图官网注册 ...
- 自然语言处理NLP学习笔记三:使用Django做一个NLP的Web站点
前言: 前面我们已经能初步实现一个中文自然处理语言的模型了,但交互界面是命令行的,不太友好. 如果想做一个类似http://xiaosi.trs.cn/demo/rs/demo的界面,那就还需要继续往 ...
- 【技巧】使用weeman来做一个钓鱼网页
本文来自网友836834283 对玄魂工作室的投稿. 工具项目地址:https://github.com/Hypsurus/weeman/ 克隆地址:https://github.com/Hypsur ...
- 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。
最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 做一个 App 前需要考虑的几件事
做一个 App 前需要考虑的几件事 来源:limboy的博客 随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...
- 有了lisk,为什么我们还要做一个Asch?
0 前言 首先要声明一点,我们和我们的一些朋友都是lisk的投资人和支持者,我们也相信lisk会成功. 事实上,lisk已经成功了一半,目前在区块链领域融资金额排行第二,仅次于以太坊. 那为什么我们还 ...
- 做一个阅读管理APP
背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...
随机推荐
- (65)C# 任务
1.启动任务 //Framework4.5新增的Task.Run开启一个任务,Run方法中传入一个Action委托 Task.Run(()=> { Thread.Sleep(); Console ...
- 图解HTTP 读书笔记
1 了解Web及网络基础 1.1 HTTP/1.0 HTTP正式作为标准被公布实在1996年五月,版本命名为HTTP/1.0,记载于RFC1945.至今仍广泛使用在服务器端. RFC1945 – ...
- ORM模型类介绍,
所有的软件开发过程中,都会涉及到对象和关系型数据库,在用户层面和业务逻辑层面,程序员编写代码都是面向对象的,当我们对象的信息发生变化的时候,都需要将对应的信息,传到关系型数据库中.而在此之前,需要我们 ...
- KMP算法——字符匹配
暴力匹配: 假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢? 如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置, ...
- 路由参数 query和params
1. path:'www.baidu.com' query { id:122 } 对应地址:http:'www.baidu.coom?id=122' 类似get方式 2.name:'baidu' ...
- Libgdx中TextButton的一些思考
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/caihongshijie6/article/details/37566183 由于有 ...
- Unity接入AbMob踩坑记
之前是配置好的环境,不知道怎么突然就不正常了. 一直弹出下面的报错: Error running CocoaPods. Please ensure you have at least version ...
- ThinkPhp学习
页面跳转 界面跳转是很常用的操作,所以基于ubuntu16系统,这周学习了ThinkPHP页面跳转和重定向. 页面跳转 系统的Think\Controller类内置了两个页面跳转方法err ...
- CentOS 6.5安装aria2(转载)
CentOS 6.5安装aria2 由于yum install aria2无法找到安装包,试了好几个源,都找不到,于是自己找了一些地址: 1.下载安装包: # wget http://ftp.tu-c ...
- android中返回数据给上一个活动,可以用来回显数据
(一)who简介:没错,就是startActivityForResult()方法,这个方法用来在活动被销毁的时候返回数据给上一个方法.参数说明: startActivityForResult(inte ...