I noticed in my game that if I press the Menu button to pause, when I press it again, I'm immediately thrown into a situation where I'm going to die immediately.
To mitigate this circumstance, I wanted a "countdown", so the player would have a little time to get readjusted to the game and its controls before resuming.
This is the code I wrote to make this happen. I save off my normal playdate.update()
routine, swap in a temporary one that displays the countdown, then swap it back in at the end. I'm sure this could be done better, so feel free to post improvements!
Just in case anyone finds this useful!
local gfx <const> = playdate.graphics
…
local lastFrame = nil
function playdate.gameWillPause()
-- conditionally save off the current frame so we can do a countdown animation when coming back
-- only do this if gameplay is happening (that's what the "ModePlay" check does; your game will need to do a different check)
if Mode.getCurrentMode().className == "ModePlay" then
lastFrame = gfx.getDisplayImage()
end
end
function playdate.gameWillResume()
-- only run this routine if a screen image was saved off
if lastFrame == nil then return end
-- do a countdown so player isn't thrown right back into the middle of things
-- swap in my own playdate.update routine in favor of the standard one
local saveOffUpdateFunction = playdate.update
local frames = 3 * playdate.display.getRefreshRate()
playdate.update = function()
gfx.setColor( gfx.kColorBlack )
gfx.setImageDrawMode( gfx.kDrawModeFillBlack )
lastFrame:drawFaded( 0, 0, 0.5, gfx.image.kDitherTypeBayer2x2 ) -- draw game image, faded
gfx.fillCircleAtPoint( 200, 112, 18 ) -- draw background for countdown number
gfx.setImageDrawMode( gfx.kDrawModeFillWhite )
gfx.setColor( gfx.kColorWhite )
gfx.drawCircleAtPoint( 200, 112, 18 ) -- draw outline for circle
local countdown = ( frames // playdate.display.getRefreshRate() ) + 1
gfx.drawTextAligned( string.format( "_%d_", countdown ), 200, 105, kTextAlignment.center )
frames -= 1
if frames == 0 then
playdate.update = saveOffUpdateFunction -- restore
lastFrame = nil -- don't need to save any longer
end
end
end