How to show some text when the system menu opens

This ended up being easier than I expected, so I thought I'd share.

function playdate.gameWillPause()
	local img = gfx.getDisplayImage()
	gfx.lockFocus(img)
	local bgRect = playdate.geometry.rect.new(20, 20, 160, 200)
	local textRect = playdate.geometry.rect.new(30, 30, 140, 180)
	gfx.setColor(gfx.kColorWhite)
	gfx.fillRoundRect(bgRect, 10)
	gfx.setColor(gfx.kColorBlack)
	gfx.drawRoundRect(bgRect, 10)

	-- this is the important bit here.
	-- You can of course create this string however you like,
	-- including adding a score or level or whathaveyou.
	local text = "Text to show on the left side of the screen goes HERE."

	-- this line actually draws the text. You only need the first two parameters.
	-- See https://sdk.play.date/1.11.1/Inside%20Playdate.html#f-graphics.drawTextInRect for more details.
	gfx.drawTextInRect(text, textRect, 0, "...", kTextAlignment.center)

	gfx.unlockFocus()
	playdate.setMenuImage(img, 0)
end

This code was adapted from the FlippyFish example project in the SDK.

2 Likes