Snake 1D

[ pt_BR ]

snake 1D

One of my oldest repositories on Github is an implementation of the snake game in python. I had some addressable RGB LED strips (with WS2812s, also known as NeoPixels) and a fadecandy laying around. I remembered about line wobbler, an amazing game I saw in an event many years ago. They use LED strips and a custom controler as the user interface for their game. Then I realised: if we take one dimension away from the classic snake game it fits in an LED strip. So all I needed to do was put it all together.

Fadecandy has a server to which we can communicate using Open Pixel Control, so we can write python code to send data to the server and, thus, control the LEDs. As the code I wrote 9 years ago was also in python, I could copy and paste most of it and make some small changes: the LED strip instead of the screen and make it fit into one dimension.

The game is as simple as possible: the snake (in yellow) moves in one direction and some food appears (in green). Pressing the button changes the snake's direction. Each food eaten moves us to the next level, but hitting a wall (in white) is game over.

Snake game running on an LED strip, controlled by python using fadecandy

This worked quite well and brings all the advantages Fadecandy has, mainly the beautiful colors and transitions (which unfortunately the video doesn't show very well). The main problem is that we still need a USB connection to a computer (for the Fadecandy) and an entire keyboard to press one key.

The next step is then, of course, port the code to a microcontroller. This way we can make it reasonably portable (you'll still need an LED strip connected to a power outlet). I ended up using an ATTiny 85 because it's cheap, small and easy to program with the arduino IDE through ATTinyCore.

Snake game running directly from the microcontroller

As it's clear on the second video, the transitions are much chunkier than using Fadecandy. Obviously the code can be improved to bridge that gap a little bit, but the ATTiny85 is quite limited in comparison, so the results will never be the same.

The hardware is not much more than a bare bones ATTiny85 and Cherry MX button that nudges our good friend in a different direction. Everything else is just some reverse polarity protection and a debouncing circuit for the switch, so one press is not registered as two (which would make the snake continue in the same direction).

Esquema elétrico do jogo com o ATTiny 85
Schematics for the game with an ATTiny 85

With the schematics and layout, all I had to do was order the PCBs, gather the components, solder it all together and program the microcontroller. I then modeled a small enclosure in Freecad and 3D printed it, so everything was easier to hold while playing.

All the code and hardware description is free, and available in my github repository.