Code: Select all
function love.load()
detectioncounter = 0
plrx = 0
plry = 0
collidecounter = 0
collidablex = {}
collidabley = {}
tileSize = 16 -- for image 32x32 pixels
g = {image = love.graphics.newImage("/textures/tiles/ground_1.png")} -- image 32x32 pixels
a = {image = love.graphics.newImage("/textures/tiles/stone_1.png")} -- image 32x32 pixels
local w, h = 50, 50
map = {}
for y = 1, h do
map[y] = {}
for x = 1, w do
local value = love.math.noise( 0.08*x, 0.2*y)
if value > 0.8 then -- 80% air and 20% ground
map[y][x] = g -- floor
else
map[y][x] = a -- walls
collidecounter = collidecounter + 1
table.insert(collidablex, collidecounter, x)
table.insert(collidabley, collidecounter, y)
end
end
end
print(collidablex[2], collidabley[2])
_playerinit()
end
function love.update(dt)
_playermove(dt)
end
function love.draw()
_playerdraw()
for y, xs in ipairs (map) do
for x, tile in ipairs (xs) do
love.graphics.draw(tile.image, (x-1)*tileSize, (y-1)*tileSize)
end
end
_playerdraw()
end
function _playerinit()
hero = love.graphics.newImage("/textures/entities/warrior.jpg")
end
function _playerdraw()
love.graphics.draw(hero, plrx, plry)
end
function _playermove()
love.keypressed( key, scancode, isrepeat )
end
function love.keypressed( key, scancode, isrepeat )
if scancode == "right" then -- move right
plrx = plrx + 16
while detection < 2501 and x ~= collidablex[detection] and y ~= collidabley[detection] do
detection = detection + 1
if collidablex[detection]=x and collidabley[detection]=y then
plrx = plrx - 16
end
elseif scancode == "left" then -- move left
plrx = plrx - 16
elseif scancode == "down" then -- move down
plry = plry + 16
elseif scancode == "up" then -- move up
plry = plry - 16
end
end