Inventory

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Inventory

Post by theobjpo »

Code: Select all

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)?
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Inventory

Post by stout »

Here is a post where someone helped me work out how to do pages of text; probably will help for this.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inventory

Post by Roland_Yonaba »

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.

Code: Select all

-- as before
 love.graphics.draw(inventory[i], inventory.x+i*32+5+(i*5), inventory.y*(i)+5)
-- as before
theobjpo wrote: As you can see I have those weird algorithms, is there any way of making them simpler?
Err...Not sure I got this one. actually, what is weird in it, to you ?
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Inventory

Post by substitute541 »

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

Code: Select all

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...
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests