1、我们创建一个类 Bag ,用来实现背包系统的功能。首先,我们需要一个背包的图标,用于点击打开和关闭背包。这里不用图片做为图标。 self.x = 30 self.y = 30 self.width = 100 self.height = 50用这个矩形来代表背包。然后设置背包的点击属性。self.click = false再然后,我们创建一个三维数组,二维也可以,用来存储道具。--背包格子 self.BAG = {} self.BAG.page = 3 self.curPage = 1 self.BAG.width = 4 self.BAG.height = 3 self.BAG.Size = 120 self.bag = {} for pg=1,self.BAG.page do self.bag[pg] = {} for i=1,self.BAG.height do self.bag[pg][i] = {} for j=1,self.BAG.width do self.bag[pg][i][j] = {} self.bag[pg][i][j].value = 0 --表示未占用 end end end最后,如果是二维数组可以跳过这一步。创建一个下一页的按钮,用来控制页数self.nextPage = { x = self.BAG.width*self.BAG.Size - 150, y = (self.BAG.height+1)*self.BAG.Size+10, width = 60, height = 40, Click = false }
2、控制背包的打开和关闭最好在love.pressed(x,y,key) 回调函数中进行。这样不用多余的逻辑就可以实现。我们创建Bag:m_pressed(x,y,key)函数,并在love.pressed(x,y,key) 回调函数中调用它。function Bag:m_pressed(x,y,key) if x > self.x and x < self.x+self.width and y > self.y and y < self.y + self.height then self.click = not self.click end if self.click == true and x > self.nextPage.x and x < self.nextPage.x+self.nextPage.width and y > self.nextPage.y and y < self.nextPage.y + self.nextPage.height then self.nextPage.click = not self.nextPage.click self.curPage =self.curPage +1 if self.curPage >=4 then self.curPage = 1 end endend
3、设置好基本的属性之后,我们创建一个Bag:draw()函数用来绘制背包。然后在love.draw() 回调函数里面调用Bag:draw()函数。新建一个函数bbl()用来绘制背包。1、首先,在Bag:draw()函数里面,我们绘制背包图标,然后判断背包的状态是打开还是关闭,如果是打开再在bbl()函数中绘制背包栏。function Bag:draw(...) -- body local font = love.graphics.getFont() love.graphics.setFont(zh_font) if self.bagImage == nil then lg.setColor(self.backgroundcolor) lg.rectangle("fill",self.x,self.y,self.width,self.height) lg.setColor(self.fontcolor) lg.print("BAG",self.x+40,self.y+20) else lg.draw(self.bagImage,self.x,self.y) end if self.click == true then bbl(self) endend2、然后我们在bbl函数中绘制背包栏边框,下一页按钮,已经存入的道具。function bbl(bag)--背包栏 -- body --画外框 --bblisExit = true love.graphics.setColor(80, 20, 30, 200) love.graphics.rectangle("fill", 100, 100, 520, 450) love.graphics.setColor(200, 20, 30, 200) love.graphics.rectangle("line", 100, 100, 520, 450) love.graphics.setColor(10, 150, 100, 255) --for pg=1,bag.BAG.page do local pg = bag.curPage for i=1,bag.BAG.height do for j=1,bag.BAG.width do local bx = j*121 local by = i*121 love.graphics.rectangle("line", bx, by, bag.BAG.Size, bag.BAG.Size) if bag.bag[pg][i][j].value ~= 0 then love.graphics.draw(bag.bag[pg][i][j].img, bx, by) end end end --end love.graphics.rectangle("line", bag.nextPage.x, bag.nextPage.y, bag.nextPage.width, bag.nextPage.height) love.graphics.setColor(100, 100, 255, 200) love.graphics.print("next", bag.nextPage.x+15,bag.nextPage.y+10) love.graphics.print(tostring(pg).."/"..tostring(bag.BAG.page),105,105)end
4、以上,我们实现了一个背包的UI,但是一个背包系统不仅仅只有UI,我们至少还需要获取到鼠标点击到的格子的状态和获取其在背包数组中的位置,这样才能便于我们施行对应的操作。所以我们创建一个函数 getij()用来获取鼠标所在项在背包数组中的位置。function Bag:getij(bag,x,y) -- body for i=1,bag.BAG.height do for j=1,bag.BAG.width do if x > j*121 and x < j*121+121 and y > i*121 and y < i*121 +121 then return i,j end end endend获取到鼠标所在项在背包数组中的位置后,我们就可以在Bag:draw()函数中实现道具说明if self.click ==true then local pg = self.curPage local mx,my = love.mouse.getPosition() local i,j = Bag:getij(self,mx,my) if i ~= nil and self.bag[pg][i][j].value ~= 0 then love.graphics.setColor(25, 100, 255, 100) --print(getalldata(self.bag[pg][i][j].shuxing)) love.graphics.rectangle("fill", j*121+121, i*121-25, 150, 250) love.graphics.setColor(100, 100, 200, 100) love.graphics.rectangle("line", j*121+121, i*121-25, 150, 250) end end我们可以在这个方框内显示装备的属性。
5、一个背包要能够放进去和取出来道具,我们创建Bag:add()和Bag:del()两个函数用来实现放进去和取出来的操作。放进去:function Bag:add(image,shuxing) -- body for pg=1,self.BAG.page do for i=1,self.BAG.height do for j=1,self.BAG.width do if self.bag[pg][i][j].value == 0 then self.bag[pg][i][j].value = 1 self.bag[pg][i][j].img = image self.bag[pg][i][j].shuxing=shuxing --self.bag[pg][i][j].shuxing.gjl = shuxing.gjl return end end end endend取出来:function Bag:del(bag) local i,j = Bag:getij(bag,love.mouse.getPosition()) if i == nil then return end local pg = bag.curPage bag.bag[pg][i][j].value = 0 bag.bag[pg][i][j].img = nil bag.bag[pg][i][j].shuxing=nilend
6、最后,在main.lua中调用Bag类的函数,实现背包系统。