Notes

Love2D Trick: Text on Screen Center

29 May 2026

The graphics.printf function in Love2D allows you to print text on the screen. It’s a powerful function and one of the simplest forms of calling it is love.graphics.printf(text, x, y, limit, align)

---@param text string # A text string.
---@param x number # The position on the x-axis.
---@param y number # The position on the y-axis.
---@param limit number # Wrap the line after this many horizontal pixels.
---@param align? love.AlignMode # The alignment.

Here is the trick to center text horizontally on the screen:

love.graphics.printf("Game Over", 0, 300, getWidth(), "center") # 300 is just a manual position for y

The lesson to learn here is that the x and y defines not the position of the text itself, but the position of the invisible text box. This caught me by surprise. The mental model becomes that limit defines the width of a text box, and align specify the alignment of the text within that box.

So the call is saying: “Draw a text box that starts from the left edge of the screen (0) to the right edge of the screen (getWidth()), and the text is centered within that box.”