直接上代码:

 #!/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. 用 Flask 来写个轻博客 (31) — 使用 Flask-Admin 实现 FileSystem 管理

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 编写 FileSystem Admin 页面 Flask-A ...

  2. appium常见问题04_查看andriod内置浏览器webview版本

    方法一:手机上设置中查看 设置-->应用程序管理-->全部-->Android System WebView 方法二:adb指令查看(前提,已安装android sdk环境) 1,w ...

  3. hdu3518 Boring counting(后缀数组)

    Boring counting 题目传送门 解题思路 后缀数组.枚举每种长度,对于每个字符串,记录其最大起始位置和最小起始位置,比较是否重合. 代码如下 #include <bits/stdc+ ...

  4. 【awk】 处理多个文件

    处理多个文件: 1. 可以在代码中指定读取某个文件, 其他的用命令行输入           while ( geline < "file.txt" > 0 ) {   ...

  5. 垃圾循环li

    function hezhu() { // var lis= $("#aaa").find("treeview"); ////var lis= document ...

  6. kmp next数组的模板

    string s; int Next[MAX]; int len; void get_next() { ,j=-; Next[i]=j;//初始化,next[0]=-1:-1表示没有前缀等于后缀. ; ...

  7. K8S入门系列之必备扩展组件--> coredns(四)

    摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. https://github.com ...

  8. shell数学运算

    shell的数学运算 branches@ubuntu:~$ var1=$[ * ] branches@ubuntu:~$ echo $var1 branches@ubuntu:~$ var2=$[$v ...

  9. 重读ORB_SLAM之LocalMapping线程难点

    1. 认清几个锁与布尔参数 线程的通信与相互影响在ORB比较复杂,需要好好缕清思路. 1.1 mbStopRequested,由RequestStop函数设定,主要是在回环线程里,在运行全局优化时,以 ...

  10. LAN VLAN与VXLAN学习笔记

    一.LAN(Local Area Network,局域网) 1.通信方式: 向目标IP地址发送ARP广播,获取目的IP地址的MAC地址,然后用单播MAC地址实现相互通信 2.LAN的特点: 1.同一L ...