我正在love2d(lua)中制作一个俄罗斯方块克隆来练习。我想做的是用墙壁制作柯林斯。
注意:俄罗斯方块是由方块组成的,每个方块都有一个与存储在桌子内的方块位置相对的位置。
这是我实现墙壁碰撞的方法
function tetros.wallLeftCollision(x)
for i, c in pairs(tetros.shapes[v.shape].angles[v.angle].cubes) do
local cx= c.x+x
if cx-1 <= v.window.x then
return true
else
return false
end
end
end
x:是棋子的 x 位置
“tetros.shapes[v.shape].angles[v.angle].cubes”:是包含形成该块的图块的相对位置的表
“cx= c.x+x”:这里我将相对位置转换为正常位置
“cx-1”我只是将位置稍微向左移动
“v.window.x”这是图片落下的空间的 x 位置(不知道如何称呼它)
但是当我运行代码时,它显然选择了一个随机图块并进行比较,而不是比较所有图块。
这里应该发生的是代码检查俄罗斯方块中任何图块的位置是否距边界右侧 1 像素,然后返回 true。
此后,我尝试做大量研究来了解为什么会发生这种情况,但我发现它应该按原样工作。
注意:请考虑以下事实:除了 Minecraft 命令之外,我没有任何编程经验,而且我才刚刚开始
通常,任何碰撞检查都是一个循环:
(这里的块是一个图块列表,每个图块是一个带有x和y位置的表格)
function detectCollision (movingBlock, staticBlock)
for i, movingTile in ipairs (movingBlock) do
for j, staticTile in ipairs (staticBlock) do
if isColliding (movingTile, staticTile) then
return true -- yes, at least one collision
end
end
end
return false -- no, there was no collision
end
对于运动,您只需添加额外的
dx
和 dy
来检查此运动的碰撞:
function detectCollision (movingBlock, staticBlock, dx, dy)
for i, movingTile in ipairs (movingBlock) do
for j, staticTile in ipairs (staticBlock) do
if isColliding (movingTile, staticTile, dx, dy) then
return true -- yes, at least one collision
end
end
end
return false -- no, there was no collision
end
移动到 dx、dy 后比较两个图块的示例函数:
function isColliding (movingTile, staticTile, dx, dy)
if movingTile.x + dx == staticTile.x
and movingTile.y + dy == staticTile.y then
return true -- same position of tiles means collision
else
return false -- no collision
end
end