lua推箱子简单小游戏

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值