Advertisement
Guest User

Untitled

a guest
Jan 27th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. show = false
  2.  
  3. game = {
  4.    state = {
  5.       menu = true,
  6.       run = false
  7.    }
  8. }
  9.  
  10. buttons = {
  11.    menu = {},
  12.    run = {}
  13. }
  14.  
  15. function button (text, fn)
  16.    return {text = text, fn = fn}
  17. end
  18. function start()
  19.    game.state['menu'] = false
  20.    game.state['run'] = true
  21. end
  22. function love.load()
  23.    table.insert(buttons.menu, button('Play', start))
  24.    table.insert(buttons.menu, button('Exit', love.event.quit))
  25. end
  26. function love.draw()
  27.    if game.state['menu'] then
  28.       local sx, sy = love.graphics.getWidth() * (1/3), 50
  29.       local margin = 10
  30.       local tot = (sy + margin) * #buttons.menu
  31.       local cursor = 0
  32.       -- initialize the button to false
  33.       currentButton = false
  34.       for i,s in ipairs(buttons.menu) do
  35.          local x, y = love.graphics.getWidth() * 0.5 - sx * 0.5, love.graphics.getHeight() * 0.5 - (tot * 0.5) + cursor
  36.          mx, my = love.mouse.getPosition()
  37.          -- make this local just so its easier to make sense of the code
  38.          local hot = mx > x and mx < x + sx and my > y and my < y + sy
  39.          if hot and show then            
  40.             buttons.menu[i].fn()
  41.          end
  42.          local color = {0.4, 0.4, 0.5, 1}
  43.          if hot then
  44.             color = {0.8, 0.8, 0.9, 1}
  45.          end
  46.          love.graphics.setColor(unpack(color))
  47.          love.graphics.rectangle('fill', x, y, sx, sy)
  48.          cursor = cursor + sy + margin
  49.          font = love.graphics.newFont(20)
  50.  
  51.          love.graphics.setColor(0, 0, 0)
  52.          love.graphics.printf(s.text, font, x, y + (sy * 0.5) - 10, sx, 'center')
  53.       end
  54.       --if show == true then
  55.       --  buttons.menu[play]:button.fn
  56.       --end
  57.       show = false
  58.    end
  59. end
  60.  
  61. function love.mousereleased (x, y, button)
  62.       if button == 1 then
  63.          show = true
  64.       end
  65. end
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement