0
\$\begingroup\$

It's supposed to be the code for the main menu of a pong game I'm writing using LÖVE. I believe no knowledge on it is necessary for understanding, since it seems very explicit. I'm mostly unsure if its readability is impacted by the order of declarations and whether I should include methods in the object definition itself or define them later with function Object:method(). I just want someone to be able to easily skim through the code and understand what it's doing.

local Menu = {}

local Title = {}

function Title:print (self)
    local x = 0
    local y = 24
    local limit = 100
    
    love.graphics.setFont(Font.mono16)
    love.graphics.printf("LOVE PONG", x, y, limit, "center")
end

local Option = Object:extend()

function Option:new(label, fn)
    self.label = label or nil
    self.fn = fn or nil
end

local OptionList = {
    Option("Play", function () changeGameState(GameStates.game) end),
    Option("Quit", love.event.quit),
    
    optX = 35,
    optY = 40,
    spaceBetween = 10,
    cursorX = 28
}

local currOption

function OptionList:print()
    love.graphics.setFont(Font.dialog)
    
    for i, opt in ipairs(self) do
        local y = self.optY + (self.spaceBetween * i)
        love.graphics.print(opt.label, self.optX, y)

        if i == currOption then
            love.graphics.print(">", self.cursorX, y)
        end
    end
end

function Menu.load()
    currOption = 1
    love.graphics.setColor(Color.white)
end

function Menu.draw()
    Title:print()
    OptionList:print()
end

function Menu.update(dt)
end

function Menu.keypressed(key, scancode, isrepeat)
    if scancode == "down" then
        currOption = currOption + 1
    elseif scancode == "up" then
        currOption = currOption - 1
    elseif scancode == "return" or scancode == "z" then
        OptionList[currOption].fn()
    end

    if currOption < 1 then
        currOption = 2
    elseif currOption > 2 then
        currOption = 1
    end
end

return Menu
```
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Is the game working yet? We can only review code that is working as intended. We can tell you how to write the code before it is written, we can only comment on working code. \$\endgroup\$
    – pacmaninbw
    Feb 19 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.