8*8 Rainbow LED Matrix Snake Game


LUCY 发布于 2018-01-20 12:16


Remember the snake game we played on our mobile or game box during our childhood? Today we are going to learn how to use Flexible Rainbow 64 RGB 8*8 LED Matrix to make a snake game. We use Joystick breakout module to control it.

1 X Freaduino UNO Rev2.2 MB_EFUNO 

1 X Flexible 64 RGB 8*8 Rainbow LED Matrix

1 X Joystick breakout module BKOUT_MD01

3 X Jumper Cable

Arduino IDE

Connect Flexible 64 RGB 8*8 Rainbow LED Matrix to port 

The code we are going to make includes the following sections:

  1. Generate food;
  2. Time Delay;
  3. Read Joystick Value;
  4. Gameover, display the score;
  5. Display start-up picture;
  6. Snake Movement;
  7. Calculate the score. Every 5 food eaten will increase snake speed for one time. But we have to limit the maximum speed.

We use Random Number Function to produce two random numbers for X axle and Y axleof the food separately. We have to judge whether the axleof food and the axleof snake are the same. If it is the same, we need to regenerate food.

Code Example:

[cceN_cpp theme="dawn"]

FX = random(0, 7);

FY = random(0, 7);

for (int i = 0; i < 3 + socre; i++)


if ( FX == sx[i] && FY == sy[i])

goto A;


Set A0-A2 to be input mode and read its voltage. The middle value is around 510. We have to judge the value and assign it with relative movement.

Code Example:

[cceN_cpp theme="dawn"]
xValue = analogRead(A0);

yValue = analogRead(A1);

if (yValue > 950 && KEY != 5) {

K = 8;


Firstly, we have to judge which direction the snake moves. Snake move is the axledata shift. That means the second section axleof snake equals the axleof snake head and the third equals the second. According to this rule, we assign the snake head new value. We have to pay attention that the snake body surpassed the screen frame will appear in the opposite frame.

Code Example:

[cceN_cpp theme="dawn"]
if (KEY == 8) // Snake upward


for (int i = 2 + socre; i > 0; i--)


sx[i] = sx[i - 1];

sy[i] = sy[i - 1];


sy[0] = sy[0] - 1;

if (sy[0] < 0) // The part of snake body surpassed screen frame will appear in the opposite frame.

sy[0] = 7;


We only have to judge whether the axleof snake head equals the axleof food. In other words, the food was eaten.Then we have to generate food axleand display it. At the same time, we have to judge whether the score of the game is a multiple of 5. If it is, then increase speed for one time and decrease delay time.

Code Example:

[cceN_cpp theme="dawn"]
if (SY == FY && SX == FX)



socre ++;

Colour[0] = 40; // Colour[1] = 0;

Colour[2] = 0;

c = strip.Color(Colour[1], Colour[0], Colour[2]);

x = light[FX][FY]; //Display Food

strip.setPixelColor(x, c);

strip.show();//Send data

if ( !(socre % 5)) { //Increase snake speed according to the score. Every 5 food eaten, increase snake speed by 100ms.

speedS = speedS - 50;

if (speedS < 150) // The minimum speed is 200ms. If snake speed lower than 200ms, the speed remains unchanged. speedS = 150;



When snake head touches snake body, game is over. And we get the score by judging whether the axle of snake head equals to the axle of snake body.

Code Example:

[cceN_cpp theme="dawn"]
for (int i = 1; i <= 2 + socre; i++)


if ( SX == sx[i] && SY == sy[i])





Light the First Bead on 8*8 Rainbow Matrix with Arduino

Get to Know WS2812b

8*8 Rainbow Matrix Pattern Display

8*8 Rainbow Matrix Scrolling Caption


如果想联系我们,请发邮件至:louise@elecfreaks.com 。





6粉丝 62作品 15随笔
  关注 私信


阅读数: 4197