A small update this time around. Having two ghosts chase one other identical ghost around the maze was getting a bit silly, so time for a player character.
I drew up a quick puck-man in Resprite, with three frames per direction:
.. and loaded that in as puckTable:
puckTable = playdate.graphics.imagetable.new('Images/puck')
assert( puckTable )
Then, when setting up the player sprite, you can configure multiple states:
playerSprite = AnimatedSprite.new(puckTable)
playerSprite:addState("l", 4, 6, {tickStep = 4, yoyo = true})
playerSprite:addState("u", 7, 9, {tickStep = 4, yoyo = true})
-- etc
I set the name of each to my directions (u for up, l for left, etc), and then we give the start and end frames for the state – so frames 4 to 6 for left, for example. Yoyo enables, well, yoyo animation – back and forth through the frames – while tickStep is a speed controller.
Finally, we can then call one of those directional animation states when we change direction:
if (playerDirection ~= requestedDir and requestedDir ~= "n") then
-- change direction requested, but is that possible?
if checkForOpenPath(mapRefX,mapRefY,requestedDir) then
-- yup change direction
playerDirection = requestedDir
playerSprite:changeState(playerDirection)
end
end
Finally, I also renamed the function controlling the player from moveGhosty to movePuck. There, much less confusing.
Result: one chomping, moving puck-man:
Next up: a game loop reset, lives, and showing the score somewhere I guess?
Full source code here.