DEV Community

Cover image for 🐍 The Journey of Creating the Smallest Snake Game in the World🐍
​
​

Posted on • Edited on

🐍 The Journey of Creating the Smallest Snake Game in the World🐍

https://github.com/donno2048/snake

Introduction

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.

The Inspiration

It all started with a question: "Can you fit a whole game into a QR code?". Inspired by MattKC's attempt 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.

The Early Days - 133 Bytes

Obese cat

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 movs to cut 7 bytes off, slowing the game down as you progressed.

A Byte Less - 101 Bytes

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.

The 100-Byte Milestone

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.

Chasing 78 Bytes

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.

The 81-Byte Mark and Beyond

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.

Victory at 76 Bytes

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 cx register to save more data, replacing the random number generator (RNG), and adopting a new input handling mechanism. It was a remarkable achievement.

The Final Stretch - 69 Bytes

Slim cat

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

Code Size Comparison

Cats on a scale

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.

My version MattKC's version ibara's version
Bytes 69 ~1400 2024
QR My QR Code MattKC's QR Code Ibara's QR Code

The final machine code of the snake game looks like that:

fdb99c0fc55407a02000b80
300cd108b3f89e6e54021c8
93383775f7882fe4606bc00
ad414d5449801c79701c739
cf77d9f6f238cc77d330157
9cf577ad526ad938837ebd9
Enter fullscreen mode Exit fullscreen mode

Acknowledgments

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.

Cat collaboration

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.

Conclusion

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.

Feel free to explore the project on GitHub and try out the online Demo. Thank you all for being part of this remarkable journey.

Top comments (1)

Collapse
 
icy-world profile image
Albert Ward

I've followed this journey on Reddit awesome work!