随机迷宫生成[深度/广度]
唔 我来6R好像没怎么发过东西呢 只知道用别人的 ..偶尔还是来一发做做贡献吧 不然觉得有点对不起大家呢OTZ此货为迷宫生成算法,应该算是比较完善了吧
不知道有人写过没有 不过由于鄙人阅读脚本的能力甚是太好,别人写的基本都看不懂OTZ Umm..所以还是自己写好了
脚本如下:
RUBY 代码
#==============================================================================
# ■ Random_Maze
#------------------------------------------------------------------------------
# 使用说明书:
# 效果:生成一个随机迷宫数组。(0:地 1:墙)
# 用法:
# 广度优先 --> prim(X最大值,Y最大值,起始X[可省略],起始Y[可省略])
# 深度优先 --> depth(X最大值,Y最大值,起始X[可省略],起始Y[可省略])
# 制作:永远的塞尔达传说(7408)
#==============================================================================
class Random_Maze
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
attr_reader :pos_x # 当前 X 坐标
attr_reader :pos_y # 当前 Y 坐标
attr_reader :list # 可通行列表
attr_reader :mark # 标记列表
attr_accessor :maze # 迷宫
#--------------------------------------------------------------------------
# ● 初始化对像
#--------------------------------------------------------------------------
def initialize
@maze = []
end
#--------------------------------------------------------------------------
# ● 广度优先
#--------------------------------------------------------------------------
def prim(xmax,ymax,start_x = nil,start_y = nil)
# 初始化
@maze = []
start_x = rand(xmax + 1)if start_x == nil
start_y = rand(ymax + 1)if start_y == nil
for x in0..xmax
@maze = []
for y in0..ymax
@maze = 1
end
end
@maze = 0
@list = adjacent_wall(start_x,start_y)
# 生成迷宫
while !@list.empty?
id = rand(@list.size-1)
x = @list
y = @list
d = @list
new_x = x + (d == 3 ? 1 : (d == 2 ? -1 : 0))
new_y = y + (d == 1 ? 1 : (d == 4 ? -1 : 0))
# 如果前面也为墙
if exist?(new_x,new_y)and@maze == 1
@maze = 0
@maze = 0
@list = @list + adjacent_wall(new_x,new_y)
@list.delete_at(id)
else
@list.delete_at(id)
end
end
# 返回迷宫
return@maze.clone
end
#--------------------------------------------------------------------------
# ● 深度优先
#--------------------------------------------------------------------------
def depth(xmax,ymax,pos_x = nil,pos_y = nil)
# 初始化
@maze = []
pos_x = rand(xmax + 1)if pos_x == nil
pos_y = rand(ymax + 1)if pos_y == nil
for x in0..xmax
@maze = []
for y in0..ymax
@maze = 1
end
end
@pos_x = pos_x
@pos_y = pos_y
@maze = 0
@mark = []
# 生成迷宫
while not_full?
all = passage_way(@pos_x,@pos_y)
if all.empty?
@pos_x,@pos_y = @mark.pop
else
coord = all
@maze]] = 0
@maze]] = 0
@pos_x = coord
@pos_y = coord
@mark.push([@pos_x,@pos_y])
end
end
# 返回迷宫
return@maze.clone
end
#--------------------------------------------------------------------------
# ● 存在否?
#--------------------------------------------------------------------------
def exist?(x,y)
returnfalseif(x < 0)or(x >= @maze.size)or(y < 0)or(y >= @maze.size)
returntrue
end
#--------------------------------------------------------------------------
# ● 满否?
#--------------------------------------------------------------------------
def not_full?
delta_x = (@pos_x % 2 == 1 ? 1 : 0)
delta_y = (@pos_y % 2 == 1 ? 1 : 0)
xmax = (@maze.size / 2).floor
ymax = (@maze.size / 2).floor
for x in0...xmax
for y in0...ymax
returntrueif@maze == 1
end
end
returnfalse
end
#--------------------------------------------------------------------------
# ● Mark中存在此坐标否?
#--------------------------------------------------------------------------
def marked_coord?(x,y)
@mark.eachdo |this|
if this == x and this == y
returntrue
end
end
returnfalse
end
#--------------------------------------------------------------------------
# ● 取得所有通路
#--------------------------------------------------------------------------
def passage_way(x,y)
result = []
# 循环四个方向
for d in1..4
new_x_a = x + (d == 3 ? 1 : (d == 2 ? -1 : 0))
new_y_a = y + (d == 1 ? 1 : (d == 4 ? -1 : 0))
new_x_b = x + (d == 3 ? 2 : (d == 2 ? -2 : 0))
new_y_b = y + (d == 1 ? 2 : (d == 4 ? -2 : 0))
if exist?(new_x_b,new_y_b)and@maze == 1and !marked_coord?(new_x_b,new_y_b)
result.push()
end
end
return result
end
#--------------------------------------------------------------------------
# ● 获取相邻的墙壁
#--------------------------------------------------------------------------
def adjacent_wall(x,y)
result = []
# 循环四个方向
for d in1..4
new_x = x + (d == 3 ? 1 : (d == 2 ? -1 : 0))
new_y = y + (d == 1 ? 1 : (d == 4 ? -1 : 0))
if exist?(new_x,new_y)and@maze == 1
result.push()
end
end
# 返回结果
return result
end
end
范例:
具体看范例吧~嗯嗯 谢谢大家
本帖来自P1论坛作者7408,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=375096若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页:
[1]