How to: Toggling platforms using accelerometer

Hi, I'm Kieran, also known as ReverendMalerik, and now that my game 'Ribbit Rabbit!' is finished and out in the wild (Ribbit Rabbit for the Panic Playdate! by reverendmalerik ) I thought it would be nice to break out a few of the features of the game that might be useful to other people and make them into little how-to guides.

In Ribbit Rabbit there are a series of platforms on the later levels that switch between three positions on the level when you tilt/shake the device sufficiently. In this guide I will explain how this is achieved so that hopefully you can integrate a similar effect into your own projects!

The process is actually quite simple. Two pieces of code are needed. One on the 'game' script and one on the script for each of the platform tiles you wish to swap out using this function. Unlike my other guide on crank-activated platforms (How to: crank powered moving platforms), these work anywhere on the map.

The game script I use is as follows:

// this check is done every frame using the 'loop' event
on loop do

        // In order to prevent the system from changing the platform location twice (once when you tilt the playdate, once when you tilt it back to a comfortable position) we need to implement a delay between each time this will trigger. This is the 'countdown' variable. This checks if the countdown variable has hit 0 yet and if it hasn't reduces it by one.
	if countdown>0 then
		countdown--
	end

        // This checks if the current 'event.orientation' matches the previous frame's 'event.orientation', which is stored in the keeporientation variable. If they don't match, the playdate has been tilted and we move to the next check.
	if event.orientation!=keeporientation then

                // has it been at least 40 frames since the playdate was last tilted? then emit the 'systemshaken' event and set the countdown to 40, preventing 'systemshaken' from triggering for the next 40 frames.
		if countdown==0 then
			emit "systemshaken"
			countdown = 40
		end
	end

        // store the current orientation in the 'keeporientation' variable, ready to be read next frame.
	keeporientation = event.orientation
end

The event.orientation variable only changes when the playdate is tilted about 90 degrees or so, so this shouldn't trigger when the playdate is just slightly moved by normal play.

So now we have the game emitting the 'systemshaken' event when tilted. Now to make this event actually do something we apply the following code to the tiles we want to change:

// on the event being emitted
on systemshaken do

        // temporarily take player control away so they don't end up dying as the platforms shift.
	ignore

        // shake the screen to alert the player to the change
	shake 1 then

                // change the tile from the current tile to the next in the 3 tile sequence 
		swap 393

                // this runs a function on the player to check that the player is not now standing on an empty tile. This ensures that if the player tilts the system whilst standing on a platform that shifts they will die as they should, instead of remaining in mid-air. I won't paste the code here as it is VERY big and exclusively useful to Ribbit Rabbit, so you won't need it.
		tell "player" to
			call "tilecheck"
		end

                // Restore control to the player.
                listen
	end
end

So you can see that this changes the tile in question from one tile to another when the system is tilted. There are two more tiles with near identical scripts making a sequence of three which switch between each other. You could theoretically make this sequence switch as many times as you want by just chaining platforms in that fashion.

And there you have it, a fully working system to shift platforms using the accelerometer! I hope someone finds it useful! If you have any questions please feel free to ask and I will try my best to answer.

As I stated at the top I will be posting more tutorials based on features from my game 'Ribbit Rabbit!' as I get time. In the meantime, please check it out on itch (Ribbit Rabbit for the Panic Playdate! by reverendmalerik ) and try the free 4 level demo!