inventory = { x=400, y=20,
}
function inventory.load()
end
function inventory.add(id)
table.insert(inventory, love.graphics.newImage("Items/".. id ..".bmp"))
end
function inventory.update(dt)
end
function inventory.draw()
love.graphics.setColor(0,0,0,120)
love.graphics.rectangle("fill", inventory.x+37, inventory.y, 32*4+5+(4*5), 32*4+5+(4*5))
love.graphics.setColor(255,255,255,255)
if inventory[1] then
for i = 1,#inventory do
love.graphics.draw(inventory[i], inventory.x+i*32+5+(i*5), inventory.y+5)
end
end
end
function inventory.mousepressed(x,y,z)
end
As you can see I have those weird algorithms, is there any way of making them simpler?
Also, When you have 5 items, it draws them on the same y. How would I change this so after 4 items it goes to the first slot of the next y (inventory.y+5)?
theobjpo wrote:
Also, When you have 5 items, it draws them on the same y. How would I change this so after 4 items it goes to the first slot of the next y (inventory.y+5)?
Well, inventory.y doesn't change in the for loop, so inventory.y+5 remains constant.
Consider using the local i, as you already do on x-axis.
theobjpo wrote:
Also, When you have 5 items, it draws them on the same y. How would I change this so after 4 items it goes to the first slot of the next y (inventory.y+5)?
You need a nested for-loop for that. Here's an example
for y=1, columns do
for x=1, rows do
for i=1, #somerandomtable
love.graphics.draw(inventory[i], inventory.x+x*32+5+(x*5), inventory.y+(y * 5))
end
end
end
Change columns and rows to the numbers you want.
It helps to plot before you do anything complex...