Last active
March 16, 2025 11:12
-
-
Save Achie72/37043397d1057d8fab49b10fff0f9865 to your computer and use it in GitHub Desktop.
How to center any text or print with borders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- a little helper function to print centered text | |
function print_center(text, x, y, color) | |
-- get the lenght of the text | |
-- if you did not know, print returns the position of the last character | |
-- so fi you print offscreen, starting from 0 you will get the exact lenght of the text | |
local len = print(text, 0, 400, color) | |
print(text, x-(len/2), y, color) | |
end | |
function print_outline(_txt,_x,_y,_clr,_bclr,_thick) | |
-- if we don't pass a background color set it to white | |
-- thickness if 1 default | |
if _bclr == nil then _bclr = 7 end | |
if _thick == nil then _thick = 1 end | |
-- draw the text with the outline color offsetted in each direction | |
-- based from the original text position we want to draw. | |
-- This will create a big blob of singular colors, which's outliens | |
-- will perfectly match the printed text outline | |
for x=-_thick,_thick do | |
for y=-_thick,_thick do | |
print(_txt, _x-x, _y-y, _bclr) | |
end | |
end | |
-- draw the original text with the intended color in the middle | |
-- of the blob, creating the outline effect | |
print(_txt, _x, _y, _clr) | |
end |
If you would like to read more like this, feel free to checkout my Ko-fi articles, with many devlogs and code rundowns like this!
https://github.com/Achie72/kofi-articles
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More PICO-8 Tutorials: https://github.com/Achie72/kofi-articles/blob/main/README.md#pico-8-tutorials