Page 1 of 1

fixed

Posted: Wed Mar 06, 2024 7:00 pm
by uknowntgyallyonski
sorted

Re: help cleaning up my code

Posted: Wed Mar 06, 2024 8:19 pm
by BrotSagtMist
Start by using [co​de][/co​de] in this forum. :x

Then, why are you creating functions that only contain 1-3 lines of code?
There is no point adding extra lines just for the sake of splattering functionality all over the place.

Re: help cleaning up my code

Posted: Wed Mar 06, 2024 8:29 pm
by RNavega
TBH, the way that they create many threads, how they don't use the code tags, plus asking basic questions that hint to a skillset level where they shouldn't be able to write multi-state game code in the first place, makes me think that they did not write this code but got it from somewhere else, like an AI.

Edit: hm, also the way that they phrased their question: "make it more presentable". For whom? Your instructor or teacher? I'm not against people asking for help with homework / assignments, but to not mention it makes it seem like you're trying to get others to do your homework for you.

Re: help cleaning up my code

Posted: Wed Mar 06, 2024 8:50 pm
by Xugro
I would suggest the opposite of BrotSagtMist:
Create more functions that only contain 1-3 lines of code for better readability.

As an example your function love.mousepressed() has 5 indentation levels. With some easy refactorings you can get this down to two or three and have an easier time to read the functions:

Code: Select all

function love.mousepressed(x, y, button)
    if gameState == "menu" or gameState == "difficulty" or gameState == "instructions" then
        local leftMouseButton = 1
        if button == leftMouseButton then
            handleMouseOutsideOfPlaying(x, y)
        end
    end
end

function handleMouseOutsideOfPlaying(x, y)
    if gameState == "menu" then
        handleMouseInMenu(x, y)
    elseif gameState == "difficulty" then
        handleMouseInDifficulty(x, y)
    elseif gameState == "instructions" then
        handleMouseInInstructions(x, y)
    end
end
Also you can replace most comments with meaningful named variables, constants or function names - e.g. leftMouseButton.

Edit:
Don't double check your conditions and the code gets even easier:

Code: Select all

function love.mousepressed(x, y, button)
    local leftMouseButton = 1
    if button ~= leftMouseButton then
        return
    end

    if gameState == "menu" then
        handleMouseInMenu(x, y)
    elseif gameState == "difficulty" then
        handleMouseInDifficulty(x, y)
    elseif gameState == "instructions" then
        handleMouseInInstructions(x, y)
    end
end
Additionally you can use guard clauses and early returns for fewer indentations and better readability.

Re: fixed

Posted: Thu Mar 07, 2024 6:30 pm
by zorg
Also for the love of everything, do not blank your original post or edit the topic to "FIXED", that's quite disrespectful to anyone that would have learned something from whatever it was.