<?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: ​</title>
    <description>The latest articles on DEV Community by ​ (@donno2048).</description>
    <link>https://dev.to/donno2048</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%2F1177271%2F91c4d17d-0196-4e93-9fbe-f9e57671bf6e.gif</url>
      <title>DEV Community: ​</title>
      <link>https://dev.to/donno2048</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/donno2048"/>
    <language>en</language>
    <item>
      <title>🐍 The Journey of Creating the Smallest Snake Game in the World🐍</title>
      <dc:creator>​</dc:creator>
      <pubDate>Fri, 06 Oct 2023 08:01:34 +0000</pubDate>
      <link>https://dev.to/donno2048/the-journey-of-creating-the-smallest-snake-game-in-the-world-4ell</link>
      <guid>https://dev.to/donno2048/the-journey-of-creating-the-smallest-snake-game-in-the-world-4ell</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/donno2048/snake" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimg.shields.io%2Fgithub%2Fstars%2Fdonno2048%2Fsnake%3Fstyle%3Dsocial" alt="https://github.com/donno2048/snake"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;As I sit down to pen my journey of developing what may just be the world's smallest snake game, I'm filled with a sense of humility and amazement at the progress we've made. The journey was filled with challenges, optimizations, and a community of brilliant minds rallying together to push the boundaries of what's possible in x86 assembly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Inspiration
&lt;/h2&gt;

&lt;p&gt;It all started with a question: "&lt;a href="https://youtu.be/ExwqNreocpg" rel="noopener noreferrer"&gt;Can you fit a whole game into a QR code?&lt;/a&gt;". Inspired by &lt;a href="https://mattkc.com/etc/snakeqr/" rel="noopener noreferrer"&gt;MattKC's attempt&lt;/a&gt; to create an executable game within a QR code, I decided to embark on a similar journey. The challenge was clear - create an assembly-based snake game that would not only fit within a QR code but also push the boundaries of code optimization.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Early Days - 133 Bytes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwjghegy3jjyoacexd69.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnwjghegy3jjyoacexd69.jpg" alt="Obese cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my first Reddit post about the project, my game weighed in at 133 bytes. It was a small victory, but I knew there was room for improvement. Like many others, I faced the challenge of balancing code size and performance. I reached out to the community for help, as I had to use a loop instead of &lt;code&gt;movs&lt;/code&gt; to cut 7 bytes off, slowing the game down as you progressed.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Byte Less - 101 Bytes
&lt;/h2&gt;

&lt;p&gt;My determination led me to reduce the game's size to 101 bytes. The goal was now clear - reach that magical 100-byte mark. Each byte was precious, and every optimization counted. It was a thrilling challenge, and I was driven to make this game as compact as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 100-Byte Milestone
&lt;/h2&gt;

&lt;p&gt;After countless hours of coding and optimizing, I achieved what I thought was impossible - the game fitting into 100 bytes! It was a moment of triumph, but it came at a cost. I had to re-write the screen buffer handling code, which meant giving up string manipulation instructions. Sacrifices were made for the sake of size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chasing 78 Bytes
&lt;/h2&gt;

&lt;p&gt;As my game reached 89 bytes, I set my sights on an even more ambitious goal – to fit it within a version 4 QR code, which is miniature compared to the version 5 it currently occupied. But making it smaller without compromising gameplay proved to be a challenge. It became clear that I needed innovative solutions to reach my target.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 81-Byte Mark and Beyond
&lt;/h2&gt;

&lt;p&gt;I made incremental progress, achieving 81 bytes. Suggestions from the community, like using wasd and a lookup table instead of arrows and arithmetic, were considered but not implemented. Maintaining the integrity of the game experience was paramount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Victory at 76 Bytes
&lt;/h2&gt;

&lt;p&gt;Finally, with the invaluable assistance of contributors like @peterferrie and @hen-ter, I reached my goal. My snake game was now a mere 76 bytes, fitting perfectly into a version 4 QR code. Changes included utilizing the &lt;code&gt;cx&lt;/code&gt; register to save more data, replacing the random number generator (RNG), and adopting a new input handling mechanism. It was a remarkable achievement.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Final Stretch - 69 Bytes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupt6ga6198x4wsmu5bws.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupt6ga6198x4wsmu5bws.jpg" alt="Slim cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The community rallied around me, offering invaluable insights and suggestions. The achievement was the result of meticulous optimization efforts, such as replacing &lt;code&gt;di&lt;/code&gt; register usages with &lt;code&gt;bx&lt;/code&gt;, reordering instructions, and making clever use of known values in registers. Much credit goes to @hen-ter for contributing to this final triumph.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Size Comparison
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb7a74gc93mniuznelie.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb7a74gc93mniuznelie.jpg" alt="Cats on a scale"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this section, we'll take a closer look at the size of my snake game code and compare it to other versions created by fellow developers. This comparison will showcase the remarkable optimization achieved in my project.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;My version&lt;/th&gt;
&lt;th&gt;MattKC's version&lt;/th&gt;
&lt;th&gt;ibara's version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bytes&lt;/td&gt;
&lt;td&gt;69&lt;/td&gt;
&lt;td&gt;~1400&lt;/td&gt;
&lt;td&gt;2024&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;QR&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fdonno2048%2Fsnake%2Fmaster%2Fdocs%2Fsnake.png" alt="My QR Code"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmattkc.com%2Fetc%2Fsnakeqr%2Fcode.png" alt="MattKC's QR Code"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fibara%2Fsnakeqr%2Fmaster%2Fsnakeqr.png" alt="Ibara's QR Code"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The final machine code of the snake game looks like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fdb99c0fc55407a02000b80
300cd108b3f89e6e54021c8
93383775f7882fe4606bc00
ad414d5449801c79701c739
cf77d9f6f238cc77d330157
9cf577ad526ad938837ebd9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Acknowledgments
&lt;/h2&gt;

&lt;p&gt;As I reflect on this incredible journey, I'm truly humbled by the support and contributions of the community. This project was a collective effort, and I couldn't have done it alone. My deepest gratitude goes out to those who believed in this endeavor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqifzzmfc1ekruckvbn2a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqifzzmfc1ekruckvbn2a.jpg" alt="Cat collaboration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I want to take a moment to acknowledge and appreciate the community that made this journey possible. To all the brilliant minds who joined in, your input was invaluable. I would also like to extend my heartfelt thanks to @hen-ter for his significant contributions.&lt;/p&gt;

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

&lt;p&gt;From 133 bytes to a mere 69 bytes, this has been an incredible adventure. My snake game is not just a code, but a testament to the spirit of collaboration and innovation within the tech community. As we move forward, let's keep pushing the limits, creating, and optimizing - because in the world of coding, the possibilities are endless.&lt;/p&gt;

&lt;p&gt;Feel free to explore the project on &lt;a href="https://github.com/donno2048/snake" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; and try out the online &lt;a href="https://donno2048.github.io/snake/" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;. Thank you all for being part of this remarkable journey.&lt;/p&gt;

</description>
      <category>assembly</category>
      <category>opensource</category>
      <category>programming</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
