This is a simple snake game aimed at children and hobbyists. This game is made using Arduino.
In this snake game Dot matrix display is used to display snake and food. And for game score and status such as Game Start and Game Over are displayed on 16×2 LCD . To control the game five buttons are used (Start, Up, Down, Left and Right). To drive the Dot Matrix display Shift registers are used. To start the game you must press Start button and then use other keys for direction.
Below are some snapshots of the entire project setup.
Figure 1: Arduino-based Snake game prototype
Figure 2: Image of the LCD Module showing initial messages in Snake Game
Figure 3: Image of the controller designed for the Arduino-based Snake game
Figure 4: Image of the Dot Matrix display used to play Snake Game
Figure 5: Arduino-based Snake game overview
Description of the circuit and components used
Circuit Description
There is a Dot Matrix display used and connected with Shift Register 74595 . Here two shift registers are used, one to drive the Columns and second to drive the Rows . Column shift register control pins ( ds, sh, st) are directly connected to the Arduino pin number 12, 10, 11 respectively and row shift register control pins ( ds, sh, st) are connected to the Arduino pin number 9, 8, 7 respectively.
Figure 6: Pin diagram of the matrix display used in the Snake game
A 16×2 LCD is also connected to this circuit to display Score and Game Status like Game over, Game start etc. Data pins of LCD from D4 to D7 are connected to Arduino PIN number (16, 15, 14, 13/A2, A1, A0, 13) and Control pins (RS, EN) of LCDs are connected to pin number of Arduino ( A4, A3). To play the game, Control Keys (Down, Left, Right, Up, Start) are connected with Digital Pins (2, 3, 4, 5, 6) of the Arduino.
Components used
1. Arduino ProMini
2. 8×8 Dot Matrix Screen
3. Shift Register 74595
4. Snap buttons
5. Register
6. Connecting wire
7. Bread Board
8. Power supply
Project source code
###
#includeLiquidCrystal 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 column,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
Arduino based snake game circuit diagram | ![]() |