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: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...
随机推荐
- 在线常用库 + API手册
以下链接经过本人测试,均可正常访问 jQuery: http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js bootsrap: http://app ...
- left join right inner join 区别
连表查询 select a, b, c from table_a tb_a left (right) join table_b tb_b on tb_a.id = tb_b.id left : tab ...
- C# 生成小程序码
/// <summary> /// B接口-微信小程序带参数二维码的生成 /// </summary> /// <param name="access_toke ...
- js中Object.defineProperty()方法的解释
菜菜: “老大,那个, Object.defineProperty 是什么鬼?” 假设我们有个对象 user ; 我们要给它增加一个属性 name , 我们会这么做 1 2 3 var user = ...
- Shell脚本中判断输入变量或者参数是否为空的方法
shell判断一个变量是否为空方法总结 https://www.jb51.net/article/154835.htm 1.判断变量 复制代码代码如下: read -p "input a w ...
- 1.etcd
etcd v3版接口命令 etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/heal ...
- python isinstance()函数和type()函数
一.type()用法 描述: python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. 语法: 一个参数:type(object) 三个参数: ...
- ant打包遇到的问题
\build\build.xml:350: Problem: failed to create task or type foreach Cause: The name is undefined. A ...
- js非布尔值的与(&&)与或(||)运算
/** * 非布尔值的与(&&)与或(||)运算 * 1.先将其转换成布尔值再做运算,并且返回原值 * 2.与(&&)运算: * a.如果第一个值为true,则返回第二 ...
- .Net编译环境x86,x64,anycpu的区别
一.定义 x86: 将程序集编译为由兼容 x86 的 32 位公共语言运行库运行. x64: 将程序集编译为由支持 AMD64 或 EM64T 指令集的计算机上的 64 位公共语言运行库运行. any ...