tileGrid function
Posted: Sat Feb 03, 2024 10:00 pm
I recently came across a video creating a falling sand simulator in p5, I figured löve has the resources that I could do something similar, all I managed to accomplish this far is the tile layout but I split the generation of tiles into 2 functions one to define every tile in a table and the second function is to draw each tile. I have no idea how I’m going to use the tileGrid further I’m about 6 hours or so total into using löve so probably just me not knowing enough syntax. Anyways here’s the code dump if you’d like to see
To best use the functions defineTile() should be placed in love.load and drawTiles() in love.draw pass through same values for w, h on each function
To best use the functions defineTile() should be placed in love.load and drawTiles() in love.draw pass through same values for w, h on each function
Code: Select all
tileGrid = {
tile = {},
defineTile = function(w, h)
sw, sh = love.window.getMode()
tileWidth = sw/w
tileHeight = sh/h
num = 0
for j = 0, h-1 do
for i = 0, w-1 do
num = num + 1
table.insert(tileGrid.tile, num, {tileWidth * i, tileHeight * j, false})
end
end
return tileWidth, tileHeight
end,
drawTile = function(w, h)
sw, sh = love.window.getMode()
tileWidth = sw/w
tileHeight = sh/h
num = 0
for j = 0, h-1 do
for i = 0, w-1 do
num = num + 1
love.graphics.setColor(i/w, j/h, 1)
rect = {
love.graphics.rectangle("line", tileWidth * i, tileHeight * j, tileWidth, tileHeight),
love.graphics.print(num, tileWidth * i, tileHeight * j)
}
end
end
end
}