Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Snake Game Using Arduino

By Salman Khan

 

This is a simple Snake Game targeted at children and for hobbyist. This game is made by using Arduino.

In this Snake Game Dot matrix display is used for displaying snake and food. And for score and game status like Game Start, and Game Over is displayed on 16×2 LCD.  For controlling the game five push buttons are used (Start, Up, Down, Left and Right). To drive the Dot matrix display Shift registers are used. To start the game you have to press Start button and then use other Keys for direction.

Below are some snapshots of the entire project setup.

Prototype of Arduino based Snake Game

Fig. 1: Prototype of Arduino based Snake Game

Image of LCD Module showing initial messages in Snake Game

Fig. 2: Image of LCD Module showing initial messages in Snake Game

Image of Controller designed for Arduino based Snake Game

Fig. 3: Image of Controller designed for Arduino based Snake Game

Image of Dot Matrix Display used to play Snake Game

Fig. 4: Image of Dot Matrix Display used to play Snake Game

Overview of Arduino based Snake Game

Fig. 5: Overview of Arduino based Snake Game

Circuit Description & Components Used

Circuit Description

There is a Dot matrix display used and connected with Shift Register 74595. Here two shift registers are used, one for driving the Columns and second for driving the Rows. Control pins of Column shift register (ds, sh, st) are directly connected to Arduino’s pin number 12, 10, 11 respectively and control pins of row shift register (ds,  sh,  st) are connected to Arduino’s pin number 9, 8, 7 respectively.

Pin Diagram of Dot Matrix Display used in Snake Game

Fig. 6: Pin Diagram of Dot Matrix Display used in Snake Game

A 16×2 LCD is also connected with this circuit for displaying Score and Status of game like Game Over, Game Start etc. LCD’s data pins from D4 to D7 are connected to Arduino pin number (16, 15, 14, 13 / A2, A1, A0, 13) and Control Pins (RS, EN) of LCD’s are connected with Arduino’s pin number (A4, A3). For playing the Game, Control Keys (Down, Left, Right, Up, Start) are connected with Digital Pins (2, 3, 4, 5, 6) of Arduino.

Components Used

1.      Arduino Pro Mini

2.      8×8 Dot Matrix Display

3.      Shift Register 74595

4.      Push Buttons

5.      Register

6.      Connecting Wire

7.      Bread Board

8.      Power Supply

Project Source Code

###

#include
LiquidCrystal lcd(18,17,16,15,14,13);
#define ds_col 12
#define sh_col 10
#define st_col 11

#define ds_row 9
#define sh_row 8
#define st_row 7

int start=6;
int left=3;
int right=4;
int up=5;
int down=2;

char Col[21];
char Row[21];

char addc,addr;

int col(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(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 key()
{
if(digitalRead(left) == 0)
{
   addr=0;
   if(addc!=-1)
   addc=-1;

   else
   addc=1;
   while(digitalRead(left) == 0);
}

if(digitalRead(right)== 0)
{
   addr=0;
   if(addc!=1)
   addc=1;

   else
   addc=-1;
   while(digitalRead(right) == 0);
}

if(digitalRead(up)== 0)
{
   addc=0;
   if(addr!=-1)
   addr=-1;

   else
   addr=1;
   while(digitalRead(up) == 0);
}

if(digitalRead(down) == 0)
{
   addc=0;
   if(addr!=1)
   addr=1;

   else
   addr=-1;
   while(digitalRead(down) == 0);
}
 
}

void Display(int temp)
{
int j,k;
char colum,roww;
for(j=0;j>=1;
         roww>>=1;
         digitalWrite(sh_col, HIGH);
         digitalWrite(sh_col, LOW);
         digitalWrite(sh_row, HIGH);
         digitalWrite(sh_row, LOW);
        }
         digitalWrite(st_col, HIGH);
         digitalWrite(st_col, LOW);
         digitalWrite(st_row, HIGH);
         digitalWrite(st_row, LOW);
         key();
         delayMicroseconds(600);
  }
}
}

void setup()
{  
  lcd.begin(16,2);
    pinMode(ds_col, OUTPUT);
    pinMode(sh_col, OUTPUT);
    pinMode(st_col, OUTPUT);
    pinMode(ds_row, OUTPUT);
    pinMode(sh_row, OUTPUT);
    pinMode(st_row, OUTPUT);
    pinMode(start, INPUT);
    pinMode(up, INPUT);
    pinMode(down, INPUT);
    pinMode(left, INPUT);
    pinMode(right, INPUT);
    lcd.setCursor(0,0);
    lcd.print(" Snake game on  ");
    lcd.setCursor(0,1);
    lcd.print("   Dot Matrix   ");
    delay(2000);
    lcd.setCursor(0,0);
    lcd.print(" By Saddam Khan ");
    lcd.setCursor(0,1);
    lcd.print("Engineers Garage");
    delay(2000);
}

void loop()
{
  int i,j,k,spd=40,score=0;
  j=k=0;
  addc=0;
  addr=1;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Score: ");
  lcd.print(score);
  while(1)
  {
   for(i=3;i<21;i++)
   {
    Row[i]=100;
Col[i]=100;
   }

  Row[0]=rand()%8+1;
  Col[0]=rand()%8+1;

  Row[1]=1;
  Col[1]=1;
   
  Row[2]=2;
  Col[2]=1;
  j=2;
  k=1;
  while(k==1)
  {
   addc=0;
   addr=1;
   Display(1);
    lcd.clear();
     lcd.setCursor(0,0);
  lcd.print("Score: ");
  lcd.print(score);
   if(digitalRead(start)==0)
   {
    k=2;
    spd=40;
    score=0;
   }
  }
 
  while(k==2)
  {
    Display(spd);
        if(Row[1]>8 || Col[1]>8 || Row[1]<0 || Col[1]<0)
{
  Row[1]=1;
         Col[1]=1;
  k=1;
         lcd.setCursor(0,1);
         lcd.print("Game Over");
         delay(5000);
}

if(Row[0]==Row[1]+addr  &&  Col[0]==Col[1]+addc)
{
  j++;
         spd=spd-2;
         score=score+5;
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Score: ");
         lcd.print(score);
  Row[0]=rand()%8+1;
         Col[0]=rand()%8+1;
}

for(i=j;i>1;i--)
{
  Col[i]=Col[i-1];
  Row[i]=Row[i-1];
}
Col[1]=Col[2]+addc;
Row[1]=Row[2]+addr;
  }
  }
}



 
 

###

 


Circuit Diagrams

Circuit-Diagram-Arduino-Based-Snake-Game

Project Video


Filed Under: Electronic Projects
Tagged With: Arduino, dot matrix display, snake game
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces
  • Renesas’ RA Family microcontrollers earn CAVP certification for cryptographic algorithms
  • MicroPython: Serial data communication in ESP8266 and ESP32 using UART

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Problem in opening Orcad schematic file
  • Boost converter, 3W...shielded or unshielded inductor?
  • Switching frequency data not available
  • PHY to MAC communication: issue with SMI (MDIO+MDC) bus
  • Looking for miniature shaded pole motors

RSS Electro-Tech-Online.com Discussions

  • Background of Members Here
  • Opamp ciruit
  • DIY Mini 12v Router UPS malfunction
  • How can a 13 bit number represent 16 bit number?Or why is fragmentation offset multiple of 8?
  • Best home PCB printer and software for prototyping?
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering