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2],
[0,2]
]
def createMaze(row,col):
maze=[[0 for i in range(col)] for i in range(row)]
check=[]
firstrow=rd.randrange(1,row-2,2)
firstcol=rd.randrange(1,col-2,2)
maze[firstrow][firstcol]=1
check.append([firstrow,firstcol])
while len(check):
c=rd.choice(check)
nears=[]
conditions=[]
for maybe in nearmaybe:
conditions.append([c[0]+maybe[0],c[1]+maybe[1]])
for condition in conditions:
if condition[0]>=1 and condition[0]<=row-2 \
and condition[1]>=1 and condition[1]<=col-2:
nears.append([condition[0],condition[1]])
for n in nears.copy():
if maze[n[0]][n[1]]:
nears.remove(n)
for block in nears:
if block[0]==c[0]:
if block[1]<c[1]:
maze[block[0]][c[1]-1]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
maze[block[0]][block[1]-1]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
if block[0]<c[0]:
maze[c[0]-1][block[1]]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
else:
maze[block[0]-1][block[1]]=1
maze[block[0]][block[1]]=1
check.append([block[0],block[1]])
if not len(nears):
check.remove(c)
maze[1][1]="s"
while True:
c=rd.randint(1,col-2)
if maze[row-2][c]:
maze[row-2][c]="e"
break
return maze
調用該函數的方法:
maze=createMaze(27,27)
for l in maze:
for s in l:
print(s,end=" ")
print()
CreateMaze傳入的兩個參數必須都是單數,否則程序可能會無法運行!
下面的for循環遍歷就是將生成的迷宮矩陣打印出來
S表示出發點
1表示路
0表示墻
E表示終點
生成迷宮矩陣后,就可以將其引用到游戲中去啦!
原文鏈接:https://blog.csdn.net/leleprogrammer/article/details/124205148
相關推薦
- 2022-12-09 python中為main方法傳參問題_python
- 2023-01-08 簡化Cocos和Native交互利器詳解_React
- 2022-07-26 用3dmax做折扇的思路方法與步驟
- 2023-11-23 記錄apscheduler任務模塊添加任務后立即執行
- 2022-05-18 React?Hook之使用State?Hook的方法_React
- 2022-10-27 python圖像填充與裁剪/resize的實現代碼_python
- 2023-07-08 echarts多個series進行自定義tooltip的數據顯示
- 2022-07-09 Harbor鏡像倉庫搭建
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支