My brain is frying right now, please help me.
This is the code for the game.
Code: Select all
local Button = require('libraries.button')
Button:setFont(love.graphics.newFont(16))
function love.load()
button1 = Button.new(
10, 10, 200, 30,
{255/255, 0, 0, 100/255},
{255/255, 0, 0, 255/255},
{255/255, 255/255, 255/255, 255/255},
"Say random text"
)
button2 = Button.new(
10, 60, 200, 30,
{255/255, 0, 0, 100/255},
{255/255, 0, 0, 255/255},
{255/255, 255/255, 255/255, 255/255},
"Say random text"
)
button1.fn1 = function()
love.event.quit(1)
end
end
function love.update(dt)
button1:update()
button2:update()
end
function love.draw()
love.graphics.clear()
button1:draw()
button2:draw()
end
Code: Select all
local Button = {}
function Button:setFont(font)
self.font = font
end
function Button.new(x, y, w, h, color, highlightColor, textColor, text, fn1, fn2, fn3)
local newButton = {
x = x, y = y,
w = w, h = h,
color = color, highlightColor = highlightColor, textColor = textColor,
text = text, fn1 = fn1, fn2 = fn2, fn3 = fn3, highlighted = false
}
function newButton:update()
local x, y, w, h = self.x, self.y, self.w, self.h
local mouseX, mouseY = love.mouse.getPosition()
if(mouseX > x and mouseY > y and mouseX < x + w and mouseY < y + h)then
self.highlighted = true
else
self.highlighted = false
end
if(love.mouse.isDown(1))then
if(self.fn1)then
self.fn1()
love.timer.sleep(0.1)
end
elseif(love.mouse.isDown(2))then
if(self.fn2)then
self.fn2()
love.timer.sleep(0.1)
end
elseif(love.mouse.isDown(3))then
if(self.fn3)then
self.fn3()
love.timer.sleep(0.1)
end
end
end
function newButton:draw()
local gameWidth = love.graphics.getWidth()
local gameHeight = love.graphics.getHeight()
local x, y, w, h, color1, color2, color3, highlight, text, font = self.x, self.y, self.w, self.h, self.color, self.highlightColor, self.textColor, self.highlighted, self.text, Button.font
local textWidth = font:getWidth(text)
local textHeight = font:getHeight(text)
if(highlight)then
love.graphics.setColor(color2)
else
love.graphics.setColor(color1)
end
love.graphics.rectangle('fill', x, y, w, h)
love.graphics.setColor(color3)
love.graphics.print(
text, font,
x+10,h/2
)
end
return newButton
end
return Button