Alexa Game and Animation Experiments (APL)
Tags: apl, alexa, amazon, skill
Published: 2019-05-17 22:03:49

The purpose of this article is to share what I’ve done over the last two months by using (and misusing) Alexa Presentation Language (APL) to create visual games for Alexa. I will highlight animation, as I’ve spent the majority of my time making that work.


## Background

I’ve been exploring the Alexa game space for fun and just launched my first personal Alexa game skill: a voice-controlled version of the popular game 2048 ([UK version](https://www.amazon.co.uk/Team-Quasko-2048/dp/B07PTYCXFL), [US version](https://www.amazon.com/Team-Quasko-2048/dp/B07PTYCXFL), others coming soon) built with screen devices in mind (Spot, Show, FireTV). This game has a million clones across mobile and web, but I didn’t find an Alexa version. [Demo video](https://youtu.be/RMc_pFn1MbM).

Although I have used [APL](https://developer.amazon.com/docs/alexa-presentation-language/apl-overview.html) previously, for these games, I dove deep into the limits of functionality, response size, and performance. One of the big blockers I ran into was the lack of game-type animation available through existing APL [components](https://developer.amazon.com/docs/alexa-presentation-language/apl-component.html) and [commands](https://developer.amazon.com/docs/alexa-presentation-language/apl-commands.html).

Faced with this challenge, I figured out how to make animation work (X and/or Y axis movement for world and objects; sprite-based animation). If you’ve seen some of my [other posts](https://www.linkedin.com/in/willblaschko/detail/recent-activity/posts/) or followed some of my [projects](https://forum.xda-developers.com/shield-tv/general/alexa-media-voice-control-android-t3784032), you know I don’t let convention or intended use limit me. For this, I broke from APL convention to get the results I needed.


## TLDR; What’s Possible?

If you don’t care about my projects and want to get to the meat of the content, here you go! There are likely more possibilities, but here’s what I’ve made work so far:

**X or Y axis world animation**: using a [Sequence](https://developer.amazon.com/docs/alexa-presentation-language/apl-sequence.html) (or nested Sequences) you can break up the background into tiles (in [pixels or percents](https://developer.amazon.com/docs/alexa-presentation-language/apl-data-types.html#dimension) of viewport) and use [Scroll](https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#scroll-command) commands [Sequentially](https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#sequential-command) to move the world. [Example background animation here](https://youtu.be/OAHiOijGRIw) (video).

[Gist here](https://gist.github.com/willblaschko/579998deb99366be7c7876b80ecac7e2)

**X or Y axis item animation**: using a Sequence with small, hidden [items](https://developer.amazon.com/docs/alexa-presentation-language/apl-sequence.html#sequence-children), you can animate the primary item in the Sequence around the screen by using Scroll commands Sequentially. I used 1vh square empty [Frames](https://developer.amazon.com/docs/alexa-presentation-language/apl-frame.html) to accomplish this. [Example item animation here](https://youtu.be/MKBc_0Dpr5g) (video).

[Gist here](https://gist.github.com/willblaschko/579998deb99366be7c7876b80ecac7e2)

**Frame-based sprite animation**: using a Pager you can approximate a sprite animation by flipping between the different pages using [SetPage ](https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#setpage-command)commands Sequentially. [Example sprite animation here](https://youtu.be/OAHiOijGRIw) - “bird” (video).

[Gist here](https://gist.github.com/willblaschko/f8f86ccb4598c6bf4125f4ebde3a7b81)

**200+ element APL documents**: By being smart about reuse of [Layouts](https://developer.amazon.com/docs/alexa-presentation-language/apl-layout.html), you can fit hundreds of APL components into the response sent to Alexa. This allows for complex scenes to be built and minimizes [RenderDocument](https://developer.amazon.com/docs/alexa-presentation-language/apl-render-document-skill-directive.html) directives.

[Gist here](https://gist.github.com/willblaschko/e72fe9359fb8cb605484819eec3583ab)

Combine these together and you could get a pretty decent top-down 16-bit style game.


## Projects

I will touch on 2048 and two other projects I currently have underway (both of which may never be seen by the public). I wanted to experiment with a variety of game types so I created a board game, a first person RPG, and a 2D side scroller/platformer. My personal goals for all these projects were 1) use [RenderDocument](https://developer.amazon.com/docs/alexa-presentation-language/apl-render-document-skill-directive.html) directives only when absolutely necessary (a scene change) 2) animate everything possible and 3) create games that were enjoyable and sticky.

I will keep the below relatively code-light, see the gists above or the related videos for more details.


### 2048 (Board Game)


![2048 Game](https://s3.amazonaws.com/alexa-posts/ss_2048.jpg)


As the only skill for this piece launched so far, this one is the most polished and playable. I’d suggest [trying it out](https://www.amazon.com/Team-Quasko-2048/dp/B07PTYCXFL) to get a feel for how it works. You can play it on headless (no screen) devices, but it’s not the best experience. [Video here](https://youtu.be/RMc_pFn1MbM).


#### Challenges

There were two primary challenges for this project: 1) adapting to different screen sizes and 2) rendering and controlling all the tiles and keeping it within performance and response size limitations.

1) I solved through some ternary operations based on viewport shape and width/height ratio. These calculations are used to place the X/Y grid of objects (Containers within a Container) in the correct location based on screen size/shape.


    ${@viewportShape == @viewportShapeRound ? viewport.width / 6 : (viewport.width > viewport.height ? viewport.height / 4 : viewport.width / 4)}

2) I created backgrounds and pagers for each tile (4x4 = 16) and created a custom Tile layout, which eliminated repeated code and greatly reduced response size. By simplifying these Tiles, I also reduced the number of rendered elements.

    ...{
        "type": "Tile",
        "name": "pos1"
    }...

#### Goals

The only time the game receives a new RenderDocument is if you customize the tiles or background (“Alexa, customize”). Everything else is controlled by Pager commands, including the scoreboard. In theory, both the tiles and scoreboard should animate, I may play with the timing of the commands a little to see if I can get this to work. Stickiness: I personally quite enjoy this game and played it well beyond what was required to test it myself.


### RPG Name TBD (First Person RPG)


![RPG Game](https://s3.amazonaws.com/alexa-posts/ss_rpg.jpg)
 

My long-term goal for this game is to create a procedurally-generated RPG game where you fight monsters, complete quests, and gather treasure. If you want to play a game that has already done this well for voice (in my opinion), check out [Skyrim](https://www.amazon.co.uk/Bethesda-Game-Studios-Special-Edition/dp/B07D6STSX8).

I wanted visuals and animation. What’s the point of “taking a mighty swing at the troll” if all you have is a static image on the screen? Exactly. See [this video](https://youtu.be/MKBc_0Dpr5g) for an early POC.

Unrelated, for this project, I needed to be able to return a sequence of Speak responses to Alexa, so I created a modified [RequestHandler](https://gist.github.com/willblaschko/cc4f0019da90e8c0b388bf1ae42bca20) (gist) to be used with a [RequestInterceptor](https://developer.amazon.com/blogs/alexa/post/0e2015e1-8be3-4513-94cb-da000c2c9db0/what-s-new-with-request-and-response-interceptors-in-the-alexa-skills-kit-sdk-for-node-js).


#### Challenges

1) How can I animate the weapon and the monster and 2) create a layered environment that gives a sense of depth?

For #1, it was time to break Sequences. As the only moving component (Pagers are limited in that they only display one item), I had to figure out how to control the speed and direction of movement granularly. The solution was invisible components, pixels or percents of view height/width in size, that I could [Scroll](https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#scroll-command) to using a timed [Sequential](https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#sequential-command) command. With this, I have dancing creatures, attacking weapons, and about any other animation I could want on the X or Y axis.

For #2, this was easily solved with layered elements inside a container using position: absolute (more [here](https://medium.com/@punkpocko/alexa-presentation-language-293f8cf25a09)). This allowed me to create multiple foregrounds and backgrounds.


#### Goals

I want to see if it’s possible to use less RenderDocument directives, currently one is shown every scene change (movement), or when a monster dies. I could likely use Pagers to switch between the different backgrounds/foregrounds, but the load isn’t as abrupt here as it was for 2048. The monsters and attacks are animated, as I develop this more, I expect to add additional animations. This game needs a lot of polish but both the APL and voice experience have a solid base. With more, better content and direction (and a better name), this game has potential to be sticky on both screen and voice-only devices.


### Flappy Alexa (2D Side Scroller/Platformer)


![Flappy Bird Game](https://s3.amazonaws.com/alexa-posts/ss_flap.jpg)


[Flappy Bird](https://en.wikipedia.org/wiki/Flappy_Bird) came and went in popularity but after the success with animating the RPG game, I wondered if it would be possible to use the same strategy to create side-scrollers and platformers. Short answer: yes, but only if you want to invest the time. I was able to throw together a quick Flappy Alexa POC in about 4 hours, it’s about as rough as you’d expect and probably won’t get much further attention. [The video](https://youtu.be/OAHiOijGRIw) shows blocks (use your imagination) simulating the basic gameplay, all rendered and, theoretically, playable on a real device.

With the right design and math, there’s no reason this couldn’t be used to create a 2D map or platformer that automatically animates as the character “moves” around.  The character could also move on the X or Y axis as required for storytelling using the same technique. 16-bit tile based games, anyone? Turn-based would be even easier.

The bird itself is a Pager component, if you watch the video, you see the transition. This same behavior could be used with an animation sprite to do rough frame-based animation.


#### Challenges

I quickly hit the hard limit for the Alexa response size between the map information and the animation commands. If this were not a pre-rendered sequence, you’d have more flexibility in design and animation. The payload is the full map information (we only see about 30% of it in the video) and about 500 distinct commands to animate the map and the bird. If this were real-time, you’d only need ~100 commands between user interactions for most use cases.


#### Goals

There are zero RenderDocument directives after the initial map load, everything is done through Pager or Sequence commands. Everything is animated. And the game could be sticky, but I wouldn’t suggest a fast twitch game like this using voice, maybe with Echo Buttons. Make it turn-based with a good voice experience and you have something here.


## Future Projects

I have a couple fun ideas in mind. Hopefully, they become part of the Alexa skill selection in the not-too-distant future.

One game is currently in scoping and design will be a best-on-screen experience. The challenge here is I’ll need ~1000 APL elements displayed simultaneously if I want to be able to animate and avoid the RenderDocument directive load disruption. I’ve developed a few POCs that make me believe this is possible on real devices despite overwhelming the emulator.

The other game is still in ideation but will be a scripted, themed exploration game. This will be turn-based, with an animated X/Y map where the player will be able to interact with on-screen content through either voice or touch. This will also have a rich voice experience, so those of you without screen devices will also be able to enjoy.


## Conclusion

Fellow tinkerers, I encourage you to go out and see how you can break things in new and exciting ways, beyond the intended use, to get the end experience you want. Let me know if you’ve been able to tame APL to create something new and different.

In my opinion, the end result created using these techniques is a more smooth and engaging experience for users. In a world where Alexa becomes available on more and more devices with screens, a visual interface needs to be as polished as the voice experience. Developers need to take risks and try something new.

Word of warning, this is all based on the public, beta version of APL as of writing, any hacks could stop working tomorrow. Also, remember to use adaptive or responsive design for all screen shapes and sizes.


## Disclaimer

This is me representing my personal night/weekend projects and has no association with my employer. All methods discussed here are based on publicly available features, just some used in unintended ways.