当前位置: 首页 > news >正文

(一)、python程序--模拟电脑鼠走迷宫

一、绪论

1、简介

电脑鼠走迷宫是一种比赛,制作实物电脑鼠小车在迷宫找目标点,用时最短者获胜。考验参赛选手软硬件结合的能力。

2、走迷宫模拟软件中已实现功能

1、点击迷宫墙壁可编辑迷宫,并且可保存和加载迷宫形状文件;

2、自动搜索迷宫,采用回溯算法搜索整个迷宫;

3、采用洪水算法查找迷宫的最短路径。

整体界面

二、软件截图

打开迷宫形状配置文件

点击迷宫墙壁编辑迷宫形状

搜索迷宫

搜索迷宫

搜索迷宫,并且回溯未到过的地方

搜索结束,找到最短距离的路径

三、代码分享

代码中一共4个.py文件,一个迷宫形状配置文件,如下:

1、main.py

import tkinter as tk
import maze as maze
import micromouse as micromouseroot = tk.Tk()
windows_w = 640*2+100
windows_h = 640
bd = 10
map_size = 32
win = [15, 15]
root.title("MicroMouse")
root.geometry("{}x{}".format(windows_w, windows_h))cv = tk.Canvas(root, width=windows_h*2, height=windows_h, bg='white')
cv.place(x=0, y=0)mz = maze.Maze(cv, windows_h, bd, win, map_size)
mz.draw_maze()
mz.draw_win_point()mouse = micromouse.MicroMouse(cv, mz.cell_len, bd, win, windows_h, map_size)
mouse.draw_mouse(0, 0)
mouse.draw_win_oval()
mouse.draw_search_maze()save = tk.Button(root, text='保存迷宫', command=mz.save_maze, height=1, width=8)
save.place(x=windows_h*2+20, y=10)open = tk.Button(root, text='加载迷宫', command=mz.load_maze, height=1, width=8)
open.place(x=windows_h*2+20, y=50)search = tk.Button(root, text='探索迷宫', command=lambda mz=mz: mouse.search_maze(mz), height=1, width=8)
search.place(x=windows_h*2+20, y=90)setmouse = tk.Button(root, text='重置电脑鼠', command=mouse.reset_mouse, height=1, width=8)
setmouse.place(x=windows_h*2+20, y=130)root.mainloop()

2、tree.py

class Tree(object):def __init__(self):self.last_tree = Noneself.next_tree = Noneself.h = Noneself.w = None

3、maze.py

import numpy as np
from tkinter import filedialogclass Maze(object):def __init__(self, cv, windows_h, bd, win, map_size=32):self.map_size = map_sizeself.cv = cvself.bd = bdself.win = winself.up = 0self.down = 1self.left = 2self.right = 3self.maze_map = np.ones((map_size, map_size, 4), dtype='int8')  # h/y, w/x, wall/up,down,left,rightself.cell_len = int((windows_h-2*self.bd)/map_size)self.line_hand = []self.win_point = Nonedef draw_win_point(self):# clear win pointif self.win_point is not None:self.cv.delete(self.win_point)# draw win pointw, h = self.winx, y = (w+0.5)*self.cell_len+self.bd, (h+0.5)*self.cell_len+self.bdrate = self.cell_len/3x0, y0 = x - rate, y - ratex1, y1 = x + rate, y + rateself.win_point = self.cv.create_oval(x0, y0, x1, y1, fill='red')def draw_maze(self):# clear mazefor tag in self.line_hand:self.cv.delete(tag)# draw mazefor h in range(self.map_size):for w in range(self.map_size):for index in range(4):# up and down for wallif index in [self.up, self.down]:x0, y0 = w*self.cell_len+self.bd, (h+index)*self.cell_len+self.bdx1, y1 = (w+1)*self.cell_len+self.bd, (h+index)*self.cell_len+self.bd# left and right for wallelse:x0, y0 = (w+index-2)*self.cell_len+self.bd, h*self.cell_len+self.bdx1, y1 = (w+index-2)*self.cell_len+self.bd, (h+1)*self.cell_len+self.bd# no line draw white lineif self.maze_map[h, w, index] == 1:color = 'black'else:color = 'Gainsboro'# draw lineself.line_hand.append(self.cv.create_line(x0, y0, x1, y1, width=2, fill=color))self.cv.tag_bind(self.line_hand[-1], '<Button-1>', lambda event, c=[h, w, index]: self.change_cell(c))def change_cell(self, c):# click to hide or show lineh, w, index = c# edge wallif h == 0 and index == self.up:returnif w == 0 and index == self.left:return# up wallif index == self.up:# deal clicked lineif self.maze_map[h, w, self.up] == 1:self.maze_map[h, w, self.up] = 0else:self.maze_map[h, w, self.up] = 1# deal adjoin lineif h-1 >= 0:if self.maze_map[h, w, self.up] == 0:self.maze_map[h - 1, w, self.down] = 0else:self.maze_map[h - 1, w, self.down] = 1# left wallif index == self.left:# deal clicked lineif self.maze_map[h, w, self.left] == 1:self.maze_map[h, w, self.left] = 0else:self.maze_map[h, w, self.left] = 1# deal adjoin lineif w-1 >= 0:if self.maze_map[h, w, self.left] == 0:self.maze_map[h, w - 1, self.right] = 0else:self.maze_map[h, w - 1, self.right] = 1self.draw_maze()def save_maze(self):fileSave = filedialog.asksaveasfilename(defaultextension='.txt', filetypes=[("txt files", ".txt")])np.savetxt(fileSave, self.maze_map.reshape((1, self.map_size*self.map_size*4)), fmt='%d')def load_maze(self):filePath = filedialog.askopenfilename()self.maze_map = np.loadtxt(filePath).reshape((self.map_size, self.map_size, 4))self.draw_maze()def get_maze_map(self):return self.maze_map.copy()

