<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Will Blaschko</title>
    <description>The latest articles on DEV Community by Will Blaschko (@willblaschko).</description>
    <link>https://dev.to/willblaschko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F168774%2F353e0517-4c9c-4ac4-846f-11e064b819b9.png</url>
      <title>DEV Community: Will Blaschko</title>
      <link>https://dev.to/willblaschko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/willblaschko"/>
    <language>en</language>
    <item>
      <title>Falling Blocks - "Tetris"​ On Alexa (APL)</title>
      <dc:creator>Will Blaschko</dc:creator>
      <pubDate>Fri, 17 May 2019 22:10:16 +0000</pubDate>
      <link>https://dev.to/willblaschko/falling-blocks-tetris-on-alexa-apl-1gc1</link>
      <guid>https://dev.to/willblaschko/falling-blocks-tetris-on-alexa-apl-1gc1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jlbjp2u6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_tetris.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jlbjp2u6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_tetris.png" alt="Falling Blocks Game"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;I created a fully-working &lt;a href="https://en.wikipedia.org/wiki/Tetris"&gt;Tetris&lt;/a&gt; clone (named Falling Blocks) for Alexa using &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-overview.html"&gt;Alexa Presentation Language&lt;/a&gt; (APL) that allows you to play in real-time, controlled entirely by voice. Building on the techniques in my &lt;a href="https://www.linkedin.com/pulse/alexa-game-animation-experiments-apl-will-blaschko/"&gt;last post&lt;/a&gt;, the gameplay area is comprised of a 8x12 grid of &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-pager.html"&gt;Pagers&lt;/a&gt; controlled by &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#setpage-command"&gt;SetPage&lt;/a&gt; commands for forced animation. The biggest challenge was syncing the frontend with the backend, which was solved by abusing &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-commands-media.html#controlmedia"&gt;ControlMedia&lt;/a&gt; (Play/Rewind) commands (in&lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#parallel-command"&gt; Parallel&lt;/a&gt;) and listening for &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-video.html#onplay"&gt;onPlay&lt;/a&gt; events for each tick of gameplay. Big thanks to &lt;a href="https://www.linkedin.com/in/mark-spitzer-299269100/"&gt;Mark S.&lt;/a&gt; (an APL genius) for suggesting this solution!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.co.uk/dp/B07QNPMCM2"&gt;UK Skill&lt;/a&gt; - &lt;a href="https://www.amazon.com/dp/B07QNPMCM2"&gt;US Skill&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/XdgwUCVAOV4"&gt;Watch the gameplay video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042"&gt;See the Gists&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, I will talk about the Alexa (response size) and device (performance) constraints for the Falling Blocks project, share the math I used to calculate viability of the project, and cover the key elements of the solution (including gists).&lt;/p&gt;

&lt;p&gt;During my most recent trip to Italy, I had an informal brainstorming session over dinner with my &lt;a href="https://developer.amazon.com/blogs/alexa/tag/Spain"&gt;Spanish&lt;/a&gt; and &lt;a href="https://developer.amazon.com/blogs/alexa/tag/Italy"&gt;Italian&lt;/a&gt; colleagues who were amused by my &lt;a href="https://www.linkedin.com/pulse/alexa-game-animation-experiments-apl-will-blaschko/"&gt;last post&lt;/a&gt;. With a mutual interest in games (classic and modern) we started discussing what other games could be APL-ified. It is with only a little shame that I admit that I literally jumped up and down with excitement (multiple times) when we stumbled upon Tetris.&lt;/p&gt;

&lt;p&gt;I have a couple other projects in the queue, but the ability to execute on real-time voice gameplay was too tempting to pass up, so I bumped Tetris to top priority. Fast forward two weeks and my finger is hovering over the button to submit for certification.&lt;/p&gt;

&lt;p&gt;Of the ~20 hours I spent on this project, about 8 were fighting with the frontend/backend sync challenge. After a very quick chat with Mark, I was able to completely resolve the issue in about an hour, reusing most of the code I had already written (the advantages of working with talented people).&lt;/p&gt;

