--print("开发流程步骤:I、绘制推箱子地图并初始化 ----- 几*几大小的地图 \n\n II、根据宏定义和推箱子地图上的数字来选择不同的图形\n\n III、获取玩家坐标 -----------重点\n\n (有两种情况:1.最开始玩家的坐标,并获取其横纵坐标\n\n 2. 玩家在目的地上时的坐标, 并获取其横纵坐标 )\n\n IV、移动箱子 ----------------重点、难点\n\n 根据你所要移动的方向 分别判断箱子是否可以移动\n\n 以向上为例:分为以下几种情况\n\n 1.玩家上面是否为空地或目的地,若是,那就箱子移动\n\n 2.玩家前面为箱子或箱子加目的地,箱子的前面是否为目的地或空地,若是,则移动箱子(箱子移动到原来箱子的前面,人移动到原来箱子的位置)\n\n V、判断是否通关,并设置多关卡\n\n 判断通关的条件:是否还有箱子")
--[[ 开发流程步骤:I、绘制推箱子地图并初始化 ----- 几*几大小的地图 II、根据宏定义和推箱子地图上的数字来选择不同的图形
III、获取玩家坐标 -----------重点
(有两种情况:1.最开始玩家的坐标,并获取其横纵坐标
2. 玩家在目的地上时的坐标, 并获取其横纵坐标 )
IV、移动箱子 ----------------重点、难点
根据你所要移动的方向 分别判断箱子是否可以移动
以向上为例:分为以下几种情况
1.玩家上面是否为空地或目的地,若是,那就箱子移动
2.玩家前面为箱子或箱子加目的地,箱子的前面是否为目的地或空地,
若是,则移动箱子(箱子移动到原来箱子的前面,人移动到原来箱子的位置)V、判断是否通关,并设置多关卡
判断通关的条件:是否还有箱子
]]
--1、初始化地图、定义全局变量
local SPACE=0--{"空白", " "}
local WALL=1--{"墙","墙 "}
local DEST=2--{"目的地","@@"}
local BOX=3--{"箱子","¥"}
local PLAYER=4--{"玩家","&&"}
local userkey=75
--tip:注意获取人在目的地时的坐标,
--定义两个全局变量 m,n 分别为玩家的横纵坐标
local m, n=0,0
local level=0--关卡
--IMAGE image[7]
--初始化地图
local gamemap = {
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0},
{0,0,0,1,2,1,0,0,0,0},
{0,0,0,1,3,1,1,1,1,0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,0,0},
},
}
-- 2、加载图片并绘制推箱子界面
local function game()
--加载图片
for i=1,8 do
image[i]=mapimage[i]
end
end
--绘制页面
local function drawMap(map)
--输出地图
for i=1,10 do
for j=1,10 do
--image[arr[level][i][j]]
end
end
end
--3、通过方向键移动,推箱子----------重难点
--[[local SPACE=0--{"空白", " "}
local WALL=1--{"墙","墙 "}
local DEST=2--{"目的地","@@"}
local BOX=3--{"箱子","¥"}
local PLAYER=4--{"玩家","&&"}
]]
--按键控制移动方向
local function move(userkey)
local userkey=72
if userkey==72 or userkey=="w" or userkey=="W" then
--玩家前面是否为空地
if arr[level][m - 1][n] == SPACE or arr[level][m - 1][n] == DEST then
arr[level][m - 1][n] = PLAYER + arr[level][m - 1][n]
arr[level][m][n] = arr[level][m][n] - PLAYER
--玩家前面是否为箱子
elseif arr[level][m - 1][n] == BOX or arr[level][m - 1][n] == BOX + DEST then
--箱子前面是否为空地或目的地
if arr[level][m - 2][n] == SPACE or arr[level][m - 2][n] == DEST then
arr[level][m - 2][n] = arr[level][m - 2][n] + BOX
arr[level][m - 1][n] = arr[level][m - 1][n] - BOX
arr[level][m - 1][n] = arr[level][m - 1][n] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
end
print("向上移动")
--Sleep(1000)
end
elseif userkey==80 or userkey== "s" or userkey=="S" then
--玩家后面是否为空地
if arr[level][m + 1][n] == SPACE or arr[level][m + 1][n] == DEST then
arr[level][m + 1][n] = PLAYER + arr[level][m + 1][n]
arr[level][m][n] = arr[level][m][n] - PLAYER
--玩家后面是否为箱子
elseif arr[level][m + 1][n] == BOX or arr[level][m + 1][n] == BOX + DEST then
--箱子后面是否为空地或目的地
if arr[level][m + 2][n] == SPACE or arr[level][m + 2][n] == DEST then
arr[level][m + 2][n] = arr[level][m + 2][n] + BOX
arr[level][m + 1][n] = arr[level][m + 1][n] - BOX
arr[level][m + 1][n] = arr[level][m + 1][n] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
end
print("向下移动")
end
elseif userkey==75 or userkey== "a" or userkey=="A" then
--玩家左边是否为空地
if arr[level][m][n - 1] == SPACE or arr[level][m][n - 1] == DEST then
arr[level][m][n - 1] = arr[level][m][n - 1] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
--玩家左边是否为箱子
elseif arr[level][m][n - 1] == BOX or arr[level][m][n - 1] == BOX + DEST then
--箱子左边是否为空地或目的地
if arr[level][m][n - 2] == SPACE or arr[level][m][n - 2] == DEST then
arr[level][m][n - 2] = arr[level][m][n - 2] + BOX
arr[level][m][n - 1] = arr[level][m][n - 1] - BOX
arr[level][m][n - 1] = arr[level][m][n - 1] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
end
print("向左移动")
end
elseif userkey==77 or userkey== "d" or userkey=="D" then
--玩家右边是否为空地
if arr[level][m][n + 1] == SPACE or arr[level][m][n + 1] == DEST then
arr[level][m][n + 1] = arr[level][m][n + 1] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
--玩家左边是否为箱子
elseif arr[level][m][n + 1] == BOX or arr[level][m][n + 1] == BOX + DEST then
--箱子左边是否为空地或目的地
if arr[level][m][n + 2] == SPACE or arr[level][m][n + 2] == DEST then
arr[level][m][n + 2] = arr[level][m][n + 2] + BOX
arr[level][m][n + 1] = arr[level][m][n + 1] - BOX
arr[level][m][n + 1] = arr[level][m][n + 1] + PLAYER
arr[level][m][n] = arr[level][m][n] - PLAYER
end
print("向右移动")
end
end
end-- 4、判断是否通过--------有无箱子的存在
--判断是否通关--------------是否还有箱子
local function judge()
for i=1,10 do
for j=1,10 do
if arr[level][i][j] == BOX then
return false
end
end
end
return true
end--5.main函数里面
local function main()
SetWindowText(GetHWnd(), "推箱子")
initgame()
drawMap()
--获取玩家坐标,并将其下标分别赋给 m,n
for i=1,10 do
for j=1,10 do
if arr[level][i][j] == PLAYER or arr[level][i][j]== PLAYER+DEST then
m,n=i,j
end
end
end
move()
system("cls")
drawMap()
if bool_judge() then
if level>1 then
exit(666)
MessageBox(GetHWnd(), "恭喜你通关了", "提示", MB_OKCANCEL)
Sleep(3000)
end
level=level+1
print("恭喜你过关了,大佬666\n")
end
end