So what i'm trying to achieve is player/tile "collision". The player is gridlocked so i figured this would be simple..But i must be doing something wrong since i cant get this to work. Basically my setup is everytime you press for example the Right key it loops through the solid tiles (which are in a separate table) and checks if any of them are in the same x position as the player + 1.
Code: Select all
local go = true
if key == "d" then
for i=1, #tiles do
if tiles[i].x == player.x + 1 then
go = false
break
end
end
if go then
player.x = player.x + 1
end
Here's the whole code (this is a test project, not the actual game where i'll be using this stuff)
Code: Select all
function love.load()
tiles = {}
w = 12
h = 12
xs = 0
ys = 0
for y=1, h do
for x=1, w do
local r = math.random(0, 100)
if r <= 80 then
--addTile(xs, ys, false)
else
addTile(xs, ys, true)
end
xs = xs + 1
end
xs=0
ys = ys + 1
end
player = {
x = 5,
y = 5
}
end
function love.keypressed(key)
local go = true
if key == "d" then
for i=1, #tiles do
if tiles[i].x == player.x + 1 then
go = false
break
end
end
if go then
player.x = player.x + 1
end
elseif key == "a" then
player.x = player.x - 1
elseif key == "w" then
player.y = player.y - 1
elseif key == "s" then
player.y = player.y + 1
end
end
function addTile(x, y, s)
tiles[#tiles + 1] = {
x = x,
y = y,
s = s
}
end
function love.update(dt)
end
function love.draw()
for i=1, #tiles do
if tiles[i].s then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(150, 150, 150)
end
love.graphics.rectangle("fill", tiles[i].x*32, tiles[i].y *32, 32, 32)
end
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", player.x * 32, player.y * 32, 32, 32)
end
-Veethree