TrojanC

Development and Guides

Arduinolearn-arduinoplatformio

Arduino breadboard snake game

Overview of the Arduino breadboard snake game

A while back I saw simple snake game being made with JavaScript and it seems very simple how the game was put together, so I thought let’s try and do this on Arduino. I had an Arduino Nano lying around as well as a small colour LCD display. Taking the JavaScript example was easy enough to convert to Arduino compatible code. This post shows what I did to build a simple arduino breadboard snake game.

Prerequisites

To use this tutorials please make sure you already have platformio setup on your computer. See Learning Arduino GitHub Repository for more details.

Source Code

The source code for this project is available on my GitHub Repository.

Required Components

  • Arduino Nano (adapt if using a different board)
  • Fullsize breadboard
  • 240×320 2.2″ TFT SPI display
  • 7 x 1k Ohm resistor
  • 6 x 2k Ohm resitor
  • 30 breadboard jumper wires
  • breadboard push-to-make button

Arduino breadboard snake game layout

Arduino breadboard snake game circuit

The circuit basically consists of a Arduino Nano as the main controller. A 5v/3.3V level shifter for the SPI lines was made using a  resistor voltage divider (I tried using a IC to do the level shifting but it aparently couldn’t keep up with the high SPI frequency). I only had one push button, but ideally you need 2 buttons to be able to turn left or right, instead of just circling in one direction.

Make sure when connecting your LCD what voltages it needs, and check the pin layout – it might not match exactly like my diagram.

Running the code

git pull https://github.com/trojanc/learn-arduino
cd learn-arduino/snake
platformio init
platformio run

Source code notes

You might noticed that I did not use the Adafruit graphics library. I initially did use it, but it performed too slow for even a simple game like this. I then found the library PDQ_GFX_Libs which is forked from the original Adafruit libraries with a lot of performance improvements – they claim somewhere between 2 to 12 times faster. This library help take away the “choppyness” of screen refreshes.

I also took a few shortcuts to prevent whole screen updates. It only re-draws where something has changed. For example when the snake moves, we only have to draw over the background (or apple) where the head is at, and put back the background where the tail used to be. Doing this we only have to redraw two 10×10 pixel blocks for every movement.

Further improvements

  • ADD ANOTHER BUTTON! You need at least 2 buttons to properly play
  • Use hardware debouncing for button presses
  • Find a IC for level shifting that works
  • Maybe start putting together a reusable circuit in a nicer packaging so that someone can play this without the risk of letting a jumper wire pop out…

Arduino snake game breadboard

Arduino breadboard snake game screen

References

Leave a Reply

Your email address will not be published. Required fields are marked *