4、micromouse.py

import numpy as np
from tkinter import filedialogclass Maze(object):def __init__(self, cv, windows_h, bd, win, map_size=32):self.map_size = map_sizeself.cv = cvself.bd = bdself.win = winself.up = 0self.down = 1self.left = 2self.right = 3self.maze_map = np.ones((map_size, map_size, 4), dtype='int8')  # h/y, w/x, wall/up,down,left,rightself.cell_len = int((windows_h-2*self.bd)/map_size)self.line_hand = []self.win_point = Nonedef draw_win_point(self):# clear win pointif self.win_point is not None:self.cv.delete(self.win_point)# draw win pointw, h = self.winx, y = (w+0.5)*self.cell_len+self.bd, (h+0.5)*self.cell_len+self.bdrate = self.cell_len/3x0, y0 = x - rate, y - ratex1, y1 = x + rate, y + rateself.win_point = self.cv.create_oval(x0, y0, x1, y1, fill='red')def draw_maze(self):# clear mazefor tag in self.line_hand:self.cv.delete(tag)# draw mazefor h in range(self.map_size):for w in range(self.map_size):for index in range(4):# up and down for wallif index in [self.up, self.down]:x0, y0 = w*self.cell_len+self.bd, (h+index)*self.cell_len+self.bdx1, y1 = (w+1)*self.cell_len+self.bd, (h+index)*self.cell_len+self.bd# left and right for wallelse:x0, y0 = (w+index-2)*self.cell_len+self.bd, h*self.cell_len+self.bdx1, y1 = (w+index-2)*self.cell_len+self.bd, (h+1)*self.cell_len+self.bd# no line draw white lineif self.maze_map[h, w, index] == 1:color = 'black'else:color = 'Gainsboro'# draw lineself.line_hand.append(self.cv.create_line(x0, y0, x1, y1, width=2, fill=color))self.cv.tag_bind(self.line_hand[-1], '<Button-1>', lambda event, c=[h, w, index]: self.change_cell(c))def change_cell(self, c):# click to hide or show lineh, w, index = c# edge wallif h == 0 and index == self.up:returnif w == 0 and index == self.left:return# up wallif index == self.up:# deal clicked lineif self.maze_map[h, w, self.up] == 1:self.maze_map[h, w, self.up] = 0else:self.maze_map[h, w, self.up] = 1# deal adjoin lineif h-1 >= 0:if self.maze_map[h, w, self.up] == 0:self.maze_map[h - 1, w, self.down] = 0else:self.maze_map[h - 1, w, self.down] = 1# left wallif index == self.left:# deal clicked lineif self.maze_map[h, w, self.left] == 1:self.maze_map[h, w, self.left] = 0else:self.maze_map[h, w, self.left] = 1# deal adjoin lineif w-1 >= 0:if self.maze_map[h, w, self.left] == 0:self.maze_map[h, w - 1, self.right] = 0else:self.maze_map[h, w - 1, self.right] = 1self.draw_maze()def save_maze(self):fileSave = filedialog.asksaveasfilename(defaultextension='.txt', filetypes=[("txt files", ".txt")])np.savetxt(fileSave, self.maze_map.reshape((1, self.map_size*self.map_size*4)), fmt='%d')def load_maze(self):filePath = filedialog.askopenfilename()self.maze_map = np.loadtxt(filePath).reshape((self.map_size, self.map_size, 4))self.draw_maze()def get_maze_map(self):return self.maze_map.copy()

相关文章:

  • element plus 实现跨页面+跨tab栏多选
  • RK3568平台(opencv篇)opencv处理图像视频
  • 雅思词汇及发音积累 2024.7.10
  • Python 轻松生成多种条形码、二维码 (Code 128、EAN-13、QR code等)
  • 5个在线地图瓦片URL分享
  • nginx的知识面试易考点
  • PTC可复位保险丝 vs 传统型保险丝:全面对比分析
  • 零信任网络安全
  • 怎样将aac转换mp3格式?推荐四个aac转MP3的方法
  • 传统剪纸遇上AI绘画:一场跨时代的艺术对话
  • 【库架一体立体库】与【传统立体库】对比
  • Perl中追踪文件脉动:文件系统事件通知机制全解析
  • k8s 使用 helm 文件部署 8.12.2 es 分角色集群
  • 如何在玩客云中安装小雅AList并实现使用手机平板远程连接听歌看电影
  • 属性描述符初探——Vue实现数据劫持的基础
  • hexo+github搭建个人博客
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 77. Combinations
  • docker python 配置
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • es6要点
  • Java精华积累:初学者都应该搞懂的问题
  • MySQL-事务管理(基础)
  • 记一次和乔布斯合作最难忘的经历
  • 深入浅出webpack学习(1)--核心概念
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 在Docker Swarm上部署Apache Storm:第1部分
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • UI设计初学者应该如何入门?
  • ​一些不规范的GTID使用场景
  • ​字​节​一​面​
  • #HarmonyOS:Web组件的使用
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (2.2w字)前端单元测试之Jest详解篇
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (4)事件处理——(7)简单事件(Simple events)
  • (a /b)*c的值
  • (C)一些题4
  • (Java入门)抽象类,接口,内部类
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (十六)视图变换 正交投影 透视投影
  • (贪心 + 双指针) LeetCode 455. 分发饼干
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)linux 命令大全
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .CSS-hover 的解释
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Core Web APi类库如何内嵌运行?