&lt;p&gt;As always, everything here could break tomorrow. Caveat emptor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constraints
&lt;/h2&gt;

&lt;p&gt;There are two primary constraints that I had to keep in mind for this project: 1) the response size to Alexa needs to be &lt;a href="https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#response-format"&gt;under 24kb&lt;/a&gt; (~24,000 characters) and 2) the more &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-component.html"&gt;components&lt;/a&gt;, the longer an Echo device takes to render and then execute &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-commands.html"&gt;commands&lt;/a&gt;. I found that around 300 components, my &lt;a href="https://smile.amazon.co.uk/Amazon-Echo-Show-With-Screen-2nd-Generation-Alexa/dp/B0793G9T6T"&gt;Echo Show&lt;/a&gt; starts to noticeably slow. Originally, my plan was to have a 12x20 grid, but I had to scale this back due to 10+ second render times (on device, more in the emulator).&lt;/p&gt;

&lt;p&gt;For this project, I was able to trade one constraint for the other and I experimented in a few different ways before returning to my initial design:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define grid groupings, so that one &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-pager.html"&gt;Pager&lt;/a&gt; controls 2+ tiles (for 3 * 3 combinations) to halve the number of &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#setpage-command"&gt;SetPage&lt;/a&gt; commands required. This would have allowed for twice as many gameplay commands but 864 (9 * 8 * 12) components was too many for the device (and completely froze &lt;a href="https://developer.amazon.com/alexa/console/ask/displays?"&gt;the emulator&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Pre-calculate coordinates and sizes and render the tiles in absolute positions resulting in a much larger &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-document.html"&gt;APL Document&lt;/a&gt;. Despite no calculations for &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-data-types.html#dimension"&gt;dimensions&lt;/a&gt; being required at runtime (relative/absolute percentages), I saw a huge performance hit. In the future, I may try to create more simple benchmarks to figure out the root cause because this seems backwards to me.&lt;/li&gt;
&lt;li&gt;Reload the document. Even with the goal of a zero-load gameplay, this crossed my mind as I was trying to figure out how to reset the board quickly. The updated &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-document.html#document-properties"&gt;document&lt;/a&gt; and data source (without optimization) were about the same total size as the commands required to reset all the Pagers. This was not an option: load time killed this.&lt;/li&gt;
&lt;li&gt;Calculate the delta after a voice commands and change only those tiles. With a limited response size, I wanted to minimize the number of Pagers I sent SetPage commands. There were some syncing issues (even after delta calculation) leading to ghost tiles. It was more prudent to completely reset the board once after a voice command and then do delta calculations for each tick.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Quick Math
&lt;/h2&gt;

&lt;p&gt;(Please check my math!)&lt;/p&gt;

&lt;p&gt;Assuming we follow the original design (which I did), one tile equals one Pager. For the below, active is defined as: the tiles that comprise the falling shape; inactive: the settled structure tiles (former active), and empty: unused grid tiles. &lt;/p&gt;

&lt;p&gt;Our grid is going to be of the dimensions X by Y. Each grid tile has 3 states (active, inactive, empty) displayed as &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-frame.html"&gt;Frames&lt;/a&gt; within a parent Pager component. Our number of grid tile components is: 4XY (Pager plus Frames). With our &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-container.html"&gt;Container&lt;/a&gt; components used to wrap them, we have an additional Y + 1 (one Container to hold everything and one Container per grid row).&lt;/p&gt;

&lt;p&gt;With this design, our minimum number of components is therefore: 4XY + Y + 1.&lt;/p&gt;

&lt;p&gt;Our number of SetPage commands will be broken down into: 1 full reset (XY) to all Pagers and one delta calculation per tick. The delta is a minimum of 4 SetPage commands (the O-Shape moving) and a maximum of 4X (clearing 4 rows with the I-Shape). More about &lt;a href="https://tetris.fandom.com/wiki/Tetromino"&gt;shapes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The minimum number of characters I found per SetPage command is 47 (where the componentId is a two-character grid location):&lt;/p&gt;

&lt;p&gt;&lt;em&gt;{"type":"SetPage","componentId":"aa","value":0}&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ignoring all the other response data required (&lt;a href="https://developer.amazon.com/blogs/alexa/post/08edaa00-59e2-46b7-aace-4080f2a87450/using-session-attributes-in-your-alexa-skill-to-enhance-the-voice-experience"&gt;session attributes&lt;/a&gt;, &lt;a href="https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#response-object"&gt;speak/reprompt&lt;/a&gt;, other &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-commands.html"&gt;commands&lt;/a&gt;), that leaves us with the following equation:&lt;/p&gt;

&lt;p&gt;Minimum Number of Commands (I-Shape) = (24000/47 - XY)/4X&lt;/p&gt;

&lt;p&gt;Maximum Number of Commands (O-Shape) = (24000/47 - XY)/4&lt;/p&gt;

&lt;p&gt;As stated above in Constraints, if you can decrease X or Y (by either reducing grid size or by spanning multiple tiles with a single Pager), you can greatly increase the number of SetPage commands you can send. Therefore, lengthier autonomous gameplay is inversely related to higher device performance requirements.&lt;/p&gt;

&lt;p&gt;In the case of my 8x12 grid, we have:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Min Components: 397&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Min Commands: 12&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Max Commands: 103&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An intelligent solution could calculate available space left after each tick to see if there’s room for another but this calculation would need to be done as the last step. For this project, I picked a fixed number with enough headroom (27 minus user commands, so about two full shape falls), since I have other content in the response payload.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Since I already covered the basics in my &lt;a href="https://www.linkedin.com/pulse/alexa-game-animation-experiments-apl-will-blaschko/"&gt;last post&lt;/a&gt;, I will only cover here what is unique to this project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042"&gt;Gists here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Audio Marker (Hack)
&lt;/h3&gt;

&lt;p&gt;Despite seeming like there should be, there is almost no way to track what has already happened to your components on the device when you send multiple commands with staggered delays. The request payload appears to have status information on component children, visibility, and state for each component but I did not see these being updated reliably. Pager and Sequence do not have an onChange event. I originally tried to do the math to estimate latency and calculate the current state, but the game was not playable (or at least not enjoyable).&lt;/p&gt;

&lt;p&gt;In my conversation with Mark, he suggested I find a way to mark each batched set of commands (tick using &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#parallel-command"&gt;Parallel&lt;/a&gt; commands). The only commands he knew of that sent a callback to the Lambda were the &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-commands-media.html"&gt;Media Commands&lt;/a&gt;. Long story short, &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-video.html#on-end"&gt;onEnd&lt;/a&gt; wasn’t reliable enough (due to timing, it didn’t always fire), but &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-video.html#onplay"&gt;onPlay&lt;/a&gt; fired every time.&lt;/p&gt;

&lt;p&gt;Listening for the onPlay UserEvent on the Lambda, I was able to shift my server-side state queue and keep track of the current gameplay position.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042#file-event_handler-js"&gt;Event Handler Gist&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042#file-audio_counter-json"&gt;Layout Gist&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  InitialPage Attribute of Pager
&lt;/h3&gt;

&lt;p&gt;This is one of the “read the documentation” moments for me. I was trying to figure out how to display the current game state in the best way on initial load with minimal performance overhead (add a &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-pager.html#firstitem"&gt;firstItem&lt;/a&gt; attribute, send a set of reset SetPage commands, etc.). Nope, what I wanted was &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-pager.html#initialpage"&gt;initialPage&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By passing in a 2D array which conveyed both X by Y dimensions and the starting values, I was able to calculate/display the initialPage (starting tile state) with no additional overhead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042#file-data_values-json"&gt;Values Gist&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042#file-layout_game-json"&gt;Layout Gist&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple Moves Per Interaction
&lt;/h3&gt;

&lt;p&gt;More as a quality of life implementation, I allow my users to send multiple commands at a time. With the most permutations, I get utterances that look like this (see gist):&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"{DIRECTION_A} {DIRECTION_B} {DIRECTION_C} {DIRECTION_D} {DIRECTION_E} {DIRECTION_F} {DIRECTION_G} {DIRECTION_H} {DIRECTION_I} {DIRECTION_J}"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Instead of needing to say each “Right” individually (“Alexa, right. Alexa, right, Alexa, right”), the user can say all three (“Alexa, right, right, right.”). In testing, this greatly reduced friction. If I wanted to make the game more difficult, I would figure out the right timing to interleave these between the normal fall speed movements. I think the game is difficult enough (and fun enough) as it is for a proof of concept.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/44b0a399084367271af1b2ab55704042#file-en-gb-json"&gt;Intent Gist&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Saving to Persistent vs Session Attributes
&lt;/h3&gt;

&lt;p&gt;Most of the time it can make sense to save everything in your Session attributes to your &lt;a href="https://www.npmjs.com/package/ask-sdk-dynamodb-persistence-adapter"&gt;Persistent storage&lt;/a&gt; and vice versa. If you do so at the start/finish of a session instead of each turn, you save yourself IO latency and cost.&lt;/p&gt;

&lt;p&gt;Because I had to pre-calculate the future possible states, and because I was already constrained by my response size, I decided to split my storage logic. All game state queue information is in Persistent storage 100% of the time, which means I hit my DynamoDB for every conversation turn (and every gameplay tick). This is stored as a queue of states, one per gameplay tick. I then use variables in the Session attributes to extract what I need from the persisted information. This is the first legitimate need I’ve had in my own projects to split my user data in this way.&lt;/p&gt;

&lt;p&gt;They latency cost of reading from and saving to DynamoDB isn’t high in the grand scheme of things (generally under 150ms). In gameplay, it’s not explicitly noticeable, but everything adds up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;One of my colleagues asked me how I have time and energy for projects like this. It’s a great question. Pushing the boundaries of what technology can do excites me, the creation process relaxes me, and completing projects/milestones is satisfying. There were parts of this project that were painful and frustrating, but that’s part of learning and experimentation (worth it in the end).&lt;/p&gt;

&lt;p&gt;Like &lt;a href="https://www.amazon.co.uk/Team-Quasko-2048/dp/B07PTYCXFL/"&gt;2048&lt;/a&gt;, I see this game as another compelling reason for voice-controlled devices with screens. The possibilities and experience will only continue to get better as technology improves and developers become more creative.&lt;/p&gt;

&lt;p&gt;If you’re new to Alexa or APL, I would not suggest starting with a project like this. In addition to the complexities, APL is still limited in functionality; Pager/Sequence animation is a hack, UI audio markers are a hack. I did a lot of math and design before I wrote my first line of code, even then I wasn’t sure the project was even practical.&lt;/p&gt;

&lt;p&gt;That said, if you’re like me, go out there and see what you can break and then what you can create out of those broken pieces. Enjoy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Everything here could break tomorrow. Caveat emptor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/XdgwUCVAOV4"&gt;https://youtu.be/XdgwUCVAOV4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>apl</category>
      <category>alexa</category>
      <category>amazon</category>
      <category>skill</category>
    </item>
    <item>
      <title>Alexa Game and Animation Experiments (APL)</title>
      <dc:creator>Will Blaschko</dc:creator>
      <pubDate>Fri, 17 May 2019 22:03:49 +0000</pubDate>
      <link>https://dev.to/willblaschko/alexa-game-and-animation-experiments-apl-546</link>
      <guid>https://dev.to/willblaschko/alexa-game-and-animation-experiments-apl-546</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;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 (&lt;a href="https://www.amazon.co.uk/Team-Quasko-2048/dp/B07PTYCXFL"&gt;UK version&lt;/a&gt;, &lt;a href="https://www.amazon.com/Team-Quasko-2048/dp/B07PTYCXFL"&gt;US version&lt;/a&gt;, 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. &lt;a href="https://youtu.be/RMc_pFn1MbM"&gt;Demo video&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Although I have used &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-overview.html"&gt;APL&lt;/a&gt; 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 &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-component.html"&gt;components&lt;/a&gt; and &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-commands.html"&gt;commands&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://www.linkedin.com/in/willblaschko/detail/recent-activity/posts/"&gt;other posts&lt;/a&gt; or followed some of my &lt;a href="https://forum.xda-developers.com/shield-tv/general/alexa-media-voice-control-android-t3784032"&gt;projects&lt;/a&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR; What’s Possible?
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/579998deb99366be7c7876b80ecac7e2"&gt;Gist here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/579998deb99366be7c7876b80ecac7e2"&gt;Gist here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/f8f86ccb4598c6bf4125f4ebde3a7b81"&gt;Gist here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;200+ element APL documents&lt;/strong&gt;: By being smart about reuse of &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-layout.html"&gt;Layouts&lt;/a&gt;, you can fit hundreds of APL components into the response sent to Alexa. This allows for complex scenes to be built and minimizes &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-render-document-skill-directive.html"&gt;RenderDocument&lt;/a&gt; directives.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/willblaschko/e72fe9359fb8cb605484819eec3583ab"&gt;Gist here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Combine these together and you could get a pretty decent top-down 16-bit style game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Projects
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-render-document-skill-directive.html"&gt;RenderDocument&lt;/a&gt; directives only when absolutely necessary (a scene change) 2) animate everything possible and 3) create games that were enjoyable and sticky.&lt;/p&gt;

&lt;p&gt;I will keep the below relatively code-light, see the gists above or the related videos for more details.&lt;/p&gt;

&lt;h3&gt;
  
  
  2048 (Board Game)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NrpGnFMW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_2048.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NrpGnFMW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_2048.jpg" alt="2048 Game"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Challenges
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;${@viewportShape == @viewportShapeRound ? viewport.width / 6 : (viewport.width &amp;gt; viewport.height ? viewport.height / 4 : viewport.width / 4)}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...{&lt;br&gt;
    "type": "Tile",&lt;br&gt;
    "name": "pos1"&lt;br&gt;
}...&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Goals&lt;br&gt;
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  RPG Name TBD (First Person RPG)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tNGDV7Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_rpg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tNGDV7Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_rpg.jpg" alt="RPG Game"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://www.amazon.co.uk/Bethesda-Game-Studios-Special-Edition/dp/B07D6STSX8"&gt;Skyrim&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://youtu.be/MKBc_0Dpr5g"&gt;this video&lt;/a&gt; for an early POC.&lt;/p&gt;

&lt;p&gt;Unrelated, for this project, I needed to be able to return a sequence of Speak responses to Alexa, so I created a modified &lt;a href="https://gist.github.com/willblaschko/cc4f0019da90e8c0b388bf1ae42bca20"&gt;RequestHandler&lt;/a&gt; (gist) to be used with a &lt;a href="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"&gt;RequestInterceptor&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Challenges
&lt;/h4&gt;

&lt;p&gt;1) How can I animate the weapon and the monster and 2) create a layered environment that gives a sense of depth?&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#scroll-command"&gt;Scroll&lt;/a&gt; to using a timed &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-standard-commands.html#sequential-command"&gt;Sequential&lt;/a&gt; command. With this, I have dancing creatures, attacking weapons, and about any other animation I could want on the X or Y axis.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Goals
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flappy Alexa (2D Side Scroller/Platformer)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zMUgJuk2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_flap.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zMUgJuk2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_flap.jpg" alt="Flappy Bird Game"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Flappy_Bird"&gt;Flappy Bird&lt;/a&gt; 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. &lt;a href="https://youtu.be/OAHiOijGRIw"&gt;The video&lt;/a&gt; shows blocks (use your imagination) simulating the basic gameplay, all rendered and, theoretically, playable on a real device.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Challenges
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Goals
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Projects
&lt;/h2&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>apl</category>
      <category>alexa</category>
      <category>amazon</category>
      <category>skill</category>
    </item>
    <item>
      <title>Alexa APL Performance Benchmark</title>
      <dc:creator>Will Blaschko</dc:creator>
      <pubDate>Fri, 17 May 2019 21:43:48 +0000</pubDate>
      <link>https://dev.to/willblaschko/alexa-apl-performance-benchmark-9eb</link>
      <guid>https://dev.to/willblaschko/alexa-apl-performance-benchmark-9eb</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9PW3OzZw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_performance.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9PW3OzZw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s3.amazonaws.com/alexa-posts/ss_performance.png" alt="Falling Blocks Game"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(The above is a layout used to stress-test APL)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Rhetorical question: have you ever wanted to benchmark the load times for different Alexa &lt;a href="https://developer.amazon.com/docs/alexa-presentation-language/apl-document.html"&gt;APL documents&lt;/a&gt;? To be able to A/B/C/… test them to understand which implementation would give the quickest render time for your customers?&lt;/p&gt;

&lt;p&gt;Hopefully the answer is: “Absolutely!”&lt;/p&gt;

&lt;p&gt;Here’s &lt;strong&gt;&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729"&gt;some code&lt;/a&gt;&lt;/strong&gt; you can use to benchmark both in the browser and on a physical device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  NodeJS script configured (&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729"&gt;GIST&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;  Local folder where you can persist &lt;a href="https://github.com/GoogleChrome/puppeteer"&gt;Puppeteer&lt;/a&gt; user data (login cookies)&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.amazon.com/Fire-TV-Stick-with-Alexa-Voice-Remote/dp/B0791TX5P5"&gt;Fire TV Stick&lt;/a&gt; with Alexa (optional)&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://developer.android.com/studio/command-line/adb"&gt;ADB installed&lt;/a&gt; (if benchmarking on Fire TV)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
Puppeteer opens the Displays &lt;a href="https://developer.amazon.com/alexa/console/ask/displays"&gt;Simulator&lt;/a&gt; and renders the layouts/data repeatedly. Each time the browser renders the layout, a command is also (optionally) sent to the Fire TV through ADB. Listening to web console logs (simulator) and ADB logs (Fire TV), the script calculates the time of each render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure index.js to meet your needs, currently everything is defined in code, but it could be modified to be command line arguments.&lt;/li&gt;
&lt;li&gt;If using Fire TV, &lt;em&gt;adb connect …&lt;/em&gt; to the target device.&lt;/li&gt;
&lt;li&gt;Copy a valid Chromium user directory to an accessible folder and use as a parameter (or change benchmark.js to log in automatically).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What This Is:&lt;/strong&gt;&lt;br&gt;
This is meant to be a proof-of-concept experiment as I optimize my own documents. I hope someone can use this as a starting point for a more comprehensive tool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Files:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729#file-index-js"&gt;index.js&lt;/a&gt; - Launch benchmark and output performance&lt;br&gt;
&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729#file-benchmark-js"&gt;benchmark.js&lt;/a&gt; - Benchmark library&lt;br&gt;
&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729#file-package-json"&gt;package.json&lt;/a&gt; - Node package&lt;br&gt;
&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729#file-layout_base-json"&gt;layout_base.json&lt;/a&gt; - Default layout used to reset the simulator&lt;br&gt;
layout_example_*.json - Different layouts to benchmark&lt;br&gt;
&lt;a href="https://gist.github.com/willblaschko/a0e361e8e5e3afb3799a31165c362729#file-data_example-json"&gt;data_example.json&lt;/a&gt; - Data used for all layouts&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Output:&lt;/strong&gt;&lt;br&gt;
Running benchmark for Layout 0 (Browser)&lt;br&gt;
Benchmark ran 100 times.&lt;br&gt;
Average render: 1723ms&lt;br&gt;
Max render: 3989ms&lt;br&gt;
Min render: 236ms&lt;/p&gt;

&lt;p&gt;Running benchmark for Layout 1 (Browser)&lt;br&gt;
Benchmark ran 100 times.&lt;br&gt;
Average render: 1786ms&lt;br&gt;
Max render: 3326ms&lt;br&gt;
Min render: 224ms&lt;/p&gt;

</description>
      <category>apl</category>
      <category>alexa</category>
      <category>amazon</category>
      <category>skill</category>
    </item>
  </channel>
</rss>
