# Arduino Snake Game With 8x8 LED Matrix

1,962

1

1

## Introduction: Arduino Snake Game With 8x8 LED Matrix

Hi Guys in this instructables we will learn how to make a snake game with Arduino uno & 8x8 LED Matrix display.
Since snake game was very popular so we will building its revision on our Arduino Uno and we will play snake game on our Arduino.

## Step 1: Things You Need

guys for this instructables we will need following things :

Arduino UNO

POT 1K

Push Buttons

## Step 2: Circuit Diagram

Circuit of the Snake Game Project is a little complex. we have connected a dot matrix display by using Shift Register 74HC595 & we have two shift registers are used, one for driving the columns and second for driving the rows. Control pins of both the registers, Column shift register and row shift register (SH, ST), are directly connected to Arduino’s pin number 14 and 16 respectively. And DS pin of column shift register and row shift register are directly connected to pin number 15 and 17 of Arduino. Start button for start the game is connected at pin number 3, left direction button at pin 4, right direction button at pin 6, up direction button at pin 2 and down direction button at pin 5. A LCD is also connected in our hardware to show score. RS and EN pins are directly connected at pin 13 and 12. RW pin is directly ground. And data pins d4-d7 are connected at pin 11, 10, 9, 8 of Arduino. Rest of connection are shown in the circuit diagram.

## Step 3: Code

Please copy the following code from below & upload it to your Arduino Board :

#include"LiquidCrystal.h"
LiquidCrystal lcd(13,12,11,10,9,8);
#define ds_col 15
#define sh_col 16
#define st_col 14
#define ds_row 17
#define start 3
#define up 2
#define down 5
#define left 4
#define right 6
char Col,Row,move_c,move_r;
int colum_data(int temp)
{
switch(temp)
{
case 1: return 1;break;
case 2: return 2; break;
case 3: return 4; break;
case 4: return 8; break;
case 5: return 16; break;
case 6: return 32; break;
case 7: return 64; break;
case 8: return 128; break;
default: return 0; break;
}
}
int row_data(int temp)
{
switch(temp)
{
case 1: return 1;break;
case 2: return 2; break;
case 3: return 4; break;
case 4: return 8; break;
case 5: return 16; break;
case 6: return 32; break;
case 7: return 64; break;
case 8: return 128; break;
default: return 0; break;
}
}
void read_button()
{
if(!digitalRead(left))
{
move_r=0;
move_c!=-1 ? move_c=-1 : move_c=1;
while(!digitalRead(left));
}
if(!digitalRead(right))
{
move_r=0;
move_c!=1 ? move_c=1 : move_c=-1;
while(!digitalRead(right));
}
if(!digitalRead(up))
{
move_c=0;
move_r!=-1 ? move_r=-1 : move_r=1;
while(!digitalRead(up));
}
if(!digitalRead(down))
{
move_c=0;
move_r!=1 ? move_r=1 : move_r=-1;
while(!digitalRead(down));
}
}
void show_snake(int temp)
{
for(int n=0;n {
int r,c;
for(int k=0;k<21;k++)
{
int temp1=Col[k];
c=colum_data(temp1);
int temp2=Row[k];
r=0xff-row_data(temp2);
for(int i=0;i<8;i++)
{
int ds=(c & 0x01);
digitalWrite(ds_col, ds);
ds=(r & 0x01);
digitalWrite(ds_row, ds);
digitalWrite(sh_col, HIGH);
c>>=1;
r>>=1;
digitalWrite(sh_col, LOW);
}
digitalWrite(st_col, HIGH);
digitalWrite(st_col, LOW);
read_button();
delayMicroseconds(500);
}
}
}
void setup()
{
lcd.begin(16,2);
pinMode(ds_col, OUTPUT);
pinMode(sh_col, OUTPUT);
pinMode(st_col, OUTPUT);
pinMode(ds_row, OUTPUT);
pinMode(start, INPUT);
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(left, INPUT);
pinMode(right, INPUT);
digitalWrite(up, HIGH);
digitalWrite(down, HIGH);
digitalWrite(left, HIGH);
digitalWrite(right, HIGH);
digitalWrite(start, HIGH);
lcd.setCursor(0,0);
lcd.print(" Snake game ");
lcd.setCursor(0,1);
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Press Start ");
lcd.setCursor(0,1);
lcd.print(" To Play ");
delay(2000);
}
void loop()
{
int j,k,Speed=40,score=0;
j=k=move_c=0;
move_r=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Score: ");
lcd.print(score);
while(1)
{
for(int i=3;i<21;i++)
{
Row[i]=100;
Col[i]=100;
}
Row=rand()%8+1;
Col=rand()%8+1;
Row=1;
Col=1;
Row=2;
Col=1;
j=2,k=1;
while(k==1)
{
move_c=0;
move_r=1;
show_snake(1);
lcd.setCursor(7,0);
lcd.print(score);
if(!digitalRead(start))
{
k=2;
Speed=40;
score=0;
}
}

while(k==2)
{
show_snake(Speed);
if(Row>8 || Col>8 || Row<0 || Col<0)
{
Row=1;
Col=1;
k=1;
lcd.setCursor(0,1);
lcd.print("Game Over");
delay(5000);
score=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Score: ");
lcd.print(score);
}
if(Row==Row+move_r && Col==Col+move_c)
{
j++;
Speed-=2;
score=score+5;
lcd.setCursor(7,0);
lcd.print(score);
Row=rand()%8+1;
Col=rand()%8+1;
}
for(int i=j;i>1;i--)
{
Col[i]=Col[i-1];
Row[i]=Row[i-1];
}
Col=Col+move_c;
Row=Row+move_r;
}
}
}

## Step 4: Play Snake on Arduino

And if you did everything correct as we mentioned in above steps then we are ready to play snake as you can see our snake game is ready to play and i am playing snake game with my arduino so you can make your snake game and you can play with your arduino. So have fun playing snake on your arduino.