lua推箱子简单游戏(无关卡,只有一关)
--6*6地图
local mytable={}
--渲染墙体界面
for i = 1, 6, 1 do
mytable[i]={}
for j = 1, 6, 1 do
if i==1 or i==6 or j==1 or j==6 then
mytable[i][j]="*"
else
mytable[i][j]=" "
end
end
end
--指定人的初始位置 1代表玩家
local x=3
local y=4
mytable[x][y]="1"
--设置3个障碍物
mytable[2][5]="*"
mytable[5][5]="*"
mytable[2][3]="*"
--设置两个箱子
mytable[3][3]="□"
mytable[5][3]="□"
--设置终点的位置
mytable[2][5]="0"
mytable[5][4]="0"
--渲染游戏界面
for i = 1, 6, 1 do
for j = 1, 6, 1 do
io.write(mytable[i][j].." ")
end
print()
end
--移动的次数
local count=0
while true do
local a=io.read()
count=count+1
if a=="w" and mytable[x-1][y]~="*" then
if mytable[x-1][y]=="□" then
if mytable[x-2][y]~="*" then
mytable[x][y]=" "
mytable[x-1][y]="1"
mytable[x-2][y]="□"
x=x-1
else
mytable[x][y]="1"
mytable[x-1][y]="□"
end
else
mytable[x][y]=" "
mytable[x-1][y]="1"
x=x-1
end
end
if a=="s" and mytable[x+1][y]~="*" then
if mytable[x+1][y]=="□" then
if mytable[x+2][y]~="*" then
mytable[x][y]=" "
mytable[x+1][y]="1"
mytable[x+2][y]="□"
x=x+1
else
mytable[x][y]="1"
mytable[x+1][y]="□"
end
else
mytable[x][y]=" "
mytable[x+1][y]="1"
x=x+1
end
end
if a=="a" and mytable[x][y-1]~="*" then
if mytable[x][y-1]=="□" then
if mytable[x][y-2]~="*" then
mytable[x][y]=" "
mytable[x][y-1]="1"
mytable[x][y-2]="□"
y=y-1
else
mytable[x][y]="1"
mytable[x][y-1]="□"
end
else
mytable[x][y]=" "
mytable[x][y-1]="1"
y=y-1
end
end
if a=="d" and mytable[x][y+1]~="*" then
if mytable[x][y+1]=="□" then
if mytable[x][y+2]~="*" then
mytable[x][y]=" "
mytable[x][y+1]="1"
mytable[x][y+2]="□"
y=y+1
else
mytable[x][y]="1"
mytable[x][y+1]="□"
end
else
mytable[x][y]=" "
mytable[x][y+1]="1"
y=y+1
end
end
for i = 1, 6, 1 do
for j = 1, 6, 1 do
io.write(mytable[i][j].." ")
end
print()
end
if count>20 then
print("游戏失败")
break
end
if mytable[2][5]=="□" and mytable[5][4]=="□" then
print("游戏胜利")
break
end
end