效果如下
代码简介:
在10*10的网格中用白色方块代表障碍物,蓝色方块代表小人,红色方块代表箱子。通过方向键来控制小人的移动,并且实现小人推箱子的动作。
代码思路
1.场景生成
利用二维数组的不同取值来表示不同的物体,1=障碍物,2=箱子,3=小人,0=空白区域
遍历数组来通过判断值的不同,绘制30*30不同颜色的正方形
2.功能实现
1)障碍物
第一版:
function love.keypressed(key)
if love.keyboard.isDown("right") and player.x < 9 and blockList[player.y][player.x + 1]==0 then
player.x = player.x + 1
elseif love.keyboard.isDown("left") and player.x > 1 and blockList[player.y][player.x-1] == 0 then
player.x = player.x -1
elseif love.keyboard.isDown("up") and player.y >1 and blockList[player.y-1][player.x] == 0 then
player.y = player.y -1
elseif love.keyboard.isDown("down") and player.y <9 and blockList[player.y+1][player.x] == 0 then
player.y = player.y +1
end
end
当按下方向键的时候就检查数组对应的值是否为0,如果为0即可移动
第二版:
function love.keypressed(key)
local x = player.x
local y = player.y
if key == "left" then
x = x - 1
elseif key == "right" then
x=x +1
elseif key == "up" then
y = y - 1
elseif key == "down" then
y = y + 1
end
if blockList[y][x]==0 then
blockList[y][x]=3
blockList[player.y][player.x]=0
player.x = x
player.y = y
end
end
(1)改变了判断逻辑,先预行动再判断是否可以移动
(2)用局部变量x,y来减少代码量,同时将x,y的值作为模拟值来判断是否可以移动
2)人物移动
我设置了table
player={x=2,y=2}
在按下方向键后更改player的x与y值,然后根据x与y值绘制小人
后面发现这种有点不伦不类,因为我们应该做的不仅是计算player的x与y,更应该把二维数组中对应的值改为3
blockList[y][x]=3
3)箱子移动
结合上述两点即可
遇到的问题
1)格子之间有像素大小为1的分割线,再绘制方块的时候需要注意这点,保证格式的美观
2)暂无
代码如下:
function love.load()
blockList = {
{1 ,1 ,1 ,1 ,1 ,1,1 ,1 ,1 ,1},
{1 ,3 , 0, 0, 1, 0 ,0 ,0 ,0,1},
{1, 0 ,0 ,0 ,0 ,1 ,0, 0, 0, 1},
{1 ,0 ,0 ,0, 0, 0,0 ,0 ,1 ,1},
{1 ,0 ,0 ,0 ,0 ,2 ,0 ,0 ,0 ,1},
{1, 1 ,1 ,0 ,0 ,0 ,0, 0 ,0 ,1},
{1 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0, 1},
{1 ,0, 0 ,0 ,0 ,0 ,0 ,1 ,0 ,1},
{1 ,0 ,0 ,0 ,0 ,0 ,0, 1, 0 ,1},
{1 ,1 ,1,1 ,1, 1,1 ,1 ,1 ,1,1}
}
player = {
x=2,
y=2
}
block_red = {
x=6,
y=5
}
background_x = 100
background_y = 100
end
function love.keypressed(key)
local x = player.x
local y = player.y
local z = block_red.x
local p = block_red.y
if key == "left" then
x = x - 1
z=z-1
elseif key == "right" then
x=x +1
z = z + 1
elseif key == "up" then
y = y - 1
p = p -1
elseif key == "down" then
y = y + 1
p = p +1
end
if blockList[y][x]==0 then
blockList[y][x]=3
blockList[player.y][player.x]=0
player.x = x
player.y = y
elseif blockList[y][x] == 2 then
if blockList[p][z] == 0 then
blockList[p][z] = 2
blockList[y][x] = 3
blockList[player.y][player.x] = 0
player.x = x
player.y = y
block_red.x=z
block_red.y=p
elseif blockList[p][z] == 1 then
end
end
end
function love.update(dt)
end
function love.draw()
local y= 0
for i=1,10,1 do
local x=0
for j=1,10,1 do
if blockList[i][j]==1 then
love.graphics.rectangle("fill", background_x + x, background_y+y, 30, 30)
elseif blockList[i][j]==2 then
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill", background_x + (block_red.x - 1) * 31,
background_y + (block_red.y - 1) * 31,
30, 30)
love.graphics.setColor(255, 255, 255)
elseif blockList[i][j]==3 then
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", background_x + x, background_y + y, 30, 30)
love.graphics.setColor(255, 255, 255)
else
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", background_x + x, background_y + y, 30, 30)
love.graphics.setColor(255, 255, 255)
end
x = x+31
end
y = y +31
end
end