Snake Game in Arduino

The Snake Game is one of the classic games I remember playing on my first "Brick Game" when I was young. A fun, simple yet exciting game that I have decided to recreate in Arduino! Let's get started.

Things Needed:

Arduino Uno
8x8 LED Matrix
Arduino Joystick

Breadboard Jumper Wire Pack(200mm&100mm) (Seeed Studio) | SS110990049 |  Core Electronics Australia

Jumper Wires

Breadboard — Raspberry Pi Australia


Ide logo png » PNG Image

Arduino IDE

8x8 LED Matrix and Joystick Schematic:

Matrix connections s9oj43nrtc

Credits to: Sanwandter1

For the Joystick, I used four (4) out of the five (5) pins which is responsible for the Ground, 5V Power and X and Y movements. The 5th pin is for the Switch (when you push down the joystick it makes a click sound) but for this instance, we will not be using it.

For the joystick's four (4) pins, I have connected them to the ff: in the Arduino Uno Board:
- PIN 1: GND
- PIN 2: 5V
- PIN 3: A4
- PIN 4: A5

The Code:

This portion is to initialize our variables. This is where we define what are our rows and cols from the LED Matrix. We also define which pin the X and Y are plugged into the Arduino Uno. We also set the default screen (blank) and what we can see in the Game Over screen (sad face). Other important things are declared here such as the current rows and columns where the snake's body is, variable to register which direction the snake is going, default snake direction, max length of the snake, a variable to store the snake's current length, which row and column the snake's food is and so much more.
You can draw anything you want on the LED Matrix! The key is to control the on / off state of the lights. To explain this further, imagine you have a 8x2 LED Matrix:
The zeroes and ones represent a dot on the LED Matrix. When it is zero (0), it is off. When it is one (1) the light is on. In this scenario, first col first row light is on, second col, second row is on, and fifth col, first row is on. The rest are off.

This portion captures the joystick movement and the code is trying to identify which direction the joystick is being moved so that the snake can move appropriately based on the joystick direction input. We also check here if the snake is going on a specific direction and the joystick is being moved towards the opposite direction. In this case, nothing happens and the snake keeps moving as is (for example, if the snake is going right and the player moves the joystick direction to go left, nothing happens).

We also check here if the snake had collided with itself which in this case, should be the basis if it is game over or not.

This portion is to find out whether the snake had come across a Snake Food and successfully ate it. If it does, then the length of the snake increases by one (1) dot on the LED Matrix. We also specify here what happens during a game over and this is where we draw the sad face. Other things specified here is checking if the snake hits the sides of the LED Matrix which results in a Game Over.

This is where we check if the snake has eaten the food successfully and so we clear that dot or position where the previous food was and randomize the position of the next snake food in the LED Matrix. This code also contains the actual movement direction of the snake which means that for any direction the snake goes, we clear one dot and add one in front where the snake head is as long as there is a space to do so.

Apart from continuing the snake direction, we also specify here what will happen if the game is reset and in this case we re-initialize everything to how it is in the beginning. Voila! You are done!

The Result: