Adafruit NeoPixel Snake Game

Snake, a fun and addictive game that has been around since the late 70′s. Its a game with few and simple rules and I think its the perfect thing to play on a NeoPixel Display.

Background:

I was asked to speak at my old grammar school by my science teacher. He asked if I could come and talk to his classes about all the things I have made in hopes to inspire them to get creative and to inspire them to want to know how things work. I was also asked if I could bring some different projects to show off.

The problem I faced is one I face often, I’m surrounded by cool projects in my room, but I never know what to show when asked what I do. I also wanted whatever I was going to show, to be something that would entertain and captivate my young audience. To do this, I created something new and something I had wanted to try ever since I first got my NeoPixel shield. I recreated the classic snake game.

Project:

My goal was simple, use my PS/2 keyboard to control the game and have everything displayed using the shield. From my past project of using my Trinket as a PS/2 to USB converter, I was already versed in the PS/2 library. The same is true with the NeoPixel Shield after my Virtual Robot. The only thing I didn’t know how to do was create the game.

Snake is a simple game when you break it down. It has only three rules (the version I wanted to make).

  1. If the Snake goes off the screen, the game will end
  2. If the Snake goes back over itself, the game will end
  3. After eating food, the Snake will grow by one space

These are very simple rules to follow when creating the code for the game, but there was just one thing I wasn’t sure of, and that was how to move the Snake.

This is really a two part problem, because what I mean by, “How to move the Snake,” I mean, how do I get the game to move, and how to I get the body to move.

For a game that has any kind of movement, you need some kind of clock. This way you can update the game at a set time. When you create a game on a computer, you can use a clock event to trigger an update. The Arduino, while having a clock, is a little different then using a computers timer. The way I saw it I had two options, the first being I could set a timer interrupt that would update the game at a set time interval. This would work very much like the clock in a computer game, but the downside is I would need to setup the registers on the Arduino manually as there is no built in way to use the timer interrupt (that I know of). The other downside to this method is that if somebody (or even myself), wanted to use my code on a different Arduino board, there is a chance that the timer register setup might be different. This means that a novice programer might not be able to run the game.

There is a second method that I thought of, and the one I ended up using, which is an if statement. By working off the Blink without delay example Sketch, I was able to create an event that will trigger after a set amount of time. This method also works (should work) on any Arduino board. Still, there is a downside which is, if the whole program takes a long time to run then, no matter how short you set the delay time for, it will only run as fast as the rest of your code. This is unlike the interrupt where it will keep updating at a set interval no matter what the rest of the program is doing.

The second part of my problem was how to program the snake to move. There are different ways this could be done and I was having a hard time coming up with a way. What I ended up doing was looking for a simple snake tutorial I might be able to follow for the concepts, and I found one. Thought, what I found was written for Python, all I needed was the basic concepts. After a few short hours of coding I got this…

This was a very fun project that did exactly what I wanted it to do. The kids I showed this to loved it and kept wanting to play. I hope it inspired them to want to make cool things of there own.

Code:

You can find the code over at my GitHub: https://github.com/M-tech-Creations/NeoPixel_Snake_GAME

Arduino, Arduino C, Hardware, Programing , , , , , , ,