前言
上两天我们做了一个贪吃蛇小游戏,这次推箱子来了,哈哈哈
准备工作
硬件准备
1,准备724,724等能运行luat的开发版
2,矩阵键盘
软件准备
首先呢,准备几个需要用到的图片,例如这样的
根据自己的屏幕准备好驱动,我是用的是ST7899的LCD做的,1.54寸屏幕上分辨率 240*240
(后面会有代码地址)
开始制作
游戏思路
编写程序之前,我们先梳理思路,推箱子主要就是不同关卡地图绘制,推箱子的逻辑是通用的,所以我们只需要用一个多维数组保存地图,编写好逻辑不就好了,那么开始吧
游戏制作
我们使用多维数组创建地图,根据我们设置的情况绘制
--0:墙外 1:墙 2:墙内空地 3:箱子 4:箱子要去的目标 5:人 6:箱子与目标重合 7:目标与人重合
local checkpoint_1 = {
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0,0,0},
{0,0,0,0,1,4,1,0,0,0,0,0},
{0,0,0,0,1,2,1,1,1,1,0,0},
{0,0,1,1,1,3,2,3,4,1,0,0},
{0,0,1,4,2,3,5,1,1,1,0,0},
{0,0,1,1,1,1,3,1,0,0,0,0},
{0,0,0,0,0,1,4,1,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
}
这样地图就设计好了,下面开始画地图
local function draw_map()
disp.setbkcolor(0xf6e7b3)--f6e7b3
disp.clear()
disp.puttext(common.utf8ToGb2312("关卡:"),10,2)
disp.puttext(common.utf8ToGb2312(map),55,2)
for i = 1, 11 do
for j = 1, 12 do
local location = checkpoint[i][j]
if location == 0 or location == 2 then
elseif location == 1 then
disp.putimage("/lua/wall.png",j*20-20,i*20)
elseif location == 3 or location == 6 then
disp.putimage("/lua/box.png",j*20-20,i*20)
elseif location == 4 then
disp.putimage("/lua/target.png",j*20-20,i*20)
elseif location == 5 then
location_guy = {i,j}
disp.putimage("/lua/guy.png",j*20-20,i*20)
elseif location == 7 then
location_guy = {i,j}
disp.putimage("/lua/target.png",j*20-20,i*20)
disp.putimage("/lua/guy.png",j*20-20,i*20)
end
end
end
disp.update()
end
可以看到,也很简单,根据行列直接绘制就可以了,我们顺便将人的坐标保存了,方便之后人物移动获取坐标。
然后就是控制人的控制,矩阵键盘不再赘述,可以看之前的贪吃蛇或者看文章最后放出的代码,这里主要判断其中人物可以动的情况就可以,不可移动的不用管,坐标就不会变,我们以左移为例,其他方向的逻辑也一样
if checkpoint[location_guy[1]][location_guy[2]-1]==2 then--下个位置是空地
checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
end
elseif checkpoint[location_guy[1]][location_guy[2]-1]==3 then--下个位置是箱子
if checkpoint[location_guy[1]][location_guy[2]-2]==2 then--箱子后是空地
checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
checkpoint[location_guy[1]][location_guy[2]-2]=3--箱子后的位置变为箱子
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
elseif checkpoint[location_guy[1]][location_guy[2]-2]==4 then--箱子后是目标
checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
checkpoint[location_guy[1]][location_guy[2]-2]=6--箱子后的位置变为箱子与目标重合
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
end
elseif checkpoint[location_guy[1]][location_guy[2]-1]==4 then--下个位置是目标
checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
end
elseif checkpoint[location_guy[1]][location_guy[2]-1]==6 then--下个位置是箱子与目标重合
if checkpoint[location_guy[1]][location_guy[2]-2]==2 then--箱子后是空地
checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
checkpoint[location_guy[1]][location_guy[2]-2]=3--箱子后的位置变为箱子
if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
end
elseif checkpoint[location_guy[1]][location_guy[2]-2]==4 then--箱子后是目标
checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
checkpoint[location_guy[1]][location_guy[2]-2]=6--箱子后的位置变为箱子与目标重合
if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
end
end
end
好,最后我们初始化游戏即可
local function game_init()
disp.setbkcolor(0x0000)
disp.clear()
lcd.setcolor(0x00FF)
disp.setfontheight(24)
disp.puttext(common.utf8ToGb2312("推箱子"),(lcd.HEIGHT-string.utf8Len("推箱子")*24)/2,170)
disp.putimage("/lua/box_game.png",90,90)
disp.update()
disp.setfontheight(16)
sys.wait(2000)
io.writeFile("/box_game", json.encode(map_table))
game_data = json.decode(io.readFile("/box_game"))
checkpoint = game_data[map]
draw_map()
disp.putimage("/lua/start.png",40,100)
disp.update()
while true do
local result, data = sys.waitUntil("key",20)
if result == true then
if data == "key_left" then
elseif data == "key_right" then
elseif data == "key_up" then
elseif data == "key_down" then
elseif data == "key_ok" then
game_thread()
end
end
end
end
sys.taskInit(game_init)
可以看到,我们最开始将地图存起来了,在读出来,为什么呢?因为如果我们直接操作数组就会直接改变,等下次再玩游戏时候地图数据是变化了的。
最后上代码:https://gitee.com/Dozingfiretruck/luat-box_game