I'm unbelievably bad with challenges ... as I can't focus, not even for 15 min 🤣 to finish what I was challenged for 🙄 I wanted to centre text, and as we know, you need to start it with the left pixel, and then it extends to the right. I believe that solutions are already available on the internet, but I want to share my own solution, as it was a valuable learning experience.
Known is:
- The width of the screen is 128px ... 128
- The character/letter (and a space!) takes up 4px, 3px for the letter, and 1px for a gap ... 4
- The length of text or the number of letters (and spaces!) in text/string ... N
- Print statement ... PRINT(STR, X, Y, [COL]) ... searching for X
Formula: X = (128 - N * 4) / 2 ... or shorter ... X = 64 - 2 * N (this one you can do in your head).
Then, soon, I find out that I don't want to count the number of letters in "SpaceCat's Game Dev Heroes!" (27 if you wonder 😉). It's time for a function()!
function print_centered(text,top,col_id)
local left = 64 - 2 * #text
print(text,left,top,col_id)
end
And I use it like this:
print_centered("SpaceCat's Game Dev Heroes!",100,11)
So, instead of finishing the challenge, I spent time on this ... cheers, Marko 🎮