直接上代码:

 #!/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练习,做一个迷宫生成程序的更多相关文章

  1. SLAM+语音机器人DIY系列:(三)感知与大脑——6.做一个能走路和对话的机器人

    摘要 在我的想象中机器人首先应该能自由的走来走去,然后应该能流利的与主人对话.朝着这个理想,我准备设计一个能自由行走,并且可以与人语音对话的机器人.实现的关键是让机器人能通过传感器感知周围环境,并通过 ...

  2. 在树莓派上用 python 做一个炫酷的天气预报

    教大家如何在树莓派上自己动手做一个天气预报.此次教程需要大家有一定的python 基础,没有也没关系,文末我会放出我已写好的代码供大家下载. 首先在开始之前 需要申请高德地图API,去高德地图官网注册 ...

  3. 自然语言处理NLP学习笔记三:使用Django做一个NLP的Web站点

    前言: 前面我们已经能初步实现一个中文自然处理语言的模型了,但交互界面是命令行的,不太友好. 如果想做一个类似http://xiaosi.trs.cn/demo/rs/demo的界面,那就还需要继续往 ...

  4. 【技巧】使用weeman来做一个钓鱼网页

    本文来自网友836834283 对玄魂工作室的投稿. 工具项目地址:https://github.com/Hypsurus/weeman/ 克隆地址:https://github.com/Hypsur ...

  5. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  6. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  7. 做一个 App 前需要考虑的几件事

    做一个 App 前需要考虑的几件事  来源:limboy的博客   随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...

  8. 有了lisk,为什么我们还要做一个Asch?

    0 前言 首先要声明一点,我们和我们的一些朋友都是lisk的投资人和支持者,我们也相信lisk会成功. 事实上,lisk已经成功了一半,目前在区块链领域融资金额排行第二,仅次于以太坊. 那为什么我们还 ...

  9. 做一个阅读管理APP

    背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...

随机推荐

  1. identityserver4踩坑总结

    1.在配置id4服务端的时候要注意client里面的 AllowedScopes开放的值要和GetIdentityResources中加入的值是一样的,不然调用以后报{"error" ...

  2. 如何为元组中的每个元素命名,提高程序可读性---Python数据结构与算法相关问题与解决技巧

    实际案例: 学生信息系统中,数据为固定格式:(名字,年龄,性别,邮箱) ,通常使用元组来存储 使用优点: 使用元组最大的优点在于节省空间,存储相同的数据,使用元组比使用字典,空间小很多 使用缺点: 访 ...

  3. excel vlookup的使用

    表1 姓名 部门 ***   ***   ***   ***   表2 姓名 部门 ***  *** *** ***  *** ***  *** ***  找表2的姓名对应部门,粘贴在表1中 vloo ...

  4. Advanved DataGrid using QTP

    Use the GetCellData(j,i) Function for Cell data and Use the GetRowData(j) Function for Row Data wher ...

  5. cygwin的用途

    cgywin简介 打开cygwin的官方网站(www.cygwin.com),一行英文非常醒目:Get that Linux feeling – on Windows! 简而言之,cygwin是一个在 ...

  6. java中多种方式解析xml

    第一种:DOM.DOM的全称是Document Object Model,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序正 ...

  7. 链表反转&交换链表结点

    leetcode链表反转链接 要求摘要: 反转一个单链表 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1-&g ...

  8. Caused by: java.lang.NoClassDefFoundError: javax/validation/ParameterNameProvider

    问题现象:今天部署代码的时候发现,在beta环境可以正常部署,但是到了test环境就一直不成功,我以为是环境问题,就重新部署,但是没效,看了看日志发现问题是:Caused by: java.lang. ...

  9. ivew-admin 导入excel

    1.使用上传组件 <Upload ref="upload" name="importData" action="/api/device/impo ...

  10. jquery 选项卡切换 带背景图片

